Merge with Linux 2.4.0-test6-pre2.
[linux-2.6/linux-mips.git] / fs / umsdos / ioctl.c
blob8731f8ecddcf2536fcaf6aacf328ce92b22d32d7
1 /*
2 * linux/fs/umsdos/ioctl.c
4 * Written 1993 by Jacques Gelinas
6 * Extended MS-DOS ioctl directory handling functions
7 */
9 #include <asm/uaccess.h>
10 #include <linux/errno.h>
11 #include <linux/mm.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/fs.h>
15 #include <linux/msdos_fs.h>
16 #include <linux/umsdos_fs.h>
18 struct UMSDOS_DIR_ONCE {
19 struct dirent *ent;
20 int count;
24 * Record a single entry the first call.
25 * Return -EINVAL the next one.
27 static int umsdos_ioctl_fill (
28 void *buf,
29 const char *name,
30 int name_len,
31 off_t offset,
32 ino_t ino)
34 int ret = -EINVAL;
35 struct UMSDOS_DIR_ONCE *d = (struct UMSDOS_DIR_ONCE *) buf;
37 if (d->count == 0) {
38 copy_to_user (d->ent->d_name, name, name_len);
39 put_user ('\0', d->ent->d_name + name_len);
40 put_user (name_len, &d->ent->d_reclen);
41 put_user (ino, &d->ent->d_ino);
42 put_user (offset, &d->ent->d_off);
43 d->count = 1;
44 ret = 0;
46 return ret;
51 * Perform special function on a directory
53 /* #Specification: ioctl / prototypes
54 * The official prototype for the umsdos ioctl on directory
55 * is:
57 * int ioctl (
58 * int fd, // File handle of the directory
59 * int cmd, // command
60 * struct umsdos_ioctl *data)
62 * The struct and the commands are defined in linux/umsdos_fs.h.
64 * umsdos_progs/umsdosio.c provide an interface in C++ to all
65 * these ioctl. umsdos_progs/udosctl is a small utility showing
66 * all this.
68 * These ioctl generally allow one to work on the EMD or the
69 * DOS directory independently. These are essential to implement
70 * the synchronise.
72 int UMSDOS_ioctl_dir(struct inode *dir, struct file *filp, unsigned int cmd,
73 unsigned long data_ptr)
75 struct dentry *dentry = filp->f_dentry;
76 struct umsdos_ioctl *idata = (struct umsdos_ioctl *) data_ptr;
77 int ret;
78 struct umsdos_ioctl data;
80 Printk(("UMSDOS_ioctl_dir: %s/%s, cmd=%d, data=%08lx\n",
81 dentry->d_parent->d_name.name, dentry->d_name.name, cmd, data_ptr));
83 /* forward non-umsdos ioctls - this hopefully doesn't cause conflicts */
84 if (cmd != UMSDOS_GETVERSION
85 && cmd != UMSDOS_READDIR_DOS
86 && cmd != UMSDOS_READDIR_EMD
87 && cmd != UMSDOS_INIT_EMD
88 && cmd != UMSDOS_CREAT_EMD
89 && cmd != UMSDOS_RENAME_DOS
90 && cmd != UMSDOS_UNLINK_EMD
91 && cmd != UMSDOS_UNLINK_DOS
92 && cmd != UMSDOS_RMDIR_DOS
93 && cmd != UMSDOS_STAT_DOS
94 && cmd != UMSDOS_DOS_SETUP)
95 return fat_dir_ioctl (dir, filp, cmd, data_ptr);
97 /* #Specification: ioctl / acces
98 * Only root (effective id) is allowed to do IOCTL on directory
99 * in UMSDOS. EPERM is returned for other user.
102 * Well, not all cases require write access, but it simplifies
103 * the code, and let's face it, there is only one client (umssync)
104 * for all this.
106 ret = verify_area (VERIFY_WRITE, (void *) data_ptr,
107 sizeof (struct umsdos_ioctl));
108 if (ret < 0)
109 goto out;
111 ret = -EPERM;
112 if (current->euid != 0 && cmd != UMSDOS_GETVERSION)
113 goto out;
115 ret = -EINVAL;
116 if (cmd == UMSDOS_GETVERSION) {
117 /* #Specification: ioctl / UMSDOS_GETVERSION
118 * The field version and release of the structure
119 * umsdos_ioctl are filled with the version and release
120 * number of the fs code in the kernel. This will allow
121 * some form of checking. Users won't be able to run
122 * incompatible utility such as the synchroniser (umssync).
123 * umsdos_progs/umsdosio.c enforce this checking.
125 * Return always 0.
127 put_user (UMSDOS_VERSION, &idata->version);
128 put_user (UMSDOS_RELEASE, &idata->release);
129 ret = 0;
130 goto out;
132 if (cmd == UMSDOS_READDIR_DOS) {
133 /* #Specification: ioctl / UMSDOS_READDIR_DOS
134 * One entry is read from the DOS directory at the current
135 * file position. The entry is put as is in the dos_dirent
136 * field of struct umsdos_ioctl.
138 * Return > 0 if success.
140 struct UMSDOS_DIR_ONCE bufk;
142 bufk.count = 0;
143 bufk.ent = &idata->dos_dirent;
145 fat_readdir (filp, &bufk, umsdos_ioctl_fill);
147 ret = bufk.count == 1 ? 1 : 0;
148 goto out;
150 if (cmd == UMSDOS_READDIR_EMD) {
151 /* #Specification: ioctl / UMSDOS_READDIR_EMD
152 * One entry is read from the EMD at the current
153 * file position. The entry is put as is in the umsdos_dirent
154 * field of struct umsdos_ioctl. The corresponding mangled
155 * DOS entry name is put in the dos_dirent field.
157 * All entries are read including hidden links. Blank
158 * entries are skipped.
160 * Return > 0 if success.
162 struct dentry *demd;
163 loff_t pos = filp->f_pos;
165 /* The absence of the EMD is simply seen as an EOF */
166 demd = umsdos_get_emd_dentry(dentry);
167 ret = PTR_ERR(demd);
168 if (IS_ERR(demd))
169 goto out;
170 ret = 0;
171 if (!demd->d_inode)
172 goto read_dput;
174 while (pos < demd->d_inode->i_size) {
175 off_t f_pos = pos;
176 struct umsdos_dirent entry;
177 struct umsdos_info info;
179 ret = umsdos_emd_dir_readentry (demd, &pos, &entry);
181 if (ret == -ENAMETOOLONG) {
182 printk (KERN_INFO "Fixing EMD entry with invalid size -- zeroing out\n");
183 memset (&info, 0, sizeof (info));
184 info.f_pos = f_pos;
185 info.recsize = UMSDOS_REC_SIZE;
186 ret = umsdos_writeentry (dentry, &info, 1);
187 continue;
190 if (ret)
191 break;
192 if (entry.name_len <= 0)
193 continue;
195 umsdos_parse (entry.name, entry.name_len, &info);
196 info.f_pos = f_pos;
197 umsdos_manglename (&info);
198 ret = -EFAULT;
199 if (copy_to_user (&idata->umsdos_dirent, &entry,
200 sizeof (entry)))
201 break;
202 if (copy_to_user (&idata->dos_dirent.d_name,
203 info.fake.fname,
204 info.fake.len + 1))
205 break;
206 ret = entry.name_len;
207 break;
209 /* update the original f_pos */
210 filp->f_pos = pos;
211 read_dput:
212 d_drop(demd);
213 dput(demd);
214 goto out;
216 if (cmd == UMSDOS_INIT_EMD) {
217 /* #Specification: ioctl / UMSDOS_INIT_EMD
218 * The UMSDOS_INIT_EMD command makes sure the EMD
219 * exists for a directory. If it does not, it is
220 * created. Also, it makes sure the directory function
221 * table (struct inode_operations) is set to the UMSDOS
222 * semantic. This mean that umssync may be applied to
223 * an "opened" msdos directory, and it will change behavior
224 * on the fly.
226 * Return 0 if success.
229 ret = umsdos_make_emd(dentry);
230 Printk(("UMSDOS_ioctl_dir: INIT_EMD %s/%s, ret=%d\n",
231 dentry->d_parent->d_name.name, dentry->d_name.name, ret));
232 umsdos_setup_dir (dentry);
233 goto out;
236 ret = -EFAULT;
237 if (copy_from_user (&data, idata, sizeof (data)))
238 goto out;
240 if (cmd == UMSDOS_CREAT_EMD) {
241 /* #Specification: ioctl / UMSDOS_CREAT_EMD
242 * The umsdos_dirent field of the struct umsdos_ioctl is used
243 * as is to create a new entry in the EMD of the directory.
244 * The DOS directory is not modified.
245 * No validation is done (yet).
247 * Return 0 if success.
249 struct umsdos_info info;
251 /* This makes sure info.entry and info in general
252 * is correctly initialised
254 memcpy (&info.entry, &data.umsdos_dirent,
255 sizeof (data.umsdos_dirent));
256 umsdos_parse (data.umsdos_dirent.name
257 ,data.umsdos_dirent.name_len, &info);
258 ret = umsdos_newentry (dentry, &info);
259 goto out;
261 else if (cmd == UMSDOS_RENAME_DOS) {
262 struct dentry *old_dentry, *new_dentry; /* FIXME */
264 /* #Specification: ioctl / UMSDOS_RENAME_DOS
265 * A file or directory is renamed in a DOS directory
266 * (not moved across directory). The source name
267 * is in the dos_dirent.name field and the destination
268 * is in umsdos_dirent.name field.
270 * This ioctl allows umssync to rename a mangled file
271 * name before syncing it back in the EMD.
273 old_dentry = umsdos_lookup_dentry (dentry,
274 data.dos_dirent.d_name,
275 data.dos_dirent.d_reclen ,1);
276 ret = PTR_ERR(old_dentry);
277 if (IS_ERR(old_dentry))
278 goto out;
279 new_dentry = umsdos_lookup_dentry (dentry,
280 data.umsdos_dirent.name,
281 data.umsdos_dirent.name_len, 1);
282 ret = PTR_ERR(new_dentry);
283 if (!IS_ERR(new_dentry)) {
284 printk("umsdos_ioctl: renaming %s/%s to %s/%s\n",
285 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
286 new_dentry->d_parent->d_name.name, new_dentry->d_name.name);
287 ret = msdos_rename (dir, old_dentry, dir, new_dentry);
288 d_drop(new_dentry);
289 d_drop(old_dentry);
290 dput(new_dentry);
292 dput(old_dentry);
293 goto out;
295 else if (cmd == UMSDOS_UNLINK_EMD) {
296 /* #Specification: ioctl / UMSDOS_UNLINK_EMD
297 * The umsdos_dirent field of the struct umsdos_ioctl is used
298 * as is to remove an entry from the EMD of the directory.
299 * No validation is done (yet). The mode field is used
300 * to validate S_ISDIR or S_ISREG.
302 * Return 0 if success.
304 struct umsdos_info info;
306 /* This makes sure info.entry and info in general
307 * is correctly initialised
309 memcpy (&info.entry, &data.umsdos_dirent,
310 sizeof (data.umsdos_dirent));
311 umsdos_parse (data.umsdos_dirent.name,
312 data.umsdos_dirent.name_len, &info);
313 ret = umsdos_delentry (dentry, &info,
314 S_ISDIR (data.umsdos_dirent.mode));
315 if (ret) {
316 printk(KERN_WARNING
317 "umsdos_ioctl: delentry %s/%s failed, ret=%d\n",
318 dentry->d_name.name, info.entry.name, ret);
320 goto out;
322 else if (cmd == UMSDOS_UNLINK_DOS) {
323 struct dentry *temp;
325 /* #Specification: ioctl / UMSDOS_UNLINK_DOS
326 * The dos_dirent field of the struct umsdos_ioctl is used to
327 * execute a msdos_unlink operation. The d_name and d_reclen
328 * fields are used.
330 * Return 0 if success.
332 temp = umsdos_lookup_dentry(dentry, data.dos_dirent.d_name,
333 data.dos_dirent.d_reclen, 1);
334 ret = PTR_ERR(temp);
335 if (IS_ERR(temp))
336 goto out;
337 ret = -ENOENT;
338 if (temp->d_inode) {
339 ret = -EISDIR;
340 if (!S_ISDIR(temp->d_inode->i_mode))
341 ret = msdos_unlink (dir, temp);
342 if (!ret)
343 d_delete(temp);
345 dput (temp);
346 goto out;
348 else if (cmd == UMSDOS_RMDIR_DOS) {
349 struct dentry *temp;
351 /* #Specification: ioctl / UMSDOS_RMDIR_DOS
352 * The dos_dirent field of the struct umsdos_ioctl is used to
353 * execute a msdos_rmdir operation. The d_name and d_reclen
354 * fields are used.
356 * Return 0 if success.
358 temp = umsdos_lookup_dentry(dentry, data.dos_dirent.d_name,
359 data.dos_dirent.d_reclen, 1);
360 ret = PTR_ERR(temp);
361 if (IS_ERR(temp))
362 goto out;
363 ret = -ENOENT;
364 if (temp->d_inode) {
365 ret = -ENOTDIR;
366 if (S_ISDIR(temp->d_inode->i_mode))
367 ret = msdos_rmdir (dir, temp);
368 if (!ret)
369 d_delete(temp);
371 dput (temp);
372 goto out;
374 } else if (cmd == UMSDOS_STAT_DOS) {
375 /* #Specification: ioctl / UMSDOS_STAT_DOS
376 * The dos_dirent field of the struct umsdos_ioctl is
377 * used to execute a stat operation in the DOS directory.
378 * The d_name and d_reclen fields are used.
380 * The following field of umsdos_ioctl.stat are filled.
382 * st_ino,st_mode,st_size,st_atime,st_mtime,st_ctime,
383 * Return 0 if success.
385 struct dentry *dret;
386 struct inode *inode;
388 dret = umsdos_lookup_dentry(dentry, data.dos_dirent.d_name,
389 data.dos_dirent.d_reclen, 1);
390 ret = PTR_ERR(dret);
391 if (IS_ERR(dret))
392 goto out;
393 ret = -ENOENT;
394 inode = dret->d_inode;
395 if (inode) {
396 data.stat.st_ino = inode->i_ino;
397 data.stat.st_mode = inode->i_mode;
398 data.stat.st_size = inode->i_size;
399 data.stat.st_atime = inode->i_atime;
400 data.stat.st_ctime = inode->i_ctime;
401 data.stat.st_mtime = inode->i_mtime;
402 ret = -EFAULT;
403 if (!copy_to_user (&idata->stat, &data.stat,
404 sizeof (data.stat)))
405 ret = 0;
407 dput(dret);
408 goto out;
410 else if (cmd == UMSDOS_DOS_SETUP) {
411 /* #Specification: ioctl / UMSDOS_DOS_SETUP
412 * The UMSDOS_DOS_SETUP ioctl allow changing the
413 * default permission of the MS-DOS filesystem driver
414 * on the fly. The MS-DOS driver applies global permissions
415 * to every file and directory. Normally these permissions
416 * are controlled by a mount option. This is not
417 * available for root partition, so a special utility
418 * (umssetup) is provided to do this, normally in
419 * /etc/rc.local.
421 * Be aware that this applies ONLY to MS-DOS directories
422 * (those without EMD --linux-.---). Umsdos directory
423 * have independent (standard) permission for each
424 * and every file.
426 * The field umsdos_dirent provide the information needed.
427 * umsdos_dirent.uid and gid sets the owner and group.
428 * umsdos_dirent.mode set the permissions flags.
430 dir->i_sb->u.msdos_sb.options.fs_uid = data.umsdos_dirent.uid;
431 dir->i_sb->u.msdos_sb.options.fs_gid = data.umsdos_dirent.gid;
432 dir->i_sb->u.msdos_sb.options.fs_umask = data.umsdos_dirent.mode;
433 ret = 0;
435 out:
436 Printk (("ioctl %d, returning %d\n", cmd, ret));
437 return ret;