fix setsid() for sub-namespace /sbin/init
[linux-2.6/libata-dev.git] / fs / hostfs / hostfs_kern.c
blob2b9b35733aacd00be2673995887521263ae66a39
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 "hostfs.h"
15 #include "init.h"
16 #include "kern.h"
18 struct hostfs_inode_info {
19 char *host_filename;
20 int fd;
21 int mode;
22 struct inode vfs_inode;
25 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
27 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
30 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
32 int hostfs_d_delete(struct dentry *dentry)
34 return 1;
37 struct dentry_operations hostfs_dentry_ops = {
38 .d_delete = hostfs_d_delete,
41 /* Changed in hostfs_args before the kernel starts running */
42 static char *root_ino = "";
43 static int append = 0;
45 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
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;
51 #ifndef MODULE
52 static int __init hostfs_args(char *options, int *add)
54 char *ptr;
56 ptr = strchr(options, ',');
57 if (ptr != NULL)
58 *ptr++ = '\0';
59 if (*options != '\0')
60 root_ino = options;
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);
73 options = ptr;
75 return 0;
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"
88 #endif
90 static char *dentry_name(struct dentry *dentry, int extra)
92 struct dentry *parent;
93 char *root, *name;
94 int len;
96 len = 0;
97 parent = dentry;
98 while (parent->d_parent != parent) {
99 len += parent->d_name.len + 1;
100 parent = parent->d_parent;
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;
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;
118 strncpy(name, root, strlen(root));
119 return name;
122 static char *inode_name(struct inode *ino, int extra)
124 struct dentry *dentry;
126 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
127 return dentry_name(dentry, extra);
130 static int read_name(struct inode *ino, char *name)
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
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;
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;
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;
157 static char *follow_link(char *link)
159 int len, n;
160 char *name, *resolved, *end;
162 len = 64;
163 while (1) {
164 n = -ENOMEM;
165 name = kmalloc(len, GFP_KERNEL);
166 if (name == NULL)
167 goto out;
169 n = do_readlink(link, name, len);
170 if (n < len)
171 break;
172 len *= 2;
173 kfree(name);
175 if (n < 0)
176 goto out_free;
178 if (*name == '/')
179 return name;
181 end = strrchr(link, '/');
182 if (end == NULL)
183 return name;
185 *(end + 1) = '\0';
186 len = strlen(link) + strlen(name) + 1;
188 resolved = kmalloc(len, GFP_KERNEL);
189 if (resolved == NULL) {
190 n = -ENOMEM;
191 goto out_free;
194 sprintf(resolved, "%s%s", link, name);
195 kfree(name);
196 kfree(link);
197 return resolved;
199 out_free:
200 kfree(name);
201 out:
202 return ERR_PTR(n);
205 static int hostfs_read_inode(struct inode *ino)
207 char *name;
208 int err = 0;
211 * Unfortunately, we are called from iget() when we don't have a dentry
212 * allocated yet.
214 if (list_empty(&ino->i_dentry))
215 goto out;
217 err = -ENOMEM;
218 name = inode_name(ino, 0);
219 if (name == NULL)
220 goto out;
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;
230 err = read_name(ino, name);
231 kfree(name);
232 out:
233 return err;
236 static struct inode *hostfs_iget(struct super_block *sb)
238 struct inode *inode;
239 long ret;
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);
250 unlock_new_inode(inode);
252 return inode;
255 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
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
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;
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;
284 static struct inode *hostfs_alloc_inode(struct super_block *sb)
286 struct hostfs_inode_info *hi;
288 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
289 if (hi == NULL)
290 return NULL;
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;
299 static void hostfs_delete_inode(struct inode *inode)
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;
306 clear_inode(inode);
309 static void hostfs_destroy_inode(struct inode *inode)
311 kfree(HOSTFS_I(inode)->host_filename);
314 * XXX: This should not happen, probably. The check is here for
315 * additional safety.
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");
322 kfree(HOSTFS_I(inode));
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,
333 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
335 void *dir;
336 char *name;
337 unsigned long long next, ino;
338 int error, len;
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;
354 close_dir(dir);
355 return 0;
358 int hostfs_file_open(struct inode *ino, struct file *file)
360 char *name;
361 int mode = 0, r = 0, w = 0, fd;
363 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
364 if ((mode & HOSTFS_I(ino)->mode) == mode)
365 return 0;
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.
371 if (HOSTFS_I(ino)->fd != -1) {
372 close_file(&HOSTFS_I(ino)->fd);
373 HOSTFS_I(ino)->fd = -1;
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;
384 name = dentry_name(file->f_path.dentry, 0);
385 if (name == NULL)
386 return -ENOMEM;
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;
394 return 0;
397 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
399 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
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,
415 static const struct file_operations hostfs_dir_fops = {
416 .llseek = generic_file_llseek,
417 .readdir = hostfs_readdir,
418 .read = generic_read_dir,
421 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
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;
431 if (page->index >= end_index)
432 count = inode->i_size & (PAGE_CACHE_SIZE-1);
434 buffer = kmap(page);
435 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
437 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
438 if (err != count) {
439 ClearPageUptodate(page);
440 goto out;
443 if (base > inode->i_size)
444 inode->i_size = base;
446 if (PageError(page))
447 ClearPageError(page);
448 err = 0;
450 out:
451 kunmap(page);
453 unlock_page(page);
454 return err;
457 int hostfs_readpage(struct file *file, struct page *page)
459 char *buffer;
460 long long start;
461 int err = 0;
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;
470 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
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;
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)
486 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
488 *pagep = __grab_cache_page(mapping, index);
489 if (!*pagep)
490 return -ENOMEM;
491 return 0;
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)
498 struct inode *inode = mapping->host;
499 void *buffer;
500 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
501 int err;
503 buffer = kmap(page);
504 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
505 kunmap(page);
507 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
508 SetPageUptodate(page);
511 * If err > 0, write_file has added err to pos, so we are comparing
512 * i_size against the last byte written.
514 if (err > 0 && (pos > inode->i_size))
515 inode->i_size = pos;
516 unlock_page(page);
517 page_cache_release(page);
519 return err;
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,
530 static int init_inode(struct inode *inode, struct dentry *dentry)
532 char *name;
533 int type, err = -ENOMEM;
534 int maj, min;
535 dev_t rdev = 0;
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);
546 else type = OS_TYPE_DIR;
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;
555 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
556 else inode->i_fop = &hostfs_file_fops;
558 if (type == OS_TYPE_SYMLINK)
559 inode->i_mapping->a_ops = &hostfs_link_aops;
560 else inode->i_mapping->a_ops = &hostfs_aops;
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;
576 out:
577 return err;
580 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
581 struct nameidata *nd)
583 struct inode *inode;
584 char *name;
585 int error, fd;
587 inode = hostfs_iget(dir->i_sb);
588 if (IS_ERR(inode)) {
589 error = PTR_ERR(inode);
590 goto out;
593 error = init_inode(inode, dentry);
594 if (error)
595 goto out_put;
597 error = -ENOMEM;
598 name = dentry_name(dentry, 0);
599 if (name == NULL)
600 goto out_put;
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);
610 kfree(name);
611 if (error)
612 goto out_put;
614 HOSTFS_I(inode)->fd = fd;
615 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
616 d_instantiate(dentry, inode);
617 return 0;
619 out_put:
620 iput(inode);
621 out:
622 return error;
625 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
626 struct nameidata *nd)
628 struct inode *inode;
629 char *name;
630 int err;
632 inode = hostfs_iget(ino->i_sb);
633 if (IS_ERR(inode)) {
634 err = PTR_ERR(inode);
635 goto out;
638 err = init_inode(inode, dentry);
639 if (err)
640 goto out_put;
642 err = -ENOMEM;
643 name = dentry_name(dentry, 0);
644 if (name == NULL)
645 goto out_put;
647 err = read_name(inode, name);
648 kfree(name);
649 if (err == -ENOENT) {
650 iput(inode);
651 inode = NULL;
653 else if (err)
654 goto out_put;
656 d_add(dentry, inode);
657 dentry->d_op = &hostfs_dentry_ops;
658 return NULL;
660 out_put:
661 iput(inode);
662 out:
663 return ERR_PTR(err);
666 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
668 char *file;
669 int len;
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;
681 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
683 char *from_name, *to_name;
684 int err;
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;
693 err = link_file(to_name, from_name);
694 kfree(from_name);
695 kfree(to_name);
696 return err;
699 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
701 char *file;
702 int err;
704 if ((file = inode_dentry_name(ino, dentry)) == NULL)
705 return -ENOMEM;
706 if (append)
707 return -EPERM;
709 err = unlink_file(file);
710 kfree(file);
711 return err;
714 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
716 char *file;
717 int err;
719 if ((file = inode_dentry_name(ino, dentry)) == NULL)
720 return -ENOMEM;
721 err = make_symlink(file, to);
722 kfree(file);
723 return err;
726 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
728 char *file;
729 int err;
731 if ((file = inode_dentry_name(ino, dentry)) == NULL)
732 return -ENOMEM;
733 err = do_mkdir(file, mode);
734 kfree(file);
735 return err;
738 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
740 char *file;
741 int err;
743 if ((file = inode_dentry_name(ino, dentry)) == NULL)
744 return -ENOMEM;
745 err = do_rmdir(file);
746 kfree(file);
747 return err;
750 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
752 struct inode *inode;
753 char *name;
754 int err;
756 inode = hostfs_iget(dir->i_sb);
757 if (IS_ERR(inode)) {
758 err = PTR_ERR(inode);
759 goto out;
762 err = init_inode(inode, dentry);
763 if (err)
764 goto out_put;
766 err = -ENOMEM;
767 name = dentry_name(dentry, 0);
768 if (name == NULL)
769 goto out_put;
771 init_special_inode(inode, mode, dev);
772 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
773 if (err)
774 goto out_free;
776 err = read_name(inode, name);
777 kfree(name);
778 if (err)
779 goto out_put;
781 d_instantiate(dentry, inode);
782 return 0;
784 out_free:
785 kfree(name);
786 out_put:
787 iput(inode);
788 out:
789 return err;
792 int hostfs_rename(struct inode *from_ino, struct dentry *from,
793 struct inode *to_ino, struct dentry *to)
795 char *from_name, *to_name;
796 int err;
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;
804 err = rename_file(from_name, to_name);
805 kfree(from_name);
806 kfree(to_name);
807 return err;
810 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
812 char *name;
813 int r = 0, w = 0, x = 0, err;
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;
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;
833 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
835 struct hostfs_iattr attrs;
836 char *name;
837 int err;
839 int fd = HOSTFS_I(dentry->d_inode)->fd;
841 err = inode_change_ok(dentry->d_inode, attr);
842 if (err)
843 return err;
845 if (append)
846 attr->ia_valid &= ~ATTR_SIZE;
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;
853 if (attr->ia_valid & ATTR_UID) {
854 attrs.ia_valid |= HOSTFS_ATTR_UID;
855 attrs.ia_uid = attr->ia_uid;
857 if (attr->ia_valid & ATTR_GID) {
858 attrs.ia_valid |= HOSTFS_ATTR_GID;
859 attrs.ia_gid = attr->ia_gid;
861 if (attr->ia_valid & ATTR_SIZE) {
862 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
863 attrs.ia_size = attr->ia_size;
865 if (attr->ia_valid & ATTR_ATIME) {
866 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
867 attrs.ia_atime = attr->ia_atime;
869 if (attr->ia_valid & ATTR_MTIME) {
870 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
871 attrs.ia_mtime = attr->ia_mtime;
873 if (attr->ia_valid & ATTR_CTIME) {
874 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
875 attrs.ia_ctime = attr->ia_ctime;
877 if (attr->ia_valid & ATTR_ATIME_SET) {
878 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
880 if (attr->ia_valid & ATTR_MTIME_SET) {
881 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
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;
891 return inode_setattr(dentry->d_inode, attr);
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,
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,
921 int hostfs_link_readpage(struct file *file, struct page *page)
923 char *buffer, *name;
924 int err;
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;
940 kunmap(page);
941 unlock_page(page);
942 return err;
945 static const struct address_space_operations hostfs_link_aops = {
946 .readpage = hostfs_link_readpage,
949 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
951 struct inode *root_inode;
952 char *host_root_path, *req_root = d;
953 int err;
955 sb->s_blocksize = 1024;
956 sb->s_blocksize_bits = 10;
957 sb->s_magic = HOSTFS_SUPER_MAGIC;
958 sb->s_op = &hostfs_sbops;
960 /* NULL is printed as <NULL> by sprintf: avoid that. */
961 if (req_root == NULL)
962 req_root = "";
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;
970 sprintf(host_root_path, "%s/%s", root_ino, req_root);
972 root_inode = hostfs_iget(sb);
973 if (IS_ERR(root_inode)) {
974 err = PTR_ERR(root_inode);
975 goto out_free;
978 err = init_inode(root_inode, NULL);
979 if (err)
980 goto out_put;
982 HOSTFS_I(root_inode)->host_filename = host_root_path;
984 * Avoid that in the error path, iput(root_inode) frees again
985 * host_root_path through hostfs_destroy_inode!
987 host_root_path = NULL;
989 err = -ENOMEM;
990 sb->s_root = d_alloc_root(root_inode);
991 if (sb->s_root == NULL)
992 goto out_put;
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;
1002 return 0;
1004 out_put:
1005 iput(root_inode);
1006 out_free:
1007 kfree(host_root_path);
1008 out:
1009 return err;
1012 static int hostfs_read_sb(struct file_system_type *type,
1013 int flags, const char *dev_name,
1014 void *data, struct vfsmount *mnt)
1016 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
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,
1027 static int __init init_hostfs(void)
1029 return register_filesystem(&hostfs_type);
1032 static void __exit exit_hostfs(void)
1034 unregister_filesystem(&hostfs_type);
1037 module_init(init_hostfs)
1038 module_exit(exit_hostfs)
1039 MODULE_LICENSE("GPL");