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