[PATCH] Make most file operations structs in fs/ const
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / hppfs / hppfs_kern.c
blob2ba20cdb5baa7316e8f14738b895f3332c1b9a1e
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
6 #include <linux/fs.h>
7 #include <linux/file.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/kernel.h>
13 #include <linux/ctype.h>
14 #include <linux/dcache.h>
15 #include <linux/statfs.h>
16 #include <asm/uaccess.h>
17 #include <asm/fcntl.h>
18 #include "os.h"
20 static int init_inode(struct inode *inode, struct dentry *dentry);
22 struct hppfs_data {
23 struct list_head list;
24 char contents[PAGE_SIZE - sizeof(struct list_head)];
27 struct hppfs_private {
28 struct file *proc_file;
29 int host_fd;
30 loff_t len;
31 struct hppfs_data *contents;
34 struct hppfs_inode_info {
35 struct dentry *proc_dentry;
36 struct inode vfs_inode;
39 static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
41 return container_of(inode, struct hppfs_inode_info, vfs_inode);
44 #define HPPFS_SUPER_MAGIC 0xb00000ee
46 static struct super_operations hppfs_sbops;
48 static int is_pid(struct dentry *dentry)
50 struct super_block *sb;
51 int i;
53 sb = dentry->d_sb;
54 if((sb->s_op != &hppfs_sbops) || (dentry->d_parent != sb->s_root))
55 return(0);
57 for(i = 0; i < dentry->d_name.len; i++){
58 if(!isdigit(dentry->d_name.name[i]))
59 return(0);
61 return(1);
64 static char *dentry_name(struct dentry *dentry, int extra)
66 struct dentry *parent;
67 char *root, *name;
68 const char *seg_name;
69 int len, seg_len;
71 len = 0;
72 parent = dentry;
73 while(parent->d_parent != parent){
74 if(is_pid(parent))
75 len += strlen("pid") + 1;
76 else len += parent->d_name.len + 1;
77 parent = parent->d_parent;
80 root = "proc";
81 len += strlen(root);
82 name = kmalloc(len + extra + 1, GFP_KERNEL);
83 if(name == NULL) return(NULL);
85 name[len] = '\0';
86 parent = dentry;
87 while(parent->d_parent != parent){
88 if(is_pid(parent)){
89 seg_name = "pid";
90 seg_len = strlen("pid");
92 else {
93 seg_name = parent->d_name.name;
94 seg_len = parent->d_name.len;
97 len -= seg_len + 1;
98 name[len] = '/';
99 strncpy(&name[len + 1], seg_name, seg_len);
100 parent = parent->d_parent;
102 strncpy(name, root, strlen(root));
103 return(name);
106 struct dentry_operations hppfs_dentry_ops = {
109 static int file_removed(struct dentry *dentry, const char *file)
111 char *host_file;
112 int extra, fd;
114 extra = 0;
115 if(file != NULL) extra += strlen(file) + 1;
117 host_file = dentry_name(dentry, extra + strlen("/remove"));
118 if(host_file == NULL){
119 printk("file_removed : allocation failed\n");
120 return(-ENOMEM);
123 if(file != NULL){
124 strcat(host_file, "/");
125 strcat(host_file, file);
127 strcat(host_file, "/remove");
129 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
130 kfree(host_file);
131 if(fd > 0){
132 os_close_file(fd);
133 return(1);
135 return(0);
138 static void hppfs_read_inode(struct inode *ino)
140 struct inode *proc_ino;
142 if(HPPFS_I(ino)->proc_dentry == NULL)
143 return;
145 proc_ino = HPPFS_I(ino)->proc_dentry->d_inode;
146 ino->i_uid = proc_ino->i_uid;
147 ino->i_gid = proc_ino->i_gid;
148 ino->i_atime = proc_ino->i_atime;
149 ino->i_mtime = proc_ino->i_mtime;
150 ino->i_ctime = proc_ino->i_ctime;
151 ino->i_ino = proc_ino->i_ino;
152 ino->i_mode = proc_ino->i_mode;
153 ino->i_nlink = proc_ino->i_nlink;
154 ino->i_size = proc_ino->i_size;
155 ino->i_blksize = proc_ino->i_blksize;
156 ino->i_blocks = proc_ino->i_blocks;
159 static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
160 struct nameidata *nd)
162 struct dentry *proc_dentry, *new, *parent;
163 struct inode *inode;
164 int err, deleted;
166 deleted = file_removed(dentry, NULL);
167 if(deleted < 0)
168 return(ERR_PTR(deleted));
169 else if(deleted)
170 return(ERR_PTR(-ENOENT));
172 err = -ENOMEM;
173 parent = HPPFS_I(ino)->proc_dentry;
174 mutex_lock(&parent->d_inode->i_mutex);
175 proc_dentry = d_lookup(parent, &dentry->d_name);
176 if(proc_dentry == NULL){
177 proc_dentry = d_alloc(parent, &dentry->d_name);
178 if(proc_dentry == NULL){
179 mutex_unlock(&parent->d_inode->i_mutex);
180 goto out;
182 new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
183 proc_dentry, NULL);
184 if(new){
185 dput(proc_dentry);
186 proc_dentry = new;
189 mutex_unlock(&parent->d_inode->i_mutex);
191 if(IS_ERR(proc_dentry))
192 return(proc_dentry);
194 inode = iget(ino->i_sb, 0);
195 if(inode == NULL)
196 goto out_dput;
198 err = init_inode(inode, proc_dentry);
199 if(err)
200 goto out_put;
202 hppfs_read_inode(inode);
204 d_add(dentry, inode);
205 dentry->d_op = &hppfs_dentry_ops;
206 return(NULL);
208 out_put:
209 iput(inode);
210 out_dput:
211 dput(proc_dentry);
212 out:
213 return(ERR_PTR(err));
216 static struct inode_operations hppfs_file_iops = {
219 static ssize_t read_proc(struct file *file, char *buf, ssize_t count,
220 loff_t *ppos, int is_user)
222 ssize_t (*read)(struct file *, char *, size_t, loff_t *);
223 ssize_t n;
225 read = file->f_dentry->d_inode->i_fop->read;
227 if(!is_user)
228 set_fs(KERNEL_DS);
230 n = (*read)(file, buf, count, &file->f_pos);
232 if(!is_user)
233 set_fs(USER_DS);
235 if(ppos) *ppos = file->f_pos;
236 return n;
239 static ssize_t hppfs_read_file(int fd, char *buf, ssize_t count)
241 ssize_t n;
242 int cur, err;
243 char *new_buf;
245 n = -ENOMEM;
246 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
247 if(new_buf == NULL){
248 printk("hppfs_read_file : kmalloc failed\n");
249 goto out;
251 n = 0;
252 while(count > 0){
253 cur = min_t(ssize_t, count, PAGE_SIZE);
254 err = os_read_file(fd, new_buf, cur);
255 if(err < 0){
256 printk("hppfs_read : read failed, errno = %d\n",
257 err);
258 n = err;
259 goto out_free;
261 else if(err == 0)
262 break;
264 if(copy_to_user(buf, new_buf, err)){
265 n = -EFAULT;
266 goto out_free;
268 n += err;
269 count -= err;
271 out_free:
272 kfree(new_buf);
273 out:
274 return n;
277 static ssize_t hppfs_read(struct file *file, char *buf, size_t count,
278 loff_t *ppos)
280 struct hppfs_private *hppfs = file->private_data;
281 struct hppfs_data *data;
282 loff_t off;
283 int err;
285 if(hppfs->contents != NULL){
286 if(*ppos >= hppfs->len) return(0);
288 data = hppfs->contents;
289 off = *ppos;
290 while(off >= sizeof(data->contents)){
291 data = list_entry(data->list.next, struct hppfs_data,
292 list);
293 off -= sizeof(data->contents);
296 if(off + count > hppfs->len)
297 count = hppfs->len - off;
298 copy_to_user(buf, &data->contents[off], count);
299 *ppos += count;
301 else if(hppfs->host_fd != -1){
302 err = os_seek_file(hppfs->host_fd, *ppos);
303 if(err){
304 printk("hppfs_read : seek failed, errno = %d\n", err);
305 return(err);
307 count = hppfs_read_file(hppfs->host_fd, buf, count);
308 if(count > 0)
309 *ppos += count;
311 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
313 return(count);
316 static ssize_t hppfs_write(struct file *file, const char *buf, size_t len,
317 loff_t *ppos)
319 struct hppfs_private *data = file->private_data;
320 struct file *proc_file = data->proc_file;
321 ssize_t (*write)(struct file *, const char *, size_t, loff_t *);
322 int err;
324 write = proc_file->f_dentry->d_inode->i_fop->write;
326 proc_file->f_pos = file->f_pos;
327 err = (*write)(proc_file, buf, len, &proc_file->f_pos);
328 file->f_pos = proc_file->f_pos;
330 return(err);
333 static int open_host_sock(char *host_file, int *filter_out)
335 char *end;
336 int fd;
338 end = &host_file[strlen(host_file)];
339 strcpy(end, "/rw");
340 *filter_out = 1;
341 fd = os_connect_socket(host_file);
342 if(fd > 0)
343 return(fd);
345 strcpy(end, "/r");
346 *filter_out = 0;
347 fd = os_connect_socket(host_file);
348 return(fd);
351 static void free_contents(struct hppfs_data *head)
353 struct hppfs_data *data;
354 struct list_head *ele, *next;
356 if(head == NULL) return;
358 list_for_each_safe(ele, next, &head->list){
359 data = list_entry(ele, struct hppfs_data, list);
360 kfree(data);
362 kfree(head);
365 static struct hppfs_data *hppfs_get_data(int fd, int filter,
366 struct file *proc_file,
367 struct file *hppfs_file,
368 loff_t *size_out)
370 struct hppfs_data *data, *new, *head;
371 int n, err;
373 err = -ENOMEM;
374 data = kmalloc(sizeof(*data), GFP_KERNEL);
375 if(data == NULL){
376 printk("hppfs_get_data : head allocation failed\n");
377 goto failed;
380 INIT_LIST_HEAD(&data->list);
382 head = data;
383 *size_out = 0;
385 if(filter){
386 while((n = read_proc(proc_file, data->contents,
387 sizeof(data->contents), NULL, 0)) > 0)
388 os_write_file(fd, data->contents, n);
389 err = os_shutdown_socket(fd, 0, 1);
390 if(err){
391 printk("hppfs_get_data : failed to shut down "
392 "socket\n");
393 goto failed_free;
396 while(1){
397 n = os_read_file(fd, data->contents, sizeof(data->contents));
398 if(n < 0){
399 err = n;
400 printk("hppfs_get_data : read failed, errno = %d\n",
401 err);
402 goto failed_free;
404 else if(n == 0)
405 break;
407 *size_out += n;
409 if(n < sizeof(data->contents))
410 break;
412 new = kmalloc(sizeof(*data), GFP_KERNEL);
413 if(new == 0){
414 printk("hppfs_get_data : data allocation failed\n");
415 err = -ENOMEM;
416 goto failed_free;
419 INIT_LIST_HEAD(&new->list);
420 list_add(&new->list, &data->list);
421 data = new;
423 return(head);
425 failed_free:
426 free_contents(head);
427 failed:
428 return(ERR_PTR(err));
431 static struct hppfs_private *hppfs_data(void)
433 struct hppfs_private *data;
435 data = kmalloc(sizeof(*data), GFP_KERNEL);
436 if(data == NULL)
437 return(data);
439 *data = ((struct hppfs_private ) { .host_fd = -1,
440 .len = -1,
441 .contents = NULL } );
442 return(data);
445 static int file_mode(int fmode)
447 if(fmode == (FMODE_READ | FMODE_WRITE))
448 return(O_RDWR);
449 if(fmode == FMODE_READ)
450 return(O_RDONLY);
451 if(fmode == FMODE_WRITE)
452 return(O_WRONLY);
453 return(0);
456 static int hppfs_open(struct inode *inode, struct file *file)
458 struct hppfs_private *data;
459 struct dentry *proc_dentry;
460 char *host_file;
461 int err, fd, type, filter;
463 err = -ENOMEM;
464 data = hppfs_data();
465 if(data == NULL)
466 goto out;
468 host_file = dentry_name(file->f_dentry, strlen("/rw"));
469 if(host_file == NULL)
470 goto out_free2;
472 proc_dentry = HPPFS_I(inode)->proc_dentry;
474 /* XXX This isn't closed anywhere */
475 data->proc_file = dentry_open(dget(proc_dentry), NULL,
476 file_mode(file->f_mode));
477 err = PTR_ERR(data->proc_file);
478 if(IS_ERR(data->proc_file))
479 goto out_free1;
481 type = os_file_type(host_file);
482 if(type == OS_TYPE_FILE){
483 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
484 if(fd >= 0)
485 data->host_fd = fd;
486 else printk("hppfs_open : failed to open '%s', errno = %d\n",
487 host_file, -fd);
489 data->contents = NULL;
491 else if(type == OS_TYPE_DIR){
492 fd = open_host_sock(host_file, &filter);
493 if(fd > 0){
494 data->contents = hppfs_get_data(fd, filter,
495 data->proc_file,
496 file, &data->len);
497 if(!IS_ERR(data->contents))
498 data->host_fd = fd;
500 else printk("hppfs_open : failed to open a socket in "
501 "'%s', errno = %d\n", host_file, -fd);
503 kfree(host_file);
505 file->private_data = data;
506 return(0);
508 out_free1:
509 kfree(host_file);
510 out_free2:
511 free_contents(data->contents);
512 kfree(data);
513 out:
514 return(err);
517 static int hppfs_dir_open(struct inode *inode, struct file *file)
519 struct hppfs_private *data;
520 struct dentry *proc_dentry;
521 int err;
523 err = -ENOMEM;
524 data = hppfs_data();
525 if(data == NULL)
526 goto out;
528 proc_dentry = HPPFS_I(inode)->proc_dentry;
529 data->proc_file = dentry_open(dget(proc_dentry), NULL,
530 file_mode(file->f_mode));
531 err = PTR_ERR(data->proc_file);
532 if(IS_ERR(data->proc_file))
533 goto out_free;
535 file->private_data = data;
536 return(0);
538 out_free:
539 kfree(data);
540 out:
541 return(err);
544 static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
546 struct hppfs_private *data = file->private_data;
547 struct file *proc_file = data->proc_file;
548 loff_t (*llseek)(struct file *, loff_t, int);
549 loff_t ret;
551 llseek = proc_file->f_dentry->d_inode->i_fop->llseek;
552 if(llseek != NULL){
553 ret = (*llseek)(proc_file, off, where);
554 if(ret < 0)
555 return(ret);
558 return(default_llseek(file, off, where));
561 static const struct file_operations hppfs_file_fops = {
562 .owner = NULL,
563 .llseek = hppfs_llseek,
564 .read = hppfs_read,
565 .write = hppfs_write,
566 .open = hppfs_open,
569 struct hppfs_dirent {
570 void *vfs_dirent;
571 filldir_t filldir;
572 struct dentry *dentry;
575 static int hppfs_filldir(void *d, const char *name, int size,
576 loff_t offset, ino_t inode, unsigned int type)
578 struct hppfs_dirent *dirent = d;
580 if(file_removed(dirent->dentry, name))
581 return(0);
583 return((*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
584 inode, type));
587 static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
589 struct hppfs_private *data = file->private_data;
590 struct file *proc_file = data->proc_file;
591 int (*readdir)(struct file *, void *, filldir_t);
592 struct hppfs_dirent dirent = ((struct hppfs_dirent)
593 { .vfs_dirent = ent,
594 .filldir = filldir,
595 .dentry = file->f_dentry } );
596 int err;
598 readdir = proc_file->f_dentry->d_inode->i_fop->readdir;
600 proc_file->f_pos = file->f_pos;
601 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
602 file->f_pos = proc_file->f_pos;
604 return(err);
607 static int hppfs_fsync(struct file *file, struct dentry *dentry, int datasync)
609 return(0);
612 static const struct file_operations hppfs_dir_fops = {
613 .owner = NULL,
614 .readdir = hppfs_readdir,
615 .open = hppfs_dir_open,
616 .fsync = hppfs_fsync,
619 static int hppfs_statfs(struct super_block *sb, struct kstatfs *sf)
621 sf->f_blocks = 0;
622 sf->f_bfree = 0;
623 sf->f_bavail = 0;
624 sf->f_files = 0;
625 sf->f_ffree = 0;
626 sf->f_type = HPPFS_SUPER_MAGIC;
627 return(0);
630 static struct inode *hppfs_alloc_inode(struct super_block *sb)
632 struct hppfs_inode_info *hi;
634 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
635 if(hi == NULL)
636 return(NULL);
638 *hi = ((struct hppfs_inode_info) { .proc_dentry = NULL });
639 inode_init_once(&hi->vfs_inode);
640 return(&hi->vfs_inode);
643 void hppfs_delete_inode(struct inode *ino)
645 clear_inode(ino);
648 static void hppfs_destroy_inode(struct inode *inode)
650 kfree(HPPFS_I(inode));
653 static struct super_operations hppfs_sbops = {
654 .alloc_inode = hppfs_alloc_inode,
655 .destroy_inode = hppfs_destroy_inode,
656 .read_inode = hppfs_read_inode,
657 .delete_inode = hppfs_delete_inode,
658 .statfs = hppfs_statfs,
661 static int hppfs_readlink(struct dentry *dentry, char *buffer, int buflen)
663 struct file *proc_file;
664 struct dentry *proc_dentry;
665 int ret;
667 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
668 proc_file = dentry_open(dget(proc_dentry), NULL, O_RDONLY);
669 if (IS_ERR(proc_file))
670 return PTR_ERR(proc_file);
672 ret = proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer, buflen);
674 fput(proc_file);
676 return ret;
679 static void* hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
681 struct file *proc_file;
682 struct dentry *proc_dentry;
683 void *ret;
685 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
686 proc_file = dentry_open(dget(proc_dentry), NULL, O_RDONLY);
687 if (IS_ERR(proc_file))
688 return proc_file;
690 ret = proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
692 fput(proc_file);
694 return ret;
697 static struct inode_operations hppfs_dir_iops = {
698 .lookup = hppfs_lookup,
701 static struct inode_operations hppfs_link_iops = {
702 .readlink = hppfs_readlink,
703 .follow_link = hppfs_follow_link,
706 static int init_inode(struct inode *inode, struct dentry *dentry)
708 if(S_ISDIR(dentry->d_inode->i_mode)){
709 inode->i_op = &hppfs_dir_iops;
710 inode->i_fop = &hppfs_dir_fops;
712 else if(S_ISLNK(dentry->d_inode->i_mode)){
713 inode->i_op = &hppfs_link_iops;
714 inode->i_fop = &hppfs_file_fops;
716 else {
717 inode->i_op = &hppfs_file_iops;
718 inode->i_fop = &hppfs_file_fops;
721 HPPFS_I(inode)->proc_dentry = dentry;
723 return(0);
726 static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
728 struct inode *root_inode;
729 struct file_system_type *procfs;
730 struct super_block *proc_sb;
731 int err;
733 err = -ENOENT;
734 procfs = get_fs_type("proc");
735 if(procfs == NULL)
736 goto out;
738 if(list_empty(&procfs->fs_supers))
739 goto out;
741 proc_sb = list_entry(procfs->fs_supers.next, struct super_block,
742 s_instances);
744 sb->s_blocksize = 1024;
745 sb->s_blocksize_bits = 10;
746 sb->s_magic = HPPFS_SUPER_MAGIC;
747 sb->s_op = &hppfs_sbops;
749 root_inode = iget(sb, 0);
750 if(root_inode == NULL)
751 goto out;
753 err = init_inode(root_inode, proc_sb->s_root);
754 if(err)
755 goto out_put;
757 err = -ENOMEM;
758 sb->s_root = d_alloc_root(root_inode);
759 if(sb->s_root == NULL)
760 goto out_put;
762 hppfs_read_inode(root_inode);
764 return(0);
766 out_put:
767 iput(root_inode);
768 out:
769 return(err);
772 static struct super_block *hppfs_read_super(struct file_system_type *type,
773 int flags, const char *dev_name,
774 void *data)
776 return(get_sb_nodev(type, flags, data, hppfs_fill_super));
779 static struct file_system_type hppfs_type = {
780 .owner = THIS_MODULE,
781 .name = "hppfs",
782 .get_sb = hppfs_read_super,
783 .kill_sb = kill_anon_super,
784 .fs_flags = 0,
787 static int __init init_hppfs(void)
789 return(register_filesystem(&hppfs_type));
792 static void __exit exit_hppfs(void)
794 unregister_filesystem(&hppfs_type);
797 module_init(init_hppfs)
798 module_exit(exit_hppfs)
799 MODULE_LICENSE("GPL");
802 * Overrides for Emacs so that we follow Linus's tabbing style.
803 * Emacs will notice this stuff at the end of the file and automatically
804 * adjust the settings for this buffer only. This must remain at the end
805 * of the file.
806 * ---------------------------------------------------------------------------
807 * Local variables:
808 * c-file-style: "linux"
809 * End: