[readdir] introduce ->iterate(), ctx->pos, dir_emit()
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / parisc / hpux / fs.c
1 /*
2  *    Implements HPUX syscalls.
3  *
4  *    Copyright (C) 1999 Matthew Wilcox <willy with parisc-linux.org>
5  *    Copyright (C) 2000 Michael Ang <mang with subcarrier.org>
6  *    Copyright (C) 2000 John Marvin <jsm with parisc-linux.org>
7  *    Copyright (C) 2000 Philipp Rumpf
8  *
9  *    This program is free software; you can redistribute it and/or modify
10  *    it under the terms of the GNU General Public License as published by
11  *    the Free Software Foundation; either version 2 of the License, or
12  *    (at your option) any later version.
13  *
14  *    This program is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU General Public License for more details.
18  *
19  *    You should have received a copy of the GNU General Public License
20  *    along with this program; if not, write to the Free Software
21  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/fs.h>
27 #include <linux/sched.h>
28 #include <linux/file.h>
29 #include <linux/ptrace.h>
30 #include <linux/slab.h>
31 #include <asm/errno.h>
32 #include <asm/uaccess.h>
33
34 int hpux_execve(struct pt_regs *regs)
35 {
36         int error;
37         struct filename *filename;
38
39         filename = getname((const char __user *) regs->gr[26]);
40         error = PTR_ERR(filename);
41         if (IS_ERR(filename))
42                 goto out;
43
44         error = do_execve(filename->name,
45                           (const char __user *const __user *) regs->gr[25],
46                           (const char __user *const __user *) regs->gr[24]);
47
48         putname(filename);
49
50 out:
51         return error;
52 }
53
54 struct hpux_dirent {
55         loff_t  d_off;
56         ino_t   d_ino;
57         short   d_reclen;
58         short   d_namlen;
59         char    d_name[1];
60 };
61
62 struct getdents_callback {
63         struct dir_context ctx;
64         struct hpux_dirent __user *current_dir;
65         struct hpux_dirent __user *previous;
66         int count;
67         int error;
68 };
69
70 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
71
72 static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
73                 u64 ino, unsigned d_type)
74 {
75         struct hpux_dirent __user * dirent;
76         struct getdents_callback * buf = (struct getdents_callback *) __buf;
77         ino_t d_ino;
78         int reclen = ALIGN(NAME_OFFSET(dirent) + namlen + 1, sizeof(long));
79
80         buf->error = -EINVAL;   /* only used if we fail.. */
81         if (reclen > buf->count)
82                 return -EINVAL;
83         d_ino = ino;
84         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
85                 buf->error = -EOVERFLOW;
86                 return -EOVERFLOW;
87         }
88         dirent = buf->previous;
89         if (dirent)
90                 if (put_user(offset, &dirent->d_off))
91                         goto Efault;
92         dirent = buf->current_dir;
93         if (put_user(d_ino, &dirent->d_ino) ||
94             put_user(reclen, &dirent->d_reclen) ||
95             put_user(namlen, &dirent->d_namlen) ||
96             copy_to_user(dirent->d_name, name, namlen) ||
97             put_user(0, dirent->d_name + namlen))
98                 goto Efault;
99         buf->previous = dirent;
100         buf->current_dir = (void __user *)dirent + reclen;
101         buf->count -= reclen;
102         return 0;
103 Efault:
104         buf->error = -EFAULT;
105         return -EFAULT;
106 }
107
108 #undef NAME_OFFSET
109
110 int hpux_getdents(unsigned int fd, struct hpux_dirent __user *dirent, unsigned int count)
111 {
112         struct fd arg;
113         struct hpux_dirent __user * lastdirent;
114         struct getdents_callback buf;
115         int error;
116
117         arg = fdget(fd);
118         if (!arg.file)
119                 return -EBADF;
120
121         buf.current_dir = dirent;
122         buf.previous = NULL;
123         buf.count = count;
124         buf.error = 0;
125         buf.ctx.actor = filldir;
126
127         error = iterate_dir(arg.file, &buf.ctx);
128         if (error >= 0)
129                 error = buf.error;
130         lastdirent = buf.previous;
131         if (lastdirent) {
132                 if (put_user(buf.ctx.pos, &lastdirent->d_off))
133                         error = -EFAULT;
134                 else
135                         error = count - buf.count;
136         }
137
138         fdput(arg);
139         return error;
140 }
141
142 int hpux_mount(const char *fs, const char *path, int mflag,
143                 const char *fstype, const char *dataptr, int datalen)
144 {
145         return -ENOSYS;
146 }
147
148 static int cp_hpux_stat(struct kstat *stat, struct hpux_stat64 __user *statbuf)
149 {
150         struct hpux_stat64 tmp;
151
152         /* we probably want a different split here - is hpux 12:20? */
153
154         if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
155                 return -EOVERFLOW;
156
157         memset(&tmp, 0, sizeof(tmp));
158         tmp.st_dev = new_encode_dev(stat->dev);
159         tmp.st_ino = stat->ino;
160         tmp.st_mode = stat->mode;
161         tmp.st_nlink = stat->nlink;
162         tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
163         tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
164         tmp.st_rdev = new_encode_dev(stat->rdev);
165         tmp.st_size = stat->size;
166         tmp.st_atime = stat->atime.tv_sec;
167         tmp.st_mtime = stat->mtime.tv_sec;
168         tmp.st_ctime = stat->ctime.tv_sec;
169         tmp.st_blocks = stat->blocks;
170         tmp.st_blksize = stat->blksize;
171         return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
172 }
173
174 long hpux_stat64(const char __user *filename, struct hpux_stat64 __user *statbuf)
175 {
176         struct kstat stat;
177         int error = vfs_stat(filename, &stat);
178
179         if (!error)
180                 error = cp_hpux_stat(&stat, statbuf);
181
182         return error;
183 }
184
185 long hpux_fstat64(unsigned int fd, struct hpux_stat64 __user *statbuf)
186 {
187         struct kstat stat;
188         int error = vfs_fstat(fd, &stat);
189
190         if (!error)
191                 error = cp_hpux_stat(&stat, statbuf);
192
193         return error;
194 }
195
196 long hpux_lstat64(const char __user *filename,
197                   struct hpux_stat64 __user *statbuf)
198 {
199         struct kstat stat;
200         int error = vfs_lstat(filename, &stat);
201
202         if (!error)
203                 error = cp_hpux_stat(&stat, statbuf);
204
205         return error;
206 }