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