[PATCH] I2C: Clarify the usage of i2c-dev.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ncpfs / ioctl.c
blob88df79356a1f333b75f079f6940dc9492709312d
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/errno.h>
14 #include <linux/fs.h>
15 #include <linux/ioctl.h>
16 #include <linux/time.h>
17 #include <linux/mm.h>
18 #include <linux/highuid.h>
19 #include <linux/vmalloc.h>
21 #include <linux/ncp_fs.h>
23 #include "ncplib_kernel.h"
25 /* maximum limit for ncp_objectname_ioctl */
26 #define NCP_OBJECT_NAME_MAX_LEN 4096
27 /* maximum limit for ncp_privatedata_ioctl */
28 #define NCP_PRIVATE_DATA_MAX_LEN 8192
29 /* maximum negotiable packet size */
30 #define NCP_PACKET_SIZE_INTERNAL 65536
32 static int
33 ncp_get_fs_info(struct ncp_server* server, struct inode* inode, struct ncp_fs_info __user *arg)
35 struct ncp_fs_info info;
37 if ((permission(inode, MAY_WRITE, NULL) != 0)
38 && (current->uid != server->m.mounted_uid)) {
39 return -EACCES;
41 if (copy_from_user(&info, arg, sizeof(info)))
42 return -EFAULT;
44 if (info.version != NCP_GET_FS_INFO_VERSION) {
45 DPRINTK("info.version invalid: %d\n", info.version);
46 return -EINVAL;
48 /* TODO: info.addr = server->m.serv_addr; */
49 SET_UID(info.mounted_uid, server->m.mounted_uid);
50 info.connection = server->connection;
51 info.buffer_size = server->buffer_size;
52 info.volume_number = NCP_FINFO(inode)->volNumber;
53 info.directory_id = NCP_FINFO(inode)->DosDirNum;
55 if (copy_to_user(arg, &info, sizeof(info)))
56 return -EFAULT;
57 return 0;
60 static int
61 ncp_get_fs_info_v2(struct ncp_server* server, struct inode* inode, struct ncp_fs_info_v2 __user * arg)
63 struct ncp_fs_info_v2 info2;
65 if ((permission(inode, MAY_WRITE, NULL) != 0)
66 && (current->uid != server->m.mounted_uid)) {
67 return -EACCES;
69 if (copy_from_user(&info2, arg, sizeof(info2)))
70 return -EFAULT;
72 if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
73 DPRINTK("info.version invalid: %d\n", info2.version);
74 return -EINVAL;
76 info2.mounted_uid = server->m.mounted_uid;
77 info2.connection = server->connection;
78 info2.buffer_size = server->buffer_size;
79 info2.volume_number = NCP_FINFO(inode)->volNumber;
80 info2.directory_id = NCP_FINFO(inode)->DosDirNum;
81 info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
83 if (copy_to_user(arg, &info2, sizeof(info2)))
84 return -EFAULT;
85 return 0;
88 #ifdef CONFIG_NCPFS_NLS
89 /* Here we are select the iocharset and the codepage for NLS.
90 * Thanks Petr Vandrovec for idea and many hints.
92 static int
93 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
95 struct ncp_nls_ioctl user;
96 struct nls_table *codepage;
97 struct nls_table *iocharset;
98 struct nls_table *oldset_io;
99 struct nls_table *oldset_cp;
101 if (!capable(CAP_SYS_ADMIN))
102 return -EACCES;
103 if (server->root_setuped)
104 return -EBUSY;
106 if (copy_from_user(&user, arg, sizeof(user)))
107 return -EFAULT;
109 codepage = NULL;
110 user.codepage[NCP_IOCSNAME_LEN] = 0;
111 if (!user.codepage[0] || !strcmp(user.codepage, "default"))
112 codepage = load_nls_default();
113 else {
114 codepage = load_nls(user.codepage);
115 if (!codepage) {
116 return -EBADRQC;
120 iocharset = NULL;
121 user.iocharset[NCP_IOCSNAME_LEN] = 0;
122 if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
123 iocharset = load_nls_default();
124 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
125 } else if (!strcmp(user.iocharset, "utf8")) {
126 iocharset = load_nls_default();
127 NCP_SET_FLAG(server, NCP_FLAG_UTF8);
128 } else {
129 iocharset = load_nls(user.iocharset);
130 if (!iocharset) {
131 unload_nls(codepage);
132 return -EBADRQC;
134 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
137 oldset_cp = server->nls_vol;
138 server->nls_vol = codepage;
139 oldset_io = server->nls_io;
140 server->nls_io = iocharset;
142 if (oldset_cp)
143 unload_nls(oldset_cp);
144 if (oldset_io)
145 unload_nls(oldset_io);
147 return 0;
150 static int
151 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
153 struct ncp_nls_ioctl user;
154 int len;
156 memset(&user, 0, sizeof(user));
157 if (server->nls_vol && server->nls_vol->charset) {
158 len = strlen(server->nls_vol->charset);
159 if (len > NCP_IOCSNAME_LEN)
160 len = NCP_IOCSNAME_LEN;
161 strncpy(user.codepage, server->nls_vol->charset, len);
162 user.codepage[len] = 0;
165 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
166 strcpy(user.iocharset, "utf8");
167 else if (server->nls_io && server->nls_io->charset) {
168 len = strlen(server->nls_io->charset);
169 if (len > NCP_IOCSNAME_LEN)
170 len = NCP_IOCSNAME_LEN;
171 strncpy(user.iocharset, server->nls_io->charset, len);
172 user.iocharset[len] = 0;
175 if (copy_to_user(arg, &user, sizeof(user)))
176 return -EFAULT;
177 return 0;
179 #endif /* CONFIG_NCPFS_NLS */
181 int ncp_ioctl(struct inode *inode, struct file *filp,
182 unsigned int cmd, unsigned long arg)
184 struct ncp_server *server = NCP_SERVER(inode);
185 int result;
186 struct ncp_ioctl_request request;
187 char* bouncebuffer;
188 void __user *argp = (void __user *)arg;
190 switch (cmd) {
191 case NCP_IOC_NCPREQUEST:
193 if ((permission(inode, MAY_WRITE, NULL) != 0)
194 && (current->uid != server->m.mounted_uid)) {
195 return -EACCES;
197 if (copy_from_user(&request, argp, sizeof(request)))
198 return -EFAULT;
200 if ((request.function > 255)
201 || (request.size >
202 NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
203 return -EINVAL;
205 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
206 if (!bouncebuffer)
207 return -ENOMEM;
208 if (copy_from_user(bouncebuffer, request.data, request.size)) {
209 vfree(bouncebuffer);
210 return -EFAULT;
212 ncp_lock_server(server);
214 /* FIXME: We hack around in the server's structures
215 here to be able to use ncp_request */
217 server->has_subfunction = 0;
218 server->current_size = request.size;
219 memcpy(server->packet, bouncebuffer, request.size);
221 result = ncp_request2(server, request.function,
222 bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
223 if (result < 0)
224 result = -EIO;
225 else
226 result = server->reply_size;
227 ncp_unlock_server(server);
228 DPRINTK("ncp_ioctl: copy %d bytes\n",
229 result);
230 if (result >= 0)
231 if (copy_to_user(request.data, bouncebuffer, result))
232 result = -EFAULT;
233 vfree(bouncebuffer);
234 return result;
236 case NCP_IOC_CONN_LOGGED_IN:
238 if (!capable(CAP_SYS_ADMIN))
239 return -EACCES;
240 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
241 return -EINVAL;
242 if (server->root_setuped)
243 return -EBUSY;
244 server->root_setuped = 1;
245 return ncp_conn_logged_in(inode->i_sb);
247 case NCP_IOC_GET_FS_INFO:
248 return ncp_get_fs_info(server, inode, argp);
250 case NCP_IOC_GET_FS_INFO_V2:
251 return ncp_get_fs_info_v2(server, inode, argp);
253 case NCP_IOC_GETMOUNTUID2:
255 unsigned long tmp = server->m.mounted_uid;
257 if ( (permission(inode, MAY_READ, NULL) != 0)
258 && (current->uid != server->m.mounted_uid))
260 return -EACCES;
262 if (put_user(tmp, (unsigned long __user *)argp))
263 return -EFAULT;
264 return 0;
267 case NCP_IOC_GETROOT:
269 struct ncp_setroot_ioctl sr;
271 if ( (permission(inode, MAY_READ, NULL) != 0)
272 && (current->uid != server->m.mounted_uid))
274 return -EACCES;
276 if (server->m.mounted_vol[0]) {
277 struct dentry* dentry = inode->i_sb->s_root;
279 if (dentry) {
280 struct inode* inode = dentry->d_inode;
282 if (inode) {
283 sr.volNumber = NCP_FINFO(inode)->volNumber;
284 sr.dirEntNum = NCP_FINFO(inode)->dirEntNum;
285 sr.namespace = server->name_space[sr.volNumber];
286 } else
287 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
288 } else
289 DPRINTK("ncpfs: s_root==NULL\n");
290 } else {
291 sr.volNumber = -1;
292 sr.namespace = 0;
293 sr.dirEntNum = 0;
295 if (copy_to_user(argp, &sr, sizeof(sr)))
296 return -EFAULT;
297 return 0;
299 case NCP_IOC_SETROOT:
301 struct ncp_setroot_ioctl sr;
302 __u32 vnum;
303 __le32 de;
304 __le32 dosde;
305 struct dentry* dentry;
307 if (!capable(CAP_SYS_ADMIN))
309 return -EACCES;
311 if (server->root_setuped) return -EBUSY;
312 if (copy_from_user(&sr, argp, sizeof(sr)))
313 return -EFAULT;
314 if (sr.volNumber < 0) {
315 server->m.mounted_vol[0] = 0;
316 vnum = NCP_NUMBER_OF_VOLUMES;
317 de = 0;
318 dosde = 0;
319 } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
320 return -EINVAL;
321 } else if (ncp_mount_subdir(server, sr.volNumber,
322 sr.namespace, sr.dirEntNum,
323 &vnum, &de, &dosde)) {
324 return -ENOENT;
327 dentry = inode->i_sb->s_root;
328 server->root_setuped = 1;
329 if (dentry) {
330 struct inode* inode = dentry->d_inode;
332 if (inode) {
333 NCP_FINFO(inode)->volNumber = vnum;
334 NCP_FINFO(inode)->dirEntNum = de;
335 NCP_FINFO(inode)->DosDirNum = dosde;
336 } else
337 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
338 } else
339 DPRINTK("ncpfs: s_root==NULL\n");
341 return 0;
344 #ifdef CONFIG_NCPFS_PACKET_SIGNING
345 case NCP_IOC_SIGN_INIT:
346 if ((permission(inode, MAY_WRITE, NULL) != 0)
347 && (current->uid != server->m.mounted_uid))
349 return -EACCES;
351 if (argp) {
352 if (server->sign_wanted)
354 struct ncp_sign_init sign;
356 if (copy_from_user(&sign, argp, sizeof(sign)))
357 return -EFAULT;
358 memcpy(server->sign_root,sign.sign_root,8);
359 memcpy(server->sign_last,sign.sign_last,16);
360 server->sign_active = 1;
362 /* ignore when signatures not wanted */
363 } else {
364 server->sign_active = 0;
366 return 0;
368 case NCP_IOC_SIGN_WANTED:
369 if ( (permission(inode, MAY_READ, NULL) != 0)
370 && (current->uid != server->m.mounted_uid))
372 return -EACCES;
375 if (put_user(server->sign_wanted, (int __user *)argp))
376 return -EFAULT;
377 return 0;
378 case NCP_IOC_SET_SIGN_WANTED:
380 int newstate;
382 if ( (permission(inode, MAY_WRITE, NULL) != 0)
383 && (current->uid != server->m.mounted_uid))
385 return -EACCES;
387 /* get only low 8 bits... */
388 if (get_user(newstate, (unsigned char __user *)argp))
389 return -EFAULT;
390 if (server->sign_active) {
391 /* cannot turn signatures OFF when active */
392 if (!newstate) return -EINVAL;
393 } else {
394 server->sign_wanted = newstate != 0;
396 return 0;
399 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
401 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
402 case NCP_IOC_LOCKUNLOCK:
403 if ( (permission(inode, MAY_WRITE, NULL) != 0)
404 && (current->uid != server->m.mounted_uid))
406 return -EACCES;
409 struct ncp_lock_ioctl rqdata;
410 int result;
412 if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
413 return -EFAULT;
414 if (rqdata.origin != 0)
415 return -EINVAL;
416 /* check for cmd */
417 switch (rqdata.cmd) {
418 case NCP_LOCK_EX:
419 case NCP_LOCK_SH:
420 if (rqdata.timeout == 0)
421 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
422 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
423 rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
424 break;
425 case NCP_LOCK_LOG:
426 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT; /* has no effect */
427 case NCP_LOCK_CLEAR:
428 break;
429 default:
430 return -EINVAL;
432 /* locking needs both read and write access */
433 if ((result = ncp_make_open(inode, O_RDWR)) != 0)
435 return result;
437 result = -EIO;
438 if (!ncp_conn_valid(server))
439 goto outrel;
440 result = -EISDIR;
441 if (!S_ISREG(inode->i_mode))
442 goto outrel;
443 if (rqdata.cmd == NCP_LOCK_CLEAR)
445 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
446 NCP_FINFO(inode)->file_handle,
447 rqdata.offset,
448 rqdata.length);
449 if (result > 0) result = 0; /* no such lock */
451 else
453 int lockcmd;
455 switch (rqdata.cmd)
457 case NCP_LOCK_EX: lockcmd=1; break;
458 case NCP_LOCK_SH: lockcmd=3; break;
459 default: lockcmd=0; break;
461 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
462 NCP_FINFO(inode)->file_handle,
463 lockcmd,
464 rqdata.offset,
465 rqdata.length,
466 rqdata.timeout);
467 if (result > 0) result = -EAGAIN;
469 outrel:
470 ncp_inode_close(inode);
471 return result;
473 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
475 case NCP_IOC_GETOBJECTNAME:
476 if (current->uid != server->m.mounted_uid) {
477 return -EACCES;
480 struct ncp_objectname_ioctl user;
481 size_t outl;
483 if (copy_from_user(&user, argp, sizeof(user)))
484 return -EFAULT;
485 user.auth_type = server->auth.auth_type;
486 outl = user.object_name_len;
487 user.object_name_len = server->auth.object_name_len;
488 if (outl > user.object_name_len)
489 outl = user.object_name_len;
490 if (outl) {
491 if (copy_to_user(user.object_name,
492 server->auth.object_name,
493 outl)) return -EFAULT;
495 if (copy_to_user(argp, &user, sizeof(user)))
496 return -EFAULT;
497 return 0;
499 case NCP_IOC_SETOBJECTNAME:
500 if (current->uid != server->m.mounted_uid) {
501 return -EACCES;
504 struct ncp_objectname_ioctl user;
505 void* newname;
506 void* oldname;
507 size_t oldnamelen;
508 void* oldprivate;
509 size_t oldprivatelen;
511 if (copy_from_user(&user, argp, sizeof(user)))
512 return -EFAULT;
513 if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
514 return -ENOMEM;
515 if (user.object_name_len) {
516 newname = ncp_kmalloc(user.object_name_len, GFP_USER);
517 if (!newname) return -ENOMEM;
518 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
519 ncp_kfree_s(newname, user.object_name_len);
520 return -EFAULT;
522 } else {
523 newname = NULL;
525 /* enter critical section */
526 /* maybe that kfree can sleep so do that this way */
527 /* it is at least more SMP friendly (in future...) */
528 oldname = server->auth.object_name;
529 oldnamelen = server->auth.object_name_len;
530 oldprivate = server->priv.data;
531 oldprivatelen = server->priv.len;
532 server->auth.auth_type = user.auth_type;
533 server->auth.object_name_len = user.object_name_len;
534 server->auth.object_name = newname;
535 server->priv.len = 0;
536 server->priv.data = NULL;
537 /* leave critical section */
538 if (oldprivate) ncp_kfree_s(oldprivate, oldprivatelen);
539 if (oldname) ncp_kfree_s(oldname, oldnamelen);
540 return 0;
542 case NCP_IOC_GETPRIVATEDATA:
543 if (current->uid != server->m.mounted_uid) {
544 return -EACCES;
547 struct ncp_privatedata_ioctl user;
548 size_t outl;
550 if (copy_from_user(&user, argp, sizeof(user)))
551 return -EFAULT;
552 outl = user.len;
553 user.len = server->priv.len;
554 if (outl > user.len) outl = user.len;
555 if (outl) {
556 if (copy_to_user(user.data,
557 server->priv.data,
558 outl)) return -EFAULT;
560 if (copy_to_user(argp, &user, sizeof(user)))
561 return -EFAULT;
562 return 0;
564 case NCP_IOC_SETPRIVATEDATA:
565 if (current->uid != server->m.mounted_uid) {
566 return -EACCES;
569 struct ncp_privatedata_ioctl user;
570 void* new;
571 void* old;
572 size_t oldlen;
574 if (copy_from_user(&user, argp, sizeof(user)))
575 return -EFAULT;
576 if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
577 return -ENOMEM;
578 if (user.len) {
579 new = ncp_kmalloc(user.len, GFP_USER);
580 if (!new) return -ENOMEM;
581 if (copy_from_user(new, user.data, user.len)) {
582 ncp_kfree_s(new, user.len);
583 return -EFAULT;
585 } else {
586 new = NULL;
588 /* enter critical section */
589 old = server->priv.data;
590 oldlen = server->priv.len;
591 server->priv.len = user.len;
592 server->priv.data = new;
593 /* leave critical section */
594 if (old) ncp_kfree_s(old, oldlen);
595 return 0;
598 #ifdef CONFIG_NCPFS_NLS
599 case NCP_IOC_SETCHARSETS:
600 return ncp_set_charsets(server, argp);
602 case NCP_IOC_GETCHARSETS:
603 return ncp_get_charsets(server, argp);
605 #endif /* CONFIG_NCPFS_NLS */
607 case NCP_IOC_SETDENTRYTTL:
608 if ((permission(inode, MAY_WRITE, NULL) != 0) &&
609 (current->uid != server->m.mounted_uid))
610 return -EACCES;
612 u_int32_t user;
614 if (copy_from_user(&user, argp, sizeof(user)))
615 return -EFAULT;
616 /* 20 secs at most... */
617 if (user > 20000)
618 return -EINVAL;
619 user = (user * HZ) / 1000;
620 server->dentry_ttl = user;
621 return 0;
624 case NCP_IOC_GETDENTRYTTL:
626 u_int32_t user = (server->dentry_ttl * 1000) / HZ;
627 if (copy_to_user(argp, &user, sizeof(user)))
628 return -EFAULT;
629 return 0;
633 /* #ifdef CONFIG_UID16 */
634 /* NCP_IOC_GETMOUNTUID may be same as NCP_IOC_GETMOUNTUID2,
635 so we have this out of switch */
636 if (cmd == NCP_IOC_GETMOUNTUID) {
637 __kernel_uid_t uid = 0;
638 if ((permission(inode, MAY_READ, NULL) != 0)
639 && (current->uid != server->m.mounted_uid)) {
640 return -EACCES;
642 SET_UID(uid, server->m.mounted_uid);
643 if (put_user(uid, (__kernel_uid_t __user *)argp))
644 return -EFAULT;
645 return 0;
647 /* #endif */
648 return -EINVAL;