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