This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / hostfs / hostfs_kern.c
1 /* 
2  * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.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/stddef.h>
10 #include <linux/fs.h>
11 #include <linux/version.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/pagemap.h>
16 #include <linux/blkdev.h>
17 #include <linux/list.h>
18 #include <linux/buffer_head.h>
19 #include <linux/root_dev.h>
20 #include <linux/statfs.h>
21 #include <asm/uaccess.h>
22 #include "hostfs.h"
23 #include "kern_util.h"
24 #include "kern.h"
25 #include "user_util.h"
26 #include "2_5compat.h"
27 #include "init.h"
28
29 struct hostfs_inode_info {
30         char *host_filename;
31         int fd;
32         int mode;
33         struct inode vfs_inode;
34 };
35
36 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
37 {
38         return(list_entry(inode, struct hostfs_inode_info, vfs_inode));
39 }
40
41 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_dentry->d_inode)
42
43 int hostfs_d_delete(struct dentry *dentry)
44 {
45         return(1);
46 }
47
48 struct dentry_operations hostfs_dentry_ops = {
49         .d_delete               = hostfs_d_delete,
50 };
51
52 /* Changed in hostfs_args before the kernel starts running */
53 static char *root_ino = "/";
54 static int append = 0;
55
56 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
57
58 static struct inode_operations hostfs_iops;
59 static struct inode_operations hostfs_dir_iops;
60 static struct address_space_operations hostfs_link_aops;
61
62 static int __init hostfs_args(char *options, int *add)
63 {
64         char *ptr;
65
66         ptr = strchr(options, ',');
67         if(ptr != NULL)
68                 *ptr++ = '\0';
69         if(*options != '\0')
70                 root_ino = options;
71
72         options = ptr;
73         while(options){
74                 ptr = strchr(options, ',');
75                 if(ptr != NULL)
76                         *ptr++ = '\0';
77                 if(*options != '\0'){
78                         if(!strcmp(options, "append"))
79                                 append = 1;
80                         else printf("hostfs_args - unsupported option - %s\n",
81                                     options);
82                 }
83                 options = ptr;
84         }
85         return(0);
86 }
87
88 __uml_setup("hostfs=", hostfs_args,
89 "hostfs=<root dir>,<flags>,...\n"
90 "    This is used to set hostfs parameters.  The root directory argument\n"
91 "    is used to confine all hostfs mounts to within the specified directory\n"
92 "    tree on the host.  If this isn't specified, then a user inside UML can\n"
93 "    mount anything on the host that's accessible to the user that's running\n"
94 "    it.\n"
95 "    The only flag currently supported is 'append', which specifies that all\n"
96 "    files opened by hostfs will be opened in append mode.\n\n"
97 );
98
99 static char *dentry_name(struct dentry *dentry, int extra)
100 {
101         struct dentry *parent;
102         char *root, *name;
103         int len;
104
105         len = 0;
106         parent = dentry;
107         while(parent->d_parent != parent){
108                 len += parent->d_name.len + 1;
109                 parent = parent->d_parent;
110         }
111         
112         root = HOSTFS_I(parent->d_inode)->host_filename;
113         len += strlen(root);
114         name = kmalloc(len + extra + 1, GFP_KERNEL);
115         if(name == NULL) return(NULL);
116
117         name[len] = '\0';
118         parent = dentry;
119         while(parent->d_parent != parent){
120                 len -= parent->d_name.len + 1;
121                 name[len] = '/';
122                 strncpy(&name[len + 1], parent->d_name.name, 
123                         parent->d_name.len);
124                 parent = parent->d_parent;
125         }
126         strncpy(name, root, strlen(root));
127         return(name);
128 }
129
130 static char *inode_name(struct inode *ino, int extra)
131 {
132         struct dentry *dentry;
133
134         dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
135         return(dentry_name(dentry, extra));
136 }
137
138 static int read_name(struct inode *ino, char *name)
139 {
140         /* The non-int inode fields are copied into ints by stat_file and
141          * then copied into the inode because passing the actual pointers
142          * in and having them treated as int * breaks on big-endian machines
143          */
144         int err;
145         int i_mode, i_nlink, i_blksize;
146         unsigned long long i_size;
147         unsigned long long i_ino;
148         unsigned long long i_blocks;
149
150         err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid, 
151                         &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime, 
152                         &ino->i_ctime, &i_blksize, &i_blocks);
153         if(err) 
154                 return(err);
155
156         ino->i_ino = i_ino;
157         ino->i_mode = i_mode;
158         ino->i_nlink = i_nlink;
159         ino->i_size = i_size;
160         ino->i_blksize = i_blksize;
161         ino->i_blocks = i_blocks;
162         if((ino->i_sb->s_dev == ROOT_DEV) && (ino->i_uid == getuid()))
163                 ino->i_uid = 0;
164         return(0);
165 }
166
167 static char *follow_link(char *link)
168 {
169         int len, n;
170         char *name, *resolved, *end;
171
172         len = 64;
173         while(1){
174                 n = -ENOMEM;
175                 name = kmalloc(len, GFP_KERNEL);
176                 if(name == NULL)
177                         goto out;
178
179                 n = do_readlink(link, name, len);
180                 if(n < len)
181                         break;
182                 len *= 2;
183                 kfree(name);
184         }
185         if(n < 0)
186                 goto out_free;
187
188         if(*name == '/')
189                 return(name);
190
191         end = strrchr(link, '/');
192         if(end == NULL)
193                 return(name);
194
195         *(end + 1) = '\0';
196         len = strlen(link) + strlen(name) + 1;
197
198         resolved = kmalloc(len, GFP_KERNEL);
199         if(resolved == NULL){
200                 n = -ENOMEM;
201                 goto out_free;
202         }
203
204         sprintf(resolved, "%s%s", link, name);
205         kfree(name);
206         kfree(link);
207         return(resolved);
208
209  out_free:
210         kfree(name);
211  out:
212         return(ERR_PTR(n));
213 }
214
215 static int read_inode(struct inode *ino)
216 {
217         char *name;
218         int err = 0;
219
220         /* Unfortunately, we are called from iget() when we don't have a dentry
221          * allocated yet.
222          */
223         if(list_empty(&ino->i_dentry))
224                 goto out;
225  
226         err = -ENOMEM;
227         name = inode_name(ino, 0);
228         if(name == NULL) 
229                 goto out;
230
231         if(file_type(name, NULL) == OS_TYPE_SYMLINK){
232                 name = follow_link(name);
233                 if(IS_ERR(name)){
234                         err = PTR_ERR(name);
235                         goto out;
236                 }
237         }
238         
239         err = read_name(ino, name);
240         kfree(name);
241  out:
242         return(err);
243 }
244
245 int hostfs_statfs(struct super_block *sb, struct kstatfs *sf)
246 {
247         /* do_statfs uses struct statfs64 internally, but the linux kernel
248          * struct statfs still has 32-bit versions for most of these fields,
249          * so we convert them here
250          */
251         int err;
252         long long f_blocks;
253         long long f_bfree;
254         long long f_bavail;
255         long long f_files;
256         long long f_ffree;
257
258         err = do_statfs(HOSTFS_I(sb->s_root->d_inode)->host_filename,
259                         &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
260                         &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), 
261                         &sf->f_namelen, sf->f_spare);
262         if(err) return(err);
263         sf->f_blocks = f_blocks;
264         sf->f_bfree = f_bfree;
265         sf->f_bavail = f_bavail;
266         sf->f_files = f_files;
267         sf->f_ffree = f_ffree;
268         sf->f_type = HOSTFS_SUPER_MAGIC;
269         return(0);
270 }
271
272 static struct inode *hostfs_alloc_inode(struct super_block *sb)
273 {
274         struct hostfs_inode_info *hi;
275
276         hi = kmalloc(sizeof(*hi), GFP_KERNEL);
277         if(hi == NULL) 
278                 return(NULL);
279
280         *hi = ((struct hostfs_inode_info) { .host_filename      = NULL,
281                                             .fd                 = -1,
282                                             .mode               = 0 });
283         inode_init_once(&hi->vfs_inode);
284         return(&hi->vfs_inode);
285 }
286
287 static void hostfs_destroy_inode(struct inode *inode)
288 {
289         if(HOSTFS_I(inode)->host_filename) 
290                 kfree(HOSTFS_I(inode)->host_filename);
291
292         if(HOSTFS_I(inode)->fd != -1) 
293                 close_file(&HOSTFS_I(inode)->fd);
294
295         kfree(HOSTFS_I(inode));
296 }
297
298 static void hostfs_read_inode(struct inode *inode)
299 {
300         read_inode(inode);
301 }
302
303 static struct super_operations hostfs_sbops = { 
304         .alloc_inode    = hostfs_alloc_inode,
305         .destroy_inode  = hostfs_destroy_inode,
306         .read_inode     = hostfs_read_inode,
307         .statfs         = hostfs_statfs,
308 };
309
310 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
311 {
312         void *dir;
313         char *name;
314         unsigned long long next, ino;
315         int error, len;
316
317         name = dentry_name(file->f_dentry, 0);
318         if(name == NULL) return(-ENOMEM);
319         dir = open_dir(name, &error);
320         kfree(name);
321         if(dir == NULL) return(-error);
322         next = file->f_pos;
323         while((name = read_dir(dir, &next, &ino, &len)) != NULL){
324                 error = (*filldir)(ent, name, len, file->f_pos, 
325                                    ino, DT_UNKNOWN);
326                 if(error) break;
327                 file->f_pos = next;
328         }
329         close_dir(dir);
330         return(0);
331 }
332
333 int hostfs_file_open(struct inode *ino, struct file *file)
334 {
335         char *name;
336         int mode = 0, r = 0, w = 0, fd;
337
338         mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
339         if((mode & HOSTFS_I(ino)->mode) == mode)
340                 return(0);
341
342         /* The file may already have been opened, but with the wrong access,
343          * so this resets things and reopens the file with the new access.
344          */
345         if(HOSTFS_I(ino)->fd != -1){
346                 close_file(&HOSTFS_I(ino)->fd);
347                 HOSTFS_I(ino)->fd = -1;
348         }
349
350         HOSTFS_I(ino)->mode |= mode;
351         if(HOSTFS_I(ino)->mode & FMODE_READ) 
352                 r = 1;
353         if(HOSTFS_I(ino)->mode & FMODE_WRITE) 
354                 w = 1;
355         if(w) 
356                 r = 1;
357
358         name = dentry_name(file->f_dentry, 0);
359         if(name == NULL) 
360                 return(-ENOMEM);
361
362         fd = open_file(name, r, w, append);
363         kfree(name);
364         if(fd < 0) return(fd);
365         FILE_HOSTFS_I(file)->fd = fd;
366
367         return(0);
368 }
369
370 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
371 {
372         return(0);
373 }
374
375 static struct file_operations hostfs_file_fops = {
376         .llseek         = generic_file_llseek,
377         .read           = generic_file_read,
378         .write          = generic_file_write,
379         .mmap           = generic_file_mmap,
380         .open           = hostfs_file_open,
381         .release        = NULL,
382         .fsync          = hostfs_fsync,
383 };
384
385 static struct file_operations hostfs_dir_fops = {
386         .readdir        = hostfs_readdir,
387         .read           = generic_read_dir,
388 };
389
390 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
391 {
392         struct address_space *mapping = page->mapping;
393         struct inode *inode = mapping->host;
394         char *buffer;
395         unsigned long long base;
396         int count = PAGE_CACHE_SIZE;
397         int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
398         int err;
399
400         if (page->index >= end_index)
401                 count = inode->i_size & (PAGE_CACHE_SIZE-1);
402
403         buffer = kmap(page);
404         base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
405
406         err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
407         if(err != count){
408                 ClearPageUptodate(page);
409                 goto out;
410         }
411
412         if (base > inode->i_size)
413                 inode->i_size = base;
414
415         if (PageError(page))
416                 ClearPageError(page);   
417         err = 0;
418
419  out:   
420         kunmap(page);
421
422         unlock_page(page);
423         return err; 
424 }
425
426 int hostfs_readpage(struct file *file, struct page *page)
427 {
428         char *buffer;
429         long long start;
430         int err = 0;
431
432         start = (long long) page->index << PAGE_CACHE_SHIFT;
433         buffer = kmap(page);
434         err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
435                         PAGE_CACHE_SIZE);
436         if(err < 0) goto out;
437
438         memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
439
440         flush_dcache_page(page);
441         SetPageUptodate(page);
442         if (PageError(page)) ClearPageError(page);
443         err = 0;
444  out:
445         kunmap(page);
446         unlock_page(page);
447         return(err);
448 }
449
450 int hostfs_prepare_write(struct file *file, struct page *page, 
451                          unsigned int from, unsigned int to)
452 {
453         char *buffer;
454         long long start, tmp;
455         int err;
456
457         start = (long long) page->index << PAGE_CACHE_SHIFT;
458         buffer = kmap(page);
459         if(from != 0){
460                 tmp = start;
461                 err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
462                                 from);
463                 if(err < 0) goto out;
464         }
465         if(to != PAGE_CACHE_SIZE){
466                 start += to;
467                 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
468                                 PAGE_CACHE_SIZE - to);
469                 if(err < 0) goto out;           
470         }
471         err = 0;
472  out:
473         kunmap(page);
474         return(err);
475 }
476
477 int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
478                  unsigned to)
479 {
480         struct address_space *mapping = page->mapping;
481         struct inode *inode = mapping->host;
482         char *buffer;
483         long long start;
484         int err = 0;
485
486         start = (long long) (page->index << PAGE_CACHE_SHIFT) + from;
487         buffer = kmap(page);
488         err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from, 
489                          to - from);
490         if(err > 0) err = 0;
491         if(!err && (start > inode->i_size))
492                 inode->i_size = start;
493
494         kunmap(page);
495         return(err);
496 }
497
498 static struct address_space_operations hostfs_aops = {
499         .writepage      = hostfs_writepage,
500         .readpage       = hostfs_readpage,
501 /*      .set_page_dirty = __set_page_dirty_nobuffers, */
502         .prepare_write  = hostfs_prepare_write,
503         .commit_write   = hostfs_commit_write
504 };
505
506 static int init_inode(struct inode *inode, struct dentry *dentry)
507 {
508         char *name;
509         int type, err = -ENOMEM, rdev;
510
511         if(dentry){
512                 name = dentry_name(dentry, 0);
513                 if(name == NULL)
514                         goto out;
515                 type = file_type(name, &rdev);
516                 kfree(name);
517         }
518         else type = OS_TYPE_DIR;
519
520         err = 0;
521         if(type == OS_TYPE_SYMLINK)
522                 inode->i_op = &page_symlink_inode_operations;
523         else if(type == OS_TYPE_DIR)
524                 inode->i_op = &hostfs_dir_iops;
525         else inode->i_op = &hostfs_iops;
526
527         if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
528         else inode->i_fop = &hostfs_file_fops;
529
530         if(type == OS_TYPE_SYMLINK) 
531                 inode->i_mapping->a_ops = &hostfs_link_aops;
532         else inode->i_mapping->a_ops = &hostfs_aops;
533
534         switch (type) {
535         case OS_TYPE_CHARDEV:
536                 init_special_inode(inode, S_IFCHR, rdev);
537                 break;
538         case OS_TYPE_BLOCKDEV:
539                 init_special_inode(inode, S_IFBLK, rdev);
540                 break;
541         case OS_TYPE_FIFO:
542                 init_special_inode(inode, S_IFIFO, 0);
543                 break;
544         case OS_TYPE_SOCK:
545                 init_special_inode(inode, S_IFSOCK, 0);
546                 break;
547         }
548  out:
549         return(err);
550 }
551
552 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, 
553                  struct nameidata *nd)
554 {
555         struct inode *inode;
556         char *name;
557         int error, fd;
558
559         error = -ENOMEM;
560         inode = iget(dir->i_sb, 0);
561         if(inode == NULL) goto out;
562
563         error = init_inode(inode, dentry);
564         if(error) 
565                 goto out_put;
566         
567         error = -ENOMEM;
568         name = dentry_name(dentry, 0);
569         if(name == NULL)
570                 goto out_put;
571
572         fd = file_create(name, 
573                          mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR, 
574                          mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP, 
575                          mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
576         if(fd < 0) 
577                 error = fd;
578         else error = read_name(inode, name);
579
580         kfree(name);
581         if(error)
582                 goto out_put;
583
584         HOSTFS_I(inode)->fd = fd;
585         HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
586         d_instantiate(dentry, inode);
587         return(0);
588
589  out_put:
590         iput(inode);
591  out:
592         return(error);
593 }
594
595 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, 
596                             struct nameidata *nd)
597 {
598         struct inode *inode;
599         char *name;
600         int err;
601
602         err = -ENOMEM;
603         inode = iget(ino->i_sb, 0);
604         if(inode == NULL) 
605                 goto out;
606  
607         err = init_inode(inode, dentry);
608         if(err) 
609                 goto out_put;
610
611         err = -ENOMEM;
612         name = dentry_name(dentry, 0);
613         if(name == NULL)
614                 goto out_put;
615
616         err = read_name(inode, name);
617         kfree(name);
618         if(err == -ENOENT){
619                 iput(inode);
620                 inode = NULL;
621         }
622         else if(err)
623                 goto out_put;
624
625         d_add(dentry, inode);
626         dentry->d_op = &hostfs_dentry_ops;
627         return(NULL);
628
629  out_put:
630         iput(inode);
631  out:
632         return(ERR_PTR(err));
633 }
634
635 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
636 {
637         char *file;
638         int len;
639
640         file = inode_name(ino, dentry->d_name.len + 1);
641         if(file == NULL) return(NULL);
642         strcat(file, "/");
643         len = strlen(file);
644         strncat(file, dentry->d_name.name, dentry->d_name.len);
645         file[len + dentry->d_name.len] = '\0';
646         return(file);
647 }
648
649 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
650 {
651         char *from_name, *to_name;
652         int err;
653
654         if((from_name = inode_dentry_name(ino, from)) == NULL) 
655                 return(-ENOMEM);
656         to_name = dentry_name(to, 0);
657         if(to_name == NULL){
658                 kfree(from_name);
659                 return(-ENOMEM);
660         }
661         err = link_file(to_name, from_name);
662         kfree(from_name);
663         kfree(to_name);
664         return(err);
665 }
666
667 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
668 {
669         char *file;
670         int err;
671
672         if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
673         if(append)
674                 return(-EPERM);
675
676         err = unlink_file(file);
677         kfree(file);
678         return(err);
679 }
680
681 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
682 {
683         char *file;
684         int err;
685
686         if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
687         err = make_symlink(file, to);
688         kfree(file);
689         return(err);
690 }
691
692 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
693 {
694         char *file;
695         int err;
696
697         if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
698         err = do_mkdir(file, mode);
699         kfree(file);
700         return(err);
701 }
702
703 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
704 {
705         char *file;
706         int err;
707
708         if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
709         err = do_rmdir(file);
710         kfree(file);
711         return(err);
712 }
713
714 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
715 {
716         struct inode *inode;
717         char *name;
718         int err = -ENOMEM;
719  
720         inode = iget(dir->i_sb, 0);
721         if(inode == NULL) 
722                 goto out;
723
724         err = init_inode(inode, dentry);
725         if(err) 
726                 goto out_put;
727
728         err = -ENOMEM;
729         name = dentry_name(dentry, 0);
730         if(name == NULL)
731                 goto out_put;
732
733         init_special_inode(inode, mode, dev);
734         err = do_mknod(name, mode, dev);
735         if(err)
736                 goto out_free;
737
738         err = read_name(inode, name);
739         kfree(name);
740         if(err)
741                 goto out_put;
742
743         d_instantiate(dentry, inode);
744         return(0);
745
746  out_free:
747         kfree(name);
748  out_put:
749         iput(inode);
750  out:
751         return(err);
752 }
753
754 int hostfs_rename(struct inode *from_ino, struct dentry *from,
755                   struct inode *to_ino, struct dentry *to)
756 {
757         char *from_name, *to_name;
758         int err;
759
760         if((from_name = inode_dentry_name(from_ino, from)) == NULL)
761                 return(-ENOMEM);
762         if((to_name = inode_dentry_name(to_ino, to)) == NULL){
763                 kfree(from_name);
764                 return(-ENOMEM);
765         }
766         err = rename_file(from_name, to_name);
767         kfree(from_name);
768         kfree(to_name);
769         return(err);
770 }
771
772 void hostfs_truncate(struct inode *ino)
773 {
774         not_implemented();
775 }
776
777 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
778 {
779         char *name;
780         int r = 0, w = 0, x = 0, err;
781
782         if(desired & MAY_READ) r = 1;
783         if(desired & MAY_WRITE) w = 1;
784         if(desired & MAY_EXEC) x = 1;
785         name = inode_name(ino, 0);
786         if(name == NULL) return(-ENOMEM);
787         err = access_file(name, r, w, x);
788         kfree(name);
789         if(!err) err = vfs_permission(ino, desired);
790         return(err);
791 }
792
793 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
794 {
795         struct hostfs_iattr attrs;
796         char *name;
797         int err;
798         
799         if(append) 
800                 attr->ia_valid &= ~ATTR_SIZE;
801
802         attrs.ia_valid = 0;
803         if(attr->ia_valid & ATTR_MODE){
804                 attrs.ia_valid |= HOSTFS_ATTR_MODE;
805                 attrs.ia_mode = attr->ia_mode;
806         }
807         if(attr->ia_valid & ATTR_UID){
808                 if((dentry->d_inode->i_sb->s_dev == ROOT_DEV) && 
809                    (attr->ia_uid == 0))
810                         attr->ia_uid = getuid();
811                 attrs.ia_valid |= HOSTFS_ATTR_UID;
812                 attrs.ia_uid = attr->ia_uid;
813         }
814         if(attr->ia_valid & ATTR_GID){
815                 if((dentry->d_inode->i_sb->s_dev == ROOT_DEV) && 
816                    (attr->ia_gid == 0))
817                         attr->ia_gid = getuid();
818                 attrs.ia_valid |= HOSTFS_ATTR_GID;
819                 attrs.ia_gid = attr->ia_gid;
820         }
821         if(attr->ia_valid & ATTR_SIZE){
822                 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
823                 attrs.ia_size = attr->ia_size;
824         }
825         if(attr->ia_valid & ATTR_ATIME){
826                 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
827                 attrs.ia_atime = attr->ia_atime;
828         }
829         if(attr->ia_valid & ATTR_MTIME){
830                 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
831                 attrs.ia_mtime = attr->ia_mtime;
832         }
833         if(attr->ia_valid & ATTR_CTIME){
834                 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
835                 attrs.ia_ctime = attr->ia_ctime;
836         }
837         if(attr->ia_valid & ATTR_ATIME_SET){
838                 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
839         }
840         if(attr->ia_valid & ATTR_MTIME_SET){
841                 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
842         }
843         name = dentry_name(dentry, 0);
844         if(name == NULL) return(-ENOMEM);
845         err = set_attr(name, &attrs);
846         kfree(name);
847         if(err)
848                 return(err);
849
850         return(inode_setattr(dentry->d_inode, attr));
851 }
852
853 int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry, 
854            struct kstat *stat)
855 {
856         generic_fillattr(dentry->d_inode, stat);
857         return(0);
858 }
859
860 static struct inode_operations hostfs_iops = {
861         .create         = hostfs_create,
862         .link           = hostfs_link,
863         .unlink         = hostfs_unlink,
864         .symlink        = hostfs_symlink,
865         .mkdir          = hostfs_mkdir,
866         .rmdir          = hostfs_rmdir,
867         .mknod          = hostfs_mknod,
868         .rename         = hostfs_rename,
869         .truncate       = hostfs_truncate,
870         .permission     = hostfs_permission,
871         .setattr        = hostfs_setattr,
872         .getattr        = hostfs_getattr,
873 };
874
875 static struct inode_operations hostfs_dir_iops = {
876         .create         = hostfs_create,
877         .lookup         = hostfs_lookup,
878         .link           = hostfs_link,
879         .unlink         = hostfs_unlink,
880         .symlink        = hostfs_symlink,
881         .mkdir          = hostfs_mkdir,
882         .rmdir          = hostfs_rmdir,
883         .mknod          = hostfs_mknod,
884         .rename         = hostfs_rename,
885         .truncate       = hostfs_truncate,
886         .permission     = hostfs_permission,
887         .setattr        = hostfs_setattr,
888         .getattr        = hostfs_getattr,
889 };
890
891 int hostfs_link_readpage(struct file *file, struct page *page)
892 {
893         char *buffer, *name;
894         long long start;
895         int err;
896
897         start = page->index << PAGE_CACHE_SHIFT;
898         buffer = kmap(page);
899         name = inode_name(page->mapping->host, 0);
900         if(name == NULL) return(-ENOMEM);
901         err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
902         kfree(name);
903         if(err == PAGE_CACHE_SIZE)
904                 err = -E2BIG;
905         else if(err > 0){
906                 flush_dcache_page(page);
907                 SetPageUptodate(page);
908                 if (PageError(page)) ClearPageError(page);
909                 err = 0;
910         }
911         kunmap(page);
912         unlock_page(page);
913         return(err);
914 }
915
916 static struct address_space_operations hostfs_link_aops = {
917         .readpage       = hostfs_link_readpage,
918 };
919
920 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
921 {
922         struct inode *root_inode;
923         char *name, *data = d;
924         int err;
925
926         sb->s_blocksize = 1024;
927         sb->s_blocksize_bits = 10;
928         sb->s_magic = HOSTFS_SUPER_MAGIC;
929         sb->s_op = &hostfs_sbops;
930
931         if((data == NULL) || (*data == '\0')) 
932                 data = root_ino;
933
934         err = -ENOMEM;
935         name = kmalloc(strlen(data) + 1, GFP_KERNEL);
936         if(name == NULL) 
937                 goto out;
938
939         strcpy(name, data);
940
941         root_inode = iget(sb, 0);
942         if(root_inode == NULL)
943                 goto out_free;
944
945         err = init_inode(root_inode, NULL);
946         if(err)
947                 goto out_put;
948
949         HOSTFS_I(root_inode)->host_filename = name;
950
951         err = -ENOMEM;
952         sb->s_root = d_alloc_root(root_inode);
953         if(sb->s_root == NULL)
954                 goto out_put;
955
956         err = read_inode(root_inode);
957         if(err)
958                 goto out_put;
959
960         return(0);
961
962  out_put:
963         iput(root_inode);
964  out_free:
965         kfree(name);
966  out:
967         return(err);
968 }
969
970 static struct super_block *hostfs_read_sb(struct file_system_type *type,
971                                              int flags, const char *dev_name,
972                                              void *data)
973 {
974         return(get_sb_nodev(type, flags, data, hostfs_fill_sb_common));
975 }
976
977 static struct file_system_type hostfs_type = {
978         .owner          = THIS_MODULE,
979         .name           = "hostfs",
980         .get_sb         = hostfs_read_sb,
981         .kill_sb        = kill_anon_super,
982         .fs_flags       = 0,
983 };
984
985 static int __init init_hostfs(void)
986 {
987         return(register_filesystem(&hostfs_type));
988 }
989
990 static void __exit exit_hostfs(void)
991 {
992         unregister_filesystem(&hostfs_type);
993 }
994
995 module_init(init_hostfs)
996 module_exit(exit_hostfs)
997 MODULE_LICENSE("GPL");
998
999 /*
1000  * Overrides for Emacs so that we follow Linus's tabbing style.
1001  * Emacs will notice this stuff at the end of the file and automatically
1002  * adjust the settings for this buffer only.  This must remain at the end
1003  * of the file.
1004  * ---------------------------------------------------------------------------
1005  * Local variables:
1006  * c-file-style: "linux"
1007  * End:
1008  */