[PATCH] mm: compound release fix
[wandboard.git] / fs / ncpfs / ioctl.c
blobeb3813ad136f192e5c0a7551eefc8cd417d09fb3
1 /*
2 * ioctl.c
4 * Copyright (C) 1995, 1996 by Volker Lendecke
5 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
6 * Modified 1998, 1999 Wolfram Pienkoss for NLS
8 */
10 #include <linux/config.h>
12 #include <asm/uaccess.h>
13 #include <linux/capability.h>
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/ioctl.h>
17 #include <linux/time.h>
18 #include <linux/mm.h>
19 #include <linux/highuid.h>
20 #include <linux/vmalloc.h>
22 #include <linux/ncp_fs.h>
24 #include "ncplib_kernel.h"
26 /* maximum limit for ncp_objectname_ioctl */
27 #define NCP_OBJECT_NAME_MAX_LEN 4096
28 /* maximum limit for ncp_privatedata_ioctl */
29 #define NCP_PRIVATE_DATA_MAX_LEN 8192
30 /* maximum negotiable packet size */
31 #define NCP_PACKET_SIZE_INTERNAL 65536
33 static int
34 ncp_get_fs_info(struct ncp_server * server, struct file *file,
35 struct ncp_fs_info __user *arg)
37 struct inode *inode = file->f_dentry->d_inode;
38 struct ncp_fs_info info;
40 if ((file_permission(file, MAY_WRITE) != 0)
41 && (current->uid != server->m.mounted_uid)) {
42 return -EACCES;
44 if (copy_from_user(&info, arg, sizeof(info)))
45 return -EFAULT;
47 if (info.version != NCP_GET_FS_INFO_VERSION) {
48 DPRINTK("info.version invalid: %d\n", info.version);
49 return -EINVAL;
51 /* TODO: info.addr = server->m.serv_addr; */
52 SET_UID(info.mounted_uid, server->m.mounted_uid);
53 info.connection = server->connection;
54 info.buffer_size = server->buffer_size;
55 info.volume_number = NCP_FINFO(inode)->volNumber;
56 info.directory_id = NCP_FINFO(inode)->DosDirNum;
58 if (copy_to_user(arg, &info, sizeof(info)))
59 return -EFAULT;
60 return 0;
63 static int
64 ncp_get_fs_info_v2(struct ncp_server * server, struct file *file,
65 struct ncp_fs_info_v2 __user * arg)
67 struct inode *inode = file->f_dentry->d_inode;
68 struct ncp_fs_info_v2 info2;
70 if ((file_permission(file, MAY_WRITE) != 0)
71 && (current->uid != server->m.mounted_uid)) {
72 return -EACCES;
74 if (copy_from_user(&info2, arg, sizeof(info2)))
75 return -EFAULT;
77 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
78 DPRINTK("info.version invalid: %d\n", info2.version);
79 return -EINVAL;
81 info2.mounted_uid = server->m.mounted_uid;
82 info2.connection = server->connection;
83 info2.buffer_size = server->buffer_size;
84 info2.volume_number = NCP_FINFO(inode)->volNumber;
85 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
86 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
88 if (copy_to_user(arg, &info2, sizeof(info2)))
89 return -EFAULT;
90 return 0;
93 #ifdef CONFIG_NCPFS_NLS
94 /* Here we are select the iocharset and the codepage for NLS.
95 * Thanks Petr Vandrovec for idea and many hints.
97 static int
98 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
100 struct ncp_nls_ioctl user;
101 struct nls_table *codepage;
102 struct nls_table *iocharset;
103 struct nls_table *oldset_io;
104 struct nls_table *oldset_cp;
106 if (!capable(CAP_SYS_ADMIN))
107 return -EACCES;
108 if (server->root_setuped)
109 return -EBUSY;
111 if (copy_from_user(&user, arg, sizeof(user)))
112 return -EFAULT;
114 codepage = NULL;
115 user.codepage[NCP_IOCSNAME_LEN] = 0;
116 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
117 codepage = load_nls_default();
118 else {
119 codepage = load_nls(user.codepage);
120 if (!codepage) {
121 return -EBADRQC;
125 iocharset = NULL;
126 user.iocharset[NCP_IOCSNAME_LEN] = 0;
127 if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
128 iocharset = load_nls_default();
129 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
130 } else if (!strcmp(user.iocharset, "utf8")) {
131 iocharset = load_nls_default();
132 NCP_SET_FLAG(server, NCP_FLAG_UTF8);
133 } else {
134 iocharset = load_nls(user.iocharset);
135 if (!iocharset) {
136 unload_nls(codepage);
137 return -EBADRQC;
139 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
142 oldset_cp = server->nls_vol;
143 server->nls_vol = codepage;
144 oldset_io = server->nls_io;
145 server->nls_io = iocharset;
147 if (oldset_cp)
148 unload_nls(oldset_cp);
149 if (oldset_io)
150 unload_nls(oldset_io);
152 return 0;
155 static int
156 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
158 struct ncp_nls_ioctl user;
159 int len;
161 memset(&user, 0, sizeof(user));
162 if (server->nls_vol && server->nls_vol->charset) {
163 len = strlen(server->nls_vol->charset);
164 if (len > NCP_IOCSNAME_LEN)
165 len = NCP_IOCSNAME_LEN;
166 strncpy(user.codepage, server->nls_vol->charset, len);
167 user.codepage[len] = 0;
170 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
171 strcpy(user.iocharset, "utf8");
172 else if (server->nls_io && server->nls_io->charset) {
173 len = strlen(server->nls_io->charset);
174 if (len > NCP_IOCSNAME_LEN)
175 len = NCP_IOCSNAME_LEN;
176 strncpy(user.iocharset, server->nls_io->charset, len);
177 user.iocharset[len] = 0;
180 if (copy_to_user(arg, &user, sizeof(user)))
181 return -EFAULT;
182 return 0;
184 #endif /* CONFIG_NCPFS_NLS */
186 int ncp_ioctl(struct inode *inode, struct file *filp,
187 unsigned int cmd, unsigned long arg)
189 struct ncp_server *server = NCP_SERVER(inode);
190 int result;
191 struct ncp_ioctl_request request;
192 char* bouncebuffer;
193 void __user *argp = (void __user *)arg;
195 switch (cmd) {
196 case NCP_IOC_NCPREQUEST:
198 if ((file_permission(filp, MAY_WRITE) != 0)
199 && (current->uid != server->m.mounted_uid)) {
200 return -EACCES;
202 if (copy_from_user(&request, argp, sizeof(request)))
203 return -EFAULT;
205 if ((request.function > 255)
206 || (request.size >
207 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
208 return -EINVAL;
210 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
211 if (!bouncebuffer)
212 return -ENOMEM;
213 if (copy_from_user(bouncebuffer, request.data, request.size)) {
214 vfree(bouncebuffer);
215 return -EFAULT;
217 ncp_lock_server(server);
219 /* FIXME: We hack around in the server's structures
220 here to be able to use ncp_request */
222 server->has_subfunction = 0;
223 server->current_size = request.size;
224 memcpy(server->packet, bouncebuffer, request.size);
226 result = ncp_request2(server, request.function,
227 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
228 if (result < 0)
229 result = -EIO;
230 else
231 result = server->reply_size;
232 ncp_unlock_server(server);
233 DPRINTK("ncp_ioctl: copy %d bytes\n",
234 result);
235 if (result >= 0)
236 if (copy_to_user(request.data, bouncebuffer, result))
237 result = -EFAULT;
238 vfree(bouncebuffer);
239 return result;
241 case NCP_IOC_CONN_LOGGED_IN:
243 if (!capable(CAP_SYS_ADMIN))
244 return -EACCES;
245 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
246 return -EINVAL;
247 if (server->root_setuped)
248 return -EBUSY;
249 server->root_setuped = 1;
250 return ncp_conn_logged_in(inode->i_sb);
252 case NCP_IOC_GET_FS_INFO:
253 return ncp_get_fs_info(server, filp, argp);
255 case NCP_IOC_GET_FS_INFO_V2:
256 return ncp_get_fs_info_v2(server, filp, argp);
258 case NCP_IOC_GETMOUNTUID2:
260 unsigned long tmp = server->m.mounted_uid;
262 if ((file_permission(filp, MAY_READ) != 0)
263 && (current->uid != server->m.mounted_uid))
265 return -EACCES;
267 if (put_user(tmp, (unsigned long __user *)argp))
268 return -EFAULT;
269 return 0;
272 case NCP_IOC_GETROOT:
274 struct ncp_setroot_ioctl sr;
276 if ((file_permission(filp, MAY_READ) != 0)
277 && (current->uid != server->m.mounted_uid))
279 return -EACCES;
281 if (server->m.mounted_vol[0]) {
282 struct dentry* dentry = inode->i_sb->s_root;
284 if (dentry) {
285 struct inode* inode = dentry->d_inode;
287 if (inode) {
288 sr.volNumber = NCP_FINFO(inode)->volNumber;
289 sr.dirEntNum = NCP_FINFO(inode)->dirEntNum;
290 sr.namespace = server->name_space[sr.volNumber];
291 } else
292 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
293 } else
294 DPRINTK("ncpfs: s_root==NULL\n");
295 } else {
296 sr.volNumber = -1;
297 sr.namespace = 0;
298 sr.dirEntNum = 0;
300 if (copy_to_user(argp, &sr, sizeof(sr)))
301 return -EFAULT;
302 return 0;
304 case NCP_IOC_SETROOT:
306 struct ncp_setroot_ioctl sr;
307 __u32 vnum;
308 __le32 de;
309 __le32 dosde;
310 struct dentry* dentry;
312 if (!capable(CAP_SYS_ADMIN))
314 return -EACCES;
316 if (server->root_setuped) return -EBUSY;
317 if (copy_from_user(&sr, argp, sizeof(sr)))
318 return -EFAULT;
319 if (sr.volNumber < 0) {
320 server->m.mounted_vol[0] = 0;
321 vnum = NCP_NUMBER_OF_VOLUMES;
322 de = 0;
323 dosde = 0;
324 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
325 return -EINVAL;
326 } else if (ncp_mount_subdir(server, sr.volNumber,
327 sr.namespace, sr.dirEntNum,
328 &vnum, &de, &dosde)) {
329 return -ENOENT;
332 dentry = inode->i_sb->s_root;
333 server->root_setuped = 1;
334 if (dentry) {
335 struct inode* inode = dentry->d_inode;
337 if (inode) {
338 NCP_FINFO(inode)->volNumber = vnum;
339 NCP_FINFO(inode)->dirEntNum = de;
340 NCP_FINFO(inode)->DosDirNum = dosde;
341 } else
342 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
343 } else
344 DPRINTK("ncpfs: s_root==NULL\n");
346 return 0;
349 #ifdef CONFIG_NCPFS_PACKET_SIGNING
350 case NCP_IOC_SIGN_INIT:
351 if ((file_permission(filp, MAY_WRITE) != 0)
352 && (current->uid != server->m.mounted_uid))
354 return -EACCES;
356 if (argp) {
357 if (server->sign_wanted)
359 struct ncp_sign_init sign;
361 if (copy_from_user(&sign, argp, sizeof(sign)))
362 return -EFAULT;
363 memcpy(server->sign_root,sign.sign_root,8);
364 memcpy(server->sign_last,sign.sign_last,16);
365 server->sign_active = 1;
367 /* ignore when signatures not wanted */
368 } else {
369 server->sign_active = 0;
371 return 0;
373 case NCP_IOC_SIGN_WANTED:
374 if ((file_permission(filp, MAY_READ) != 0)
375 && (current->uid != server->m.mounted_uid))
377 return -EACCES;
380 if (put_user(server->sign_wanted, (int __user *)argp))
381 return -EFAULT;
382 return 0;
383 case NCP_IOC_SET_SIGN_WANTED:
385 int newstate;
387 if ((file_permission(filp, MAY_WRITE) != 0)
388 && (current->uid != server->m.mounted_uid))
390 return -EACCES;
392 /* get only low 8 bits... */
393 if (get_user(newstate, (unsigned char __user *)argp))
394 return -EFAULT;
395 if (server->sign_active) {
396 /* cannot turn signatures OFF when active */
397 if (!newstate) return -EINVAL;
398 } else {
399 server->sign_wanted = newstate != 0;
401 return 0;
404 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
406 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
407 case NCP_IOC_LOCKUNLOCK:
408 if ((file_permission(filp, MAY_WRITE) != 0)
409 && (current->uid != server->m.mounted_uid))
411 return -EACCES;
414 struct ncp_lock_ioctl rqdata;
415 int result;
417 if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
418 return -EFAULT;
419 if (rqdata.origin != 0)
420 return -EINVAL;
421 /* check for cmd */
422 switch (rqdata.cmd) {
423 case NCP_LOCK_EX:
424 case NCP_LOCK_SH:
425 if (rqdata.timeout == 0)
426 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
427 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
428 rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
429 break;
430 case NCP_LOCK_LOG:
431 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
432 case NCP_LOCK_CLEAR:
433 break;
434 default:
435 return -EINVAL;
437 /* locking needs both read and write access */
438 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
440 return result;
442 result = -EIO;
443 if (!ncp_conn_valid(server))
444 goto outrel;
445 result = -EISDIR;
446 if (!S_ISREG(inode->i_mode))
447 goto outrel;
448 if (rqdata.cmd == NCP_LOCK_CLEAR)
450 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
451 NCP_FINFO(inode)->file_handle,
452 rqdata.offset,
453 rqdata.length);
454 if (result > 0) result = 0; /* no such lock */
456 else
458 int lockcmd;
460 switch (rqdata.cmd)
462 case NCP_LOCK_EX: lockcmd=1; break;
463 case NCP_LOCK_SH: lockcmd=3; break;
464 default: lockcmd=0; break;
466 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
467 NCP_FINFO(inode)->file_handle,
468 lockcmd,
469 rqdata.offset,
470 rqdata.length,
471 rqdata.timeout);
472 if (result > 0) result = -EAGAIN;
474 outrel:
475 ncp_inode_close(inode);
476 return result;
478 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
480 case NCP_IOC_GETOBJECTNAME:
481 if (current->uid != server->m.mounted_uid) {
482 return -EACCES;
485 struct ncp_objectname_ioctl user;
486 size_t outl;
488 if (copy_from_user(&user, argp, sizeof(user)))
489 return -EFAULT;
490 user.auth_type = server->auth.auth_type;
491 outl = user.object_name_len;
492 user.object_name_len = server->auth.object_name_len;
493 if (outl > user.object_name_len)
494 outl = user.object_name_len;
495 if (outl) {
496 if (copy_to_user(user.object_name,
497 server->auth.object_name,
498 outl)) return -EFAULT;
500 if (copy_to_user(argp, &user, sizeof(user)))
501 return -EFAULT;
502 return 0;
504 case NCP_IOC_SETOBJECTNAME:
505 if (current->uid != server->m.mounted_uid) {
506 return -EACCES;
509 struct ncp_objectname_ioctl user;
510 void* newname;
511 void* oldname;
512 size_t oldnamelen;
513 void* oldprivate;
514 size_t oldprivatelen;
516 if (copy_from_user(&user, argp, sizeof(user)))
517 return -EFAULT;
518 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
519 return -ENOMEM;
520 if (user.object_name_len) {
521 newname = kmalloc(user.object_name_len, GFP_USER);
522 if (!newname)
523 return -ENOMEM;
524 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
525 kfree(newname);
526 return -EFAULT;
528 } else {
529 newname = NULL;
531 /* enter critical section */
532 /* maybe that kfree can sleep so do that this way */
533 /* it is at least more SMP friendly (in future...) */
534 oldname = server->auth.object_name;
535 oldnamelen = server->auth.object_name_len;
536 oldprivate = server->priv.data;
537 oldprivatelen = server->priv.len;
538 server->auth.auth_type = user.auth_type;
539 server->auth.object_name_len = user.object_name_len;
540 server->auth.object_name = newname;
541 server->priv.len = 0;
542 server->priv.data = NULL;
543 /* leave critical section */
544 kfree(oldprivate);
545 kfree(oldname);
546 return 0;
548 case NCP_IOC_GETPRIVATEDATA:
549 if (current->uid != server->m.mounted_uid) {
550 return -EACCES;
553 struct ncp_privatedata_ioctl user;
554 size_t outl;
556 if (copy_from_user(&user, argp, sizeof(user)))
557 return -EFAULT;
558 outl = user.len;
559 user.len = server->priv.len;
560 if (outl > user.len) outl = user.len;
561 if (outl) {
562 if (copy_to_user(user.data,
563 server->priv.data,
564 outl)) return -EFAULT;
566 if (copy_to_user(argp, &user, sizeof(user)))
567 return -EFAULT;
568 return 0;
570 case NCP_IOC_SETPRIVATEDATA:
571 if (current->uid != server->m.mounted_uid) {
572 return -EACCES;
575 struct ncp_privatedata_ioctl user;
576 void* new;
577 void* old;
578 size_t oldlen;
580 if (copy_from_user(&user, argp, sizeof(user)))
581 return -EFAULT;
582 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
583 return -ENOMEM;
584 if (user.len) {
585 new = kmalloc(user.len, GFP_USER);
586 if (!new)
587 return -ENOMEM;
588 if (copy_from_user(new, user.data, user.len)) {
589 kfree(new);
590 return -EFAULT;
592 } else {
593 new = NULL;
595 /* enter critical section */
596 old = server->priv.data;
597 oldlen = server->priv.len;
598 server->priv.len = user.len;
599 server->priv.data = new;
600 /* leave critical section */
601 kfree(old);
602 return 0;
605 #ifdef CONFIG_NCPFS_NLS
606 case NCP_IOC_SETCHARSETS:
607 return ncp_set_charsets(server, argp);
609 case NCP_IOC_GETCHARSETS:
610 return ncp_get_charsets(server, argp);
612 #endif /* CONFIG_NCPFS_NLS */
614 case NCP_IOC_SETDENTRYTTL:
615 if ((file_permission(filp, MAY_WRITE) != 0) &&
616 (current->uid != server->m.mounted_uid))
617 return -EACCES;
619 u_int32_t user;
621 if (copy_from_user(&user, argp, sizeof(user)))
622 return -EFAULT;
623 /* 20 secs at most... */
624 if (user > 20000)
625 return -EINVAL;
626 user = (user * HZ) / 1000;
627 server->dentry_ttl = user;
628 return 0;
631 case NCP_IOC_GETDENTRYTTL:
633 u_int32_t user = (server->dentry_ttl * 1000) / HZ;
634 if (copy_to_user(argp, &user, sizeof(user)))
635 return -EFAULT;
636 return 0;
640 /* #ifdef CONFIG_UID16 */
641 /* NCP_IOC_GETMOUNTUID may be same as NCP_IOC_GETMOUNTUID2,
642 so we have this out of switch */
643 if (cmd == NCP_IOC_GETMOUNTUID) {
644 __kernel_uid_t uid = 0;
645 if ((file_permission(filp, MAY_READ) != 0)
646 && (current->uid != server->m.mounted_uid)) {
647 return -EACCES;
649 SET_UID(uid, server->m.mounted_uid);
650 if (put_user(uid, (__kernel_uid_t __user *)argp))
651 return -EFAULT;
652 return 0;
654 /* #endif */
655 return -EINVAL;