[IPV4]: Consolidate the ip cork destruction in ip_output.c
[wrt350n-kernel.git] / fs / hostfs / hostfs_kern.c
blob8966b050196e33b2f717fd687dd45df9d58c48bd
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 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 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
239 * do_statfs uses struct statfs64 internally, but the linux kernel
240 * struct statfs still has 32-bit versions for most of these fields,
241 * so we convert them here
243 int err;
244 long long f_blocks;
245 long long f_bfree;
246 long long f_bavail;
247 long long f_files;
248 long long f_ffree;
250 err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
251 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
252 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
253 &sf->f_namelen, sf->f_spare);
254 if (err)
255 return err;
256 sf->f_blocks = f_blocks;
257 sf->f_bfree = f_bfree;
258 sf->f_bavail = f_bavail;
259 sf->f_files = f_files;
260 sf->f_ffree = f_ffree;
261 sf->f_type = HOSTFS_SUPER_MAGIC;
262 return 0;
265 static struct inode *hostfs_alloc_inode(struct super_block *sb)
267 struct hostfs_inode_info *hi;
269 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
270 if (hi == NULL)
271 return NULL;
273 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
274 .fd = -1,
275 .mode = 0 });
276 inode_init_once(&hi->vfs_inode);
277 return &hi->vfs_inode;
280 static void hostfs_delete_inode(struct inode *inode)
282 truncate_inode_pages(&inode->i_data, 0);
283 if (HOSTFS_I(inode)->fd != -1) {
284 close_file(&HOSTFS_I(inode)->fd);
285 HOSTFS_I(inode)->fd = -1;
287 clear_inode(inode);
290 static void hostfs_destroy_inode(struct inode *inode)
292 kfree(HOSTFS_I(inode)->host_filename);
295 * XXX: This should not happen, probably. The check is here for
296 * additional safety.
298 if (HOSTFS_I(inode)->fd != -1) {
299 close_file(&HOSTFS_I(inode)->fd);
300 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
303 kfree(HOSTFS_I(inode));
306 static void hostfs_read_inode(struct inode *inode)
308 read_inode(inode);
311 static const struct super_operations hostfs_sbops = {
312 .alloc_inode = hostfs_alloc_inode,
313 .drop_inode = generic_delete_inode,
314 .delete_inode = hostfs_delete_inode,
315 .destroy_inode = hostfs_destroy_inode,
316 .read_inode = hostfs_read_inode,
317 .statfs = hostfs_statfs,
320 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
322 void *dir;
323 char *name;
324 unsigned long long next, ino;
325 int error, len;
327 name = dentry_name(file->f_path.dentry, 0);
328 if (name == NULL)
329 return -ENOMEM;
330 dir = open_dir(name, &error);
331 kfree(name);
332 if (dir == NULL)
333 return -error;
334 next = file->f_pos;
335 while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
336 error = (*filldir)(ent, name, len, file->f_pos,
337 ino, DT_UNKNOWN);
338 if (error) break;
339 file->f_pos = next;
341 close_dir(dir);
342 return 0;
345 int hostfs_file_open(struct inode *ino, struct file *file)
347 char *name;
348 int mode = 0, r = 0, w = 0, fd;
350 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
351 if ((mode & HOSTFS_I(ino)->mode) == mode)
352 return 0;
355 * The file may already have been opened, but with the wrong access,
356 * so this resets things and reopens the file with the new access.
358 if (HOSTFS_I(ino)->fd != -1) {
359 close_file(&HOSTFS_I(ino)->fd);
360 HOSTFS_I(ino)->fd = -1;
363 HOSTFS_I(ino)->mode |= mode;
364 if (HOSTFS_I(ino)->mode & FMODE_READ)
365 r = 1;
366 if (HOSTFS_I(ino)->mode & FMODE_WRITE)
367 w = 1;
368 if (w)
369 r = 1;
371 name = dentry_name(file->f_path.dentry, 0);
372 if (name == NULL)
373 return -ENOMEM;
375 fd = open_file(name, r, w, append);
376 kfree(name);
377 if (fd < 0)
378 return fd;
379 FILE_HOSTFS_I(file)->fd = fd;
381 return 0;
384 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
386 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
389 static const struct file_operations hostfs_file_fops = {
390 .llseek = generic_file_llseek,
391 .read = do_sync_read,
392 .splice_read = generic_file_splice_read,
393 .aio_read = generic_file_aio_read,
394 .aio_write = generic_file_aio_write,
395 .write = do_sync_write,
396 .mmap = generic_file_mmap,
397 .open = hostfs_file_open,
398 .release = NULL,
399 .fsync = hostfs_fsync,
402 static const struct file_operations hostfs_dir_fops = {
403 .llseek = generic_file_llseek,
404 .readdir = hostfs_readdir,
405 .read = generic_read_dir,
408 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
410 struct address_space *mapping = page->mapping;
411 struct inode *inode = mapping->host;
412 char *buffer;
413 unsigned long long base;
414 int count = PAGE_CACHE_SIZE;
415 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
416 int err;
418 if (page->index >= end_index)
419 count = inode->i_size & (PAGE_CACHE_SIZE-1);
421 buffer = kmap(page);
422 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
424 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
425 if (err != count) {
426 ClearPageUptodate(page);
427 goto out;
430 if (base > inode->i_size)
431 inode->i_size = base;
433 if (PageError(page))
434 ClearPageError(page);
435 err = 0;
437 out:
438 kunmap(page);
440 unlock_page(page);
441 return err;
444 int hostfs_readpage(struct file *file, struct page *page)
446 char *buffer;
447 long long start;
448 int err = 0;
450 start = (long long) page->index << PAGE_CACHE_SHIFT;
451 buffer = kmap(page);
452 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
453 PAGE_CACHE_SIZE);
454 if (err < 0)
455 goto out;
457 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
459 flush_dcache_page(page);
460 SetPageUptodate(page);
461 if (PageError(page)) ClearPageError(page);
462 err = 0;
463 out:
464 kunmap(page);
465 unlock_page(page);
466 return err;
469 int hostfs_write_begin(struct file *file, struct address_space *mapping,
470 loff_t pos, unsigned len, unsigned flags,
471 struct page **pagep, void **fsdata)
473 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
475 *pagep = __grab_cache_page(mapping, index);
476 if (!*pagep)
477 return -ENOMEM;
478 return 0;
481 int hostfs_write_end(struct file *file, struct address_space *mapping,
482 loff_t pos, unsigned len, unsigned copied,
483 struct page *page, void *fsdata)
485 struct inode *inode = mapping->host;
486 void *buffer;
487 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
488 int err;
490 buffer = kmap(page);
491 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
492 kunmap(page);
494 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
495 SetPageUptodate(page);
498 * If err > 0, write_file has added err to pos, so we are comparing
499 * i_size against the last byte written.
501 if (err > 0 && (pos > inode->i_size))
502 inode->i_size = pos;
503 unlock_page(page);
504 page_cache_release(page);
506 return err;
509 static const struct address_space_operations hostfs_aops = {
510 .writepage = hostfs_writepage,
511 .readpage = hostfs_readpage,
512 .set_page_dirty = __set_page_dirty_nobuffers,
513 .write_begin = hostfs_write_begin,
514 .write_end = hostfs_write_end,
517 static int init_inode(struct inode *inode, struct dentry *dentry)
519 char *name;
520 int type, err = -ENOMEM;
521 int maj, min;
522 dev_t rdev = 0;
524 if (dentry) {
525 name = dentry_name(dentry, 0);
526 if (name == NULL)
527 goto out;
528 type = file_type(name, &maj, &min);
529 /* Reencode maj and min with the kernel encoding.*/
530 rdev = MKDEV(maj, min);
531 kfree(name);
533 else type = OS_TYPE_DIR;
535 err = 0;
536 if (type == OS_TYPE_SYMLINK)
537 inode->i_op = &page_symlink_inode_operations;
538 else if (type == OS_TYPE_DIR)
539 inode->i_op = &hostfs_dir_iops;
540 else inode->i_op = &hostfs_iops;
542 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
543 else inode->i_fop = &hostfs_file_fops;
545 if (type == OS_TYPE_SYMLINK)
546 inode->i_mapping->a_ops = &hostfs_link_aops;
547 else inode->i_mapping->a_ops = &hostfs_aops;
549 switch (type) {
550 case OS_TYPE_CHARDEV:
551 init_special_inode(inode, S_IFCHR, rdev);
552 break;
553 case OS_TYPE_BLOCKDEV:
554 init_special_inode(inode, S_IFBLK, rdev);
555 break;
556 case OS_TYPE_FIFO:
557 init_special_inode(inode, S_IFIFO, 0);
558 break;
559 case OS_TYPE_SOCK:
560 init_special_inode(inode, S_IFSOCK, 0);
561 break;
563 out:
564 return err;
567 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
568 struct nameidata *nd)
570 struct inode *inode;
571 char *name;
572 int error, fd;
574 error = -ENOMEM;
575 inode = iget(dir->i_sb, 0);
576 if (inode == NULL)
577 goto out;
579 error = init_inode(inode, dentry);
580 if (error)
581 goto out_put;
583 error = -ENOMEM;
584 name = dentry_name(dentry, 0);
585 if (name == NULL)
586 goto out_put;
588 fd = file_create(name,
589 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
590 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
591 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
592 if (fd < 0)
593 error = fd;
594 else error = read_name(inode, name);
596 kfree(name);
597 if (error)
598 goto out_put;
600 HOSTFS_I(inode)->fd = fd;
601 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
602 d_instantiate(dentry, inode);
603 return 0;
605 out_put:
606 iput(inode);
607 out:
608 return error;
611 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
612 struct nameidata *nd)
614 struct inode *inode;
615 char *name;
616 int err;
618 err = -ENOMEM;
619 inode = iget(ino->i_sb, 0);
620 if (inode == NULL)
621 goto out;
623 err = init_inode(inode, dentry);
624 if (err)
625 goto out_put;
627 err = -ENOMEM;
628 name = dentry_name(dentry, 0);
629 if (name == NULL)
630 goto out_put;
632 err = read_name(inode, name);
633 kfree(name);
634 if (err == -ENOENT) {
635 iput(inode);
636 inode = NULL;
638 else if (err)
639 goto out_put;
641 d_add(dentry, inode);
642 dentry->d_op = &hostfs_dentry_ops;
643 return NULL;
645 out_put:
646 iput(inode);
647 out:
648 return ERR_PTR(err);
651 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
653 char *file;
654 int len;
656 file = inode_name(ino, dentry->d_name.len + 1);
657 if (file == NULL)
658 return NULL;
659 strcat(file, "/");
660 len = strlen(file);
661 strncat(file, dentry->d_name.name, dentry->d_name.len);
662 file[len + dentry->d_name.len] = '\0';
663 return file;
666 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
668 char *from_name, *to_name;
669 int err;
671 if ((from_name = inode_dentry_name(ino, from)) == NULL)
672 return -ENOMEM;
673 to_name = dentry_name(to, 0);
674 if (to_name == NULL) {
675 kfree(from_name);
676 return -ENOMEM;
678 err = link_file(to_name, from_name);
679 kfree(from_name);
680 kfree(to_name);
681 return err;
684 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
686 char *file;
687 int err;
689 if ((file = inode_dentry_name(ino, dentry)) == NULL)
690 return -ENOMEM;
691 if (append)
692 return -EPERM;
694 err = unlink_file(file);
695 kfree(file);
696 return err;
699 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
701 char *file;
702 int err;
704 if ((file = inode_dentry_name(ino, dentry)) == NULL)
705 return -ENOMEM;
706 err = make_symlink(file, to);
707 kfree(file);
708 return err;
711 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
713 char *file;
714 int err;
716 if ((file = inode_dentry_name(ino, dentry)) == NULL)
717 return -ENOMEM;
718 err = do_mkdir(file, mode);
719 kfree(file);
720 return err;
723 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
725 char *file;
726 int err;
728 if ((file = inode_dentry_name(ino, dentry)) == NULL)
729 return -ENOMEM;
730 err = do_rmdir(file);
731 kfree(file);
732 return err;
735 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
737 struct inode *inode;
738 char *name;
739 int err = -ENOMEM;
741 inode = iget(dir->i_sb, 0);
742 if (inode == NULL)
743 goto out;
745 err = init_inode(inode, dentry);
746 if (err)
747 goto out_put;
749 err = -ENOMEM;
750 name = dentry_name(dentry, 0);
751 if (name == NULL)
752 goto out_put;
754 init_special_inode(inode, mode, dev);
755 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
756 if (err)
757 goto out_free;
759 err = read_name(inode, name);
760 kfree(name);
761 if (err)
762 goto out_put;
764 d_instantiate(dentry, inode);
765 return 0;
767 out_free:
768 kfree(name);
769 out_put:
770 iput(inode);
771 out:
772 return err;
775 int hostfs_rename(struct inode *from_ino, struct dentry *from,
776 struct inode *to_ino, struct dentry *to)
778 char *from_name, *to_name;
779 int err;
781 if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
782 return -ENOMEM;
783 if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
784 kfree(from_name);
785 return -ENOMEM;
787 err = rename_file(from_name, to_name);
788 kfree(from_name);
789 kfree(to_name);
790 return err;
793 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
795 char *name;
796 int r = 0, w = 0, x = 0, err;
798 if (desired & MAY_READ) r = 1;
799 if (desired & MAY_WRITE) w = 1;
800 if (desired & MAY_EXEC) x = 1;
801 name = inode_name(ino, 0);
802 if (name == NULL)
803 return -ENOMEM;
805 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
806 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
807 err = 0;
808 else
809 err = access_file(name, r, w, x);
810 kfree(name);
811 if (!err)
812 err = generic_permission(ino, desired, NULL);
813 return err;
816 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
818 struct hostfs_iattr attrs;
819 char *name;
820 int err;
822 int fd = HOSTFS_I(dentry->d_inode)->fd;
824 err = inode_change_ok(dentry->d_inode, attr);
825 if (err)
826 return err;
828 if (append)
829 attr->ia_valid &= ~ATTR_SIZE;
831 attrs.ia_valid = 0;
832 if (attr->ia_valid & ATTR_MODE) {
833 attrs.ia_valid |= HOSTFS_ATTR_MODE;
834 attrs.ia_mode = attr->ia_mode;
836 if (attr->ia_valid & ATTR_UID) {
837 attrs.ia_valid |= HOSTFS_ATTR_UID;
838 attrs.ia_uid = attr->ia_uid;
840 if (attr->ia_valid & ATTR_GID) {
841 attrs.ia_valid |= HOSTFS_ATTR_GID;
842 attrs.ia_gid = attr->ia_gid;
844 if (attr->ia_valid & ATTR_SIZE) {
845 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
846 attrs.ia_size = attr->ia_size;
848 if (attr->ia_valid & ATTR_ATIME) {
849 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
850 attrs.ia_atime = attr->ia_atime;
852 if (attr->ia_valid & ATTR_MTIME) {
853 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
854 attrs.ia_mtime = attr->ia_mtime;
856 if (attr->ia_valid & ATTR_CTIME) {
857 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
858 attrs.ia_ctime = attr->ia_ctime;
860 if (attr->ia_valid & ATTR_ATIME_SET) {
861 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
863 if (attr->ia_valid & ATTR_MTIME_SET) {
864 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
866 name = dentry_name(dentry, 0);
867 if (name == NULL)
868 return -ENOMEM;
869 err = set_attr(name, &attrs, fd);
870 kfree(name);
871 if (err)
872 return err;
874 return inode_setattr(dentry->d_inode, attr);
877 static const struct inode_operations hostfs_iops = {
878 .create = hostfs_create,
879 .link = hostfs_link,
880 .unlink = hostfs_unlink,
881 .symlink = hostfs_symlink,
882 .mkdir = hostfs_mkdir,
883 .rmdir = hostfs_rmdir,
884 .mknod = hostfs_mknod,
885 .rename = hostfs_rename,
886 .permission = hostfs_permission,
887 .setattr = hostfs_setattr,
890 static const struct inode_operations hostfs_dir_iops = {
891 .create = hostfs_create,
892 .lookup = hostfs_lookup,
893 .link = hostfs_link,
894 .unlink = hostfs_unlink,
895 .symlink = hostfs_symlink,
896 .mkdir = hostfs_mkdir,
897 .rmdir = hostfs_rmdir,
898 .mknod = hostfs_mknod,
899 .rename = hostfs_rename,
900 .permission = hostfs_permission,
901 .setattr = hostfs_setattr,
904 int hostfs_link_readpage(struct file *file, struct page *page)
906 char *buffer, *name;
907 int err;
909 buffer = kmap(page);
910 name = inode_name(page->mapping->host, 0);
911 if (name == NULL)
912 return -ENOMEM;
913 err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
914 kfree(name);
915 if (err == PAGE_CACHE_SIZE)
916 err = -E2BIG;
917 else if (err > 0) {
918 flush_dcache_page(page);
919 SetPageUptodate(page);
920 if (PageError(page)) ClearPageError(page);
921 err = 0;
923 kunmap(page);
924 unlock_page(page);
925 return err;
928 static const struct address_space_operations hostfs_link_aops = {
929 .readpage = hostfs_link_readpage,
932 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
934 struct inode *root_inode;
935 char *host_root_path, *req_root = d;
936 int err;
938 sb->s_blocksize = 1024;
939 sb->s_blocksize_bits = 10;
940 sb->s_magic = HOSTFS_SUPER_MAGIC;
941 sb->s_op = &hostfs_sbops;
943 /* NULL is printed as <NULL> by sprintf: avoid that. */
944 if (req_root == NULL)
945 req_root = "";
947 err = -ENOMEM;
948 host_root_path = kmalloc(strlen(root_ino) + 1
949 + strlen(req_root) + 1, GFP_KERNEL);
950 if (host_root_path == NULL)
951 goto out;
953 sprintf(host_root_path, "%s/%s", root_ino, req_root);
955 root_inode = iget(sb, 0);
956 if (root_inode == NULL)
957 goto out_free;
959 err = init_inode(root_inode, NULL);
960 if (err)
961 goto out_put;
963 HOSTFS_I(root_inode)->host_filename = host_root_path;
965 * Avoid that in the error path, iput(root_inode) frees again
966 * host_root_path through hostfs_destroy_inode!
968 host_root_path = NULL;
970 err = -ENOMEM;
971 sb->s_root = d_alloc_root(root_inode);
972 if (sb->s_root == NULL)
973 goto out_put;
975 err = read_inode(root_inode);
976 if (err) {
977 /* No iput in this case because the dput does that for us */
978 dput(sb->s_root);
979 sb->s_root = NULL;
980 goto out;
983 return 0;
985 out_put:
986 iput(root_inode);
987 out_free:
988 kfree(host_root_path);
989 out:
990 return err;
993 static int hostfs_read_sb(struct file_system_type *type,
994 int flags, const char *dev_name,
995 void *data, struct vfsmount *mnt)
997 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
1000 static struct file_system_type hostfs_type = {
1001 .owner = THIS_MODULE,
1002 .name = "hostfs",
1003 .get_sb = hostfs_read_sb,
1004 .kill_sb = kill_anon_super,
1005 .fs_flags = 0,
1008 static int __init init_hostfs(void)
1010 return register_filesystem(&hostfs_type);
1013 static void __exit exit_hostfs(void)
1015 unregister_filesystem(&hostfs_type);
1018 module_init(init_hostfs)
1019 module_exit(exit_hostfs)
1020 MODULE_LICENSE("GPL");