Merge branch 'semaphore' of git://git.kernel.org/pub/scm/linux/kernel/git/willy/misc
[linux-2.6] / fs / ncpfs / ioctl.c
1 /*
2  *  ioctl.c
3  *
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
7  *
8  */
9
10 #include <linux/capability.h>
11 #include <linux/compat.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/ioctl.h>
15 #include <linux/time.h>
16 #include <linux/mm.h>
17 #include <linux/mount.h>
18 #include <linux/highuid.h>
19 #include <linux/smp_lock.h>
20 #include <linux/vmalloc.h>
21 #include <linux/sched.h>
22
23 #include <linux/ncp_fs.h>
24
25 #include <asm/uaccess.h>
26
27 #include "ncplib_kernel.h"
28
29 /* maximum limit for ncp_objectname_ioctl */
30 #define NCP_OBJECT_NAME_MAX_LEN 4096
31 /* maximum limit for ncp_privatedata_ioctl */
32 #define NCP_PRIVATE_DATA_MAX_LEN 8192
33 /* maximum negotiable packet size */
34 #define NCP_PACKET_SIZE_INTERNAL 65536
35
36 static int
37 ncp_get_fs_info(struct ncp_server * server, struct file *file,
38                 struct ncp_fs_info __user *arg)
39 {
40         struct inode *inode = file->f_path.dentry->d_inode;
41         struct ncp_fs_info info;
42
43         if ((file_permission(file, MAY_WRITE) != 0)
44             && (current->uid != server->m.mounted_uid)) {
45                 return -EACCES;
46         }
47         if (copy_from_user(&info, arg, sizeof(info)))
48                 return -EFAULT;
49
50         if (info.version != NCP_GET_FS_INFO_VERSION) {
51                 DPRINTK("info.version invalid: %d\n", info.version);
52                 return -EINVAL;
53         }
54         /* TODO: info.addr = server->m.serv_addr; */
55         SET_UID(info.mounted_uid, server->m.mounted_uid);
56         info.connection         = server->connection;
57         info.buffer_size        = server->buffer_size;
58         info.volume_number      = NCP_FINFO(inode)->volNumber;
59         info.directory_id       = NCP_FINFO(inode)->DosDirNum;
60
61         if (copy_to_user(arg, &info, sizeof(info)))
62                 return -EFAULT;
63         return 0;
64 }
65
66 static int
67 ncp_get_fs_info_v2(struct ncp_server * server, struct file *file,
68                    struct ncp_fs_info_v2 __user * arg)
69 {
70         struct inode *inode = file->f_path.dentry->d_inode;
71         struct ncp_fs_info_v2 info2;
72
73         if ((file_permission(file, MAY_WRITE) != 0)
74             && (current->uid != server->m.mounted_uid)) {
75                 return -EACCES;
76         }
77         if (copy_from_user(&info2, arg, sizeof(info2)))
78                 return -EFAULT;
79
80         if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
81                 DPRINTK("info.version invalid: %d\n", info2.version);
82                 return -EINVAL;
83         }
84         info2.mounted_uid   = server->m.mounted_uid;
85         info2.connection    = server->connection;
86         info2.buffer_size   = server->buffer_size;
87         info2.volume_number = NCP_FINFO(inode)->volNumber;
88         info2.directory_id  = NCP_FINFO(inode)->DosDirNum;
89         info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
90
91         if (copy_to_user(arg, &info2, sizeof(info2)))
92                 return -EFAULT;
93         return 0;
94 }
95
96 #ifdef CONFIG_COMPAT
97 struct compat_ncp_objectname_ioctl
98 {
99         s32             auth_type;
100         u32             object_name_len;
101         compat_caddr_t  object_name;    /* an userspace data, in most cases user name */
102 };
103
104 struct compat_ncp_fs_info_v2 {
105         s32 version;
106         u32 mounted_uid;
107         u32 connection;
108         u32 buffer_size;
109
110         u32 volume_number;
111         u32 directory_id;
112
113         u32 dummy1;
114         u32 dummy2;
115         u32 dummy3;
116 };
117
118 struct compat_ncp_ioctl_request {
119         u32 function;
120         u32 size;
121         compat_caddr_t data;
122 };
123
124 struct compat_ncp_privatedata_ioctl
125 {
126         u32             len;
127         compat_caddr_t  data;           /* ~1000 for NDS */
128 };
129
130 #define NCP_IOC_GET_FS_INFO_V2_32       _IOWR('n', 4, struct compat_ncp_fs_info_v2)
131 #define NCP_IOC_NCPREQUEST_32           _IOR('n', 1, struct compat_ncp_ioctl_request)
132 #define NCP_IOC_GETOBJECTNAME_32        _IOWR('n', 9, struct compat_ncp_objectname_ioctl)
133 #define NCP_IOC_SETOBJECTNAME_32        _IOR('n', 9, struct compat_ncp_objectname_ioctl)
134 #define NCP_IOC_GETPRIVATEDATA_32       _IOWR('n', 10, struct compat_ncp_privatedata_ioctl)
135 #define NCP_IOC_SETPRIVATEDATA_32       _IOR('n', 10, struct compat_ncp_privatedata_ioctl)
136
137 static int
138 ncp_get_compat_fs_info_v2(struct ncp_server * server, struct file *file,
139                    struct compat_ncp_fs_info_v2 __user * arg)
140 {
141         struct inode *inode = file->f_path.dentry->d_inode;
142         struct compat_ncp_fs_info_v2 info2;
143
144         if ((file_permission(file, MAY_WRITE) != 0)
145             && (current->uid != server->m.mounted_uid)) {
146                 return -EACCES;
147         }
148         if (copy_from_user(&info2, arg, sizeof(info2)))
149                 return -EFAULT;
150
151         if (info2.version != NCP_GET_FS_INFO_VERSION_V2) {
152                 DPRINTK("info.version invalid: %d\n", info2.version);
153                 return -EINVAL;
154         }
155         info2.mounted_uid   = server->m.mounted_uid;
156         info2.connection    = server->connection;
157         info2.buffer_size   = server->buffer_size;
158         info2.volume_number = NCP_FINFO(inode)->volNumber;
159         info2.directory_id  = NCP_FINFO(inode)->DosDirNum;
160         info2.dummy1 = info2.dummy2 = info2.dummy3 = 0;
161
162         if (copy_to_user(arg, &info2, sizeof(info2)))
163                 return -EFAULT;
164         return 0;
165 }
166 #endif
167
168 #define NCP_IOC_GETMOUNTUID16           _IOW('n', 2, u16)
169 #define NCP_IOC_GETMOUNTUID32           _IOW('n', 2, u32)
170 #define NCP_IOC_GETMOUNTUID64           _IOW('n', 2, u64)
171
172 #ifdef CONFIG_NCPFS_NLS
173 /* Here we are select the iocharset and the codepage for NLS.
174  * Thanks Petr Vandrovec for idea and many hints.
175  */
176 static int
177 ncp_set_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
178 {
179         struct ncp_nls_ioctl user;
180         struct nls_table *codepage;
181         struct nls_table *iocharset;
182         struct nls_table *oldset_io;
183         struct nls_table *oldset_cp;
184
185         if (!capable(CAP_SYS_ADMIN))
186                 return -EACCES;
187         if (server->root_setuped)
188                 return -EBUSY;
189
190         if (copy_from_user(&user, arg, sizeof(user)))
191                 return -EFAULT;
192
193         codepage = NULL;
194         user.codepage[NCP_IOCSNAME_LEN] = 0;
195         if (!user.codepage[0] || !strcmp(user.codepage, "default"))
196                 codepage = load_nls_default();
197         else {
198                 codepage = load_nls(user.codepage);
199                 if (!codepage) {
200                         return -EBADRQC;
201                 }
202         }
203
204         iocharset = NULL;
205         user.iocharset[NCP_IOCSNAME_LEN] = 0;
206         if (!user.iocharset[0] || !strcmp(user.iocharset, "default")) {
207                 iocharset = load_nls_default();
208                 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
209         } else if (!strcmp(user.iocharset, "utf8")) {
210                 iocharset = load_nls_default();
211                 NCP_SET_FLAG(server, NCP_FLAG_UTF8);
212         } else {
213                 iocharset = load_nls(user.iocharset);
214                 if (!iocharset) {
215                         unload_nls(codepage);
216                         return -EBADRQC;
217                 }
218                 NCP_CLR_FLAG(server, NCP_FLAG_UTF8);
219         }
220
221         oldset_cp = server->nls_vol;
222         server->nls_vol = codepage;
223         oldset_io = server->nls_io;
224         server->nls_io = iocharset;
225
226         if (oldset_cp)
227                 unload_nls(oldset_cp);
228         if (oldset_io)
229                 unload_nls(oldset_io);
230
231         return 0;
232 }
233
234 static int
235 ncp_get_charsets(struct ncp_server* server, struct ncp_nls_ioctl __user *arg)
236 {
237         struct ncp_nls_ioctl user;
238         int len;
239
240         memset(&user, 0, sizeof(user));
241         if (server->nls_vol && server->nls_vol->charset) {
242                 len = strlen(server->nls_vol->charset);
243                 if (len > NCP_IOCSNAME_LEN)
244                         len = NCP_IOCSNAME_LEN;
245                 strncpy(user.codepage, server->nls_vol->charset, len);
246                 user.codepage[len] = 0;
247         }
248
249         if (NCP_IS_FLAG(server, NCP_FLAG_UTF8))
250                 strcpy(user.iocharset, "utf8");
251         else if (server->nls_io && server->nls_io->charset) {
252                 len = strlen(server->nls_io->charset);
253                 if (len > NCP_IOCSNAME_LEN)
254                         len = NCP_IOCSNAME_LEN;
255                 strncpy(user.iocharset, server->nls_io->charset, len);
256                 user.iocharset[len] = 0;
257         }
258
259         if (copy_to_user(arg, &user, sizeof(user)))
260                 return -EFAULT;
261         return 0;
262 }
263 #endif /* CONFIG_NCPFS_NLS */
264
265 static int __ncp_ioctl(struct inode *inode, struct file *filp,
266               unsigned int cmd, unsigned long arg)
267 {
268         struct ncp_server *server = NCP_SERVER(inode);
269         int result;
270         struct ncp_ioctl_request request;
271         char* bouncebuffer;
272         void __user *argp = (void __user *)arg;
273
274         switch (cmd) {
275 #ifdef CONFIG_COMPAT
276         case NCP_IOC_NCPREQUEST_32:
277 #endif
278         case NCP_IOC_NCPREQUEST:
279                 if ((file_permission(filp, MAY_WRITE) != 0)
280                     && (current->uid != server->m.mounted_uid)) {
281                         return -EACCES;
282                 }
283 #ifdef CONFIG_COMPAT
284                 if (cmd == NCP_IOC_NCPREQUEST_32) {
285                         struct compat_ncp_ioctl_request request32;
286                         if (copy_from_user(&request32, argp, sizeof(request32)))
287                                 return -EFAULT;
288                         request.function = request32.function;
289                         request.size = request32.size;
290                         request.data = compat_ptr(request32.data);
291                 } else
292 #endif
293                 if (copy_from_user(&request, argp, sizeof(request)))
294                         return -EFAULT;
295
296                 if ((request.function > 255)
297                     || (request.size >
298                   NCP_PACKET_SIZE - sizeof(struct ncp_request_header))) {
299                         return -EINVAL;
300                 }
301                 bouncebuffer = vmalloc(NCP_PACKET_SIZE_INTERNAL);
302                 if (!bouncebuffer)
303                         return -ENOMEM;
304                 if (copy_from_user(bouncebuffer, request.data, request.size)) {
305                         vfree(bouncebuffer);
306                         return -EFAULT;
307                 }
308                 ncp_lock_server(server);
309
310                 /* FIXME: We hack around in the server's structures
311                    here to be able to use ncp_request */
312
313                 server->has_subfunction = 0;
314                 server->current_size = request.size;
315                 memcpy(server->packet, bouncebuffer, request.size);
316
317                 result = ncp_request2(server, request.function, 
318                         bouncebuffer, NCP_PACKET_SIZE_INTERNAL);
319                 if (result < 0)
320                         result = -EIO;
321                 else
322                         result = server->reply_size;
323                 ncp_unlock_server(server);
324                 DPRINTK("ncp_ioctl: copy %d bytes\n",
325                         result);
326                 if (result >= 0)
327                         if (copy_to_user(request.data, bouncebuffer, result))
328                                 result = -EFAULT;
329                 vfree(bouncebuffer);
330                 return result;
331
332         case NCP_IOC_CONN_LOGGED_IN:
333
334                 if (!capable(CAP_SYS_ADMIN))
335                         return -EACCES;
336                 if (!(server->m.int_flags & NCP_IMOUNT_LOGGEDIN_POSSIBLE))
337                         return -EINVAL;
338                 if (server->root_setuped)
339                         return -EBUSY;
340                 server->root_setuped = 1;
341                 return ncp_conn_logged_in(inode->i_sb);
342
343         case NCP_IOC_GET_FS_INFO:
344                 return ncp_get_fs_info(server, filp, argp);
345
346         case NCP_IOC_GET_FS_INFO_V2:
347                 return ncp_get_fs_info_v2(server, filp, argp);
348
349 #ifdef CONFIG_COMPAT
350         case NCP_IOC_GET_FS_INFO_V2_32:
351                 return ncp_get_compat_fs_info_v2(server, filp, argp);
352 #endif
353         /* we have too many combinations of CONFIG_COMPAT,
354          * CONFIG_64BIT and CONFIG_UID16, so just handle
355          * any of the possible ioctls */
356         case NCP_IOC_GETMOUNTUID16:
357         case NCP_IOC_GETMOUNTUID32:
358         case NCP_IOC_GETMOUNTUID64:
359                 if ((file_permission(filp, MAY_READ) != 0)
360                         && (current->uid != server->m.mounted_uid)) {
361                         return -EACCES;
362                 }
363                 if (cmd == NCP_IOC_GETMOUNTUID16) {
364                         u16 uid;
365                         SET_UID(uid, server->m.mounted_uid);
366                         if (put_user(uid, (u16 __user *)argp))
367                                 return -EFAULT;
368                 } else if (cmd == NCP_IOC_GETMOUNTUID32) {
369                         if (put_user(server->m.mounted_uid,
370                                                 (u32 __user *)argp))
371                                 return -EFAULT;
372                 } else {
373                         if (put_user(server->m.mounted_uid,
374                                                 (u64 __user *)argp))
375                                 return -EFAULT;
376                 }
377                 return 0;
378
379         case NCP_IOC_GETROOT:
380                 {
381                         struct ncp_setroot_ioctl sr;
382
383                         if ((file_permission(filp, MAY_READ) != 0)
384                             && (current->uid != server->m.mounted_uid))
385                         {
386                                 return -EACCES;
387                         }
388                         if (server->m.mounted_vol[0]) {
389                                 struct dentry* dentry = inode->i_sb->s_root;
390
391                                 if (dentry) {
392                                         struct inode* inode = dentry->d_inode;
393                                 
394                                         if (inode) {
395                                                 sr.volNumber = NCP_FINFO(inode)->volNumber;
396                                                 sr.dirEntNum = NCP_FINFO(inode)->dirEntNum;
397                                                 sr.namespace = server->name_space[sr.volNumber];
398                                         } else
399                                                 DPRINTK("ncpfs: s_root->d_inode==NULL\n");
400                                 } else
401                                         DPRINTK("ncpfs: s_root==NULL\n");
402                         } else {
403                                 sr.volNumber = -1;
404                                 sr.namespace = 0;
405                                 sr.dirEntNum = 0;
406                         }
407                         if (copy_to_user(argp, &sr, sizeof(sr)))
408                                 return -EFAULT;
409                         return 0;
410                 }
411         case NCP_IOC_SETROOT:
412                 {
413                         struct ncp_setroot_ioctl sr;
414                         __u32 vnum;
415                         __le32 de;
416                         __le32 dosde;
417                         struct dentry* dentry;
418
419                         if (!capable(CAP_SYS_ADMIN))
420                         {
421                                 return -EACCES;
422                         }
423                         if (server->root_setuped) return -EBUSY;
424                         if (copy_from_user(&sr, argp, sizeof(sr)))
425                                 return -EFAULT;
426                         if (sr.volNumber < 0) {
427                                 server->m.mounted_vol[0] = 0;
428                                 vnum = NCP_NUMBER_OF_VOLUMES;
429                                 de = 0;
430                                 dosde = 0;
431                         } else if (sr.volNumber >= NCP_NUMBER_OF_VOLUMES) {
432                                 return -EINVAL;
433                         } else if (ncp_mount_subdir(server, sr.volNumber,
434                                                 sr.namespace, sr.dirEntNum,
435                                                 &vnum, &de, &dosde)) {
436                                 return -ENOENT;
437                         }
438                         
439                         dentry = inode->i_sb->s_root;
440                         server->root_setuped = 1;
441                         if (dentry) {
442                                 struct inode* inode = dentry->d_inode;
443                                 
444                                 if (inode) {
445                                         NCP_FINFO(inode)->volNumber = vnum;
446                                         NCP_FINFO(inode)->dirEntNum = de;
447                                         NCP_FINFO(inode)->DosDirNum = dosde;
448                                 } else
449                                         DPRINTK("ncpfs: s_root->d_inode==NULL\n");
450                         } else
451                                 DPRINTK("ncpfs: s_root==NULL\n");
452
453                         return 0;
454                 }
455
456 #ifdef CONFIG_NCPFS_PACKET_SIGNING      
457         case NCP_IOC_SIGN_INIT:
458                 if ((file_permission(filp, MAY_WRITE) != 0)
459                     && (current->uid != server->m.mounted_uid))
460                 {
461                         return -EACCES;
462                 }
463                 if (argp) {
464                         if (server->sign_wanted)
465                         {
466                                 struct ncp_sign_init sign;
467
468                                 if (copy_from_user(&sign, argp, sizeof(sign)))
469                                         return -EFAULT;
470                                 memcpy(server->sign_root,sign.sign_root,8);
471                                 memcpy(server->sign_last,sign.sign_last,16);
472                                 server->sign_active = 1;
473                         }
474                         /* ignore when signatures not wanted */
475                 } else {
476                         server->sign_active = 0;
477                 }
478                 return 0;               
479                 
480         case NCP_IOC_SIGN_WANTED:
481                 if ((file_permission(filp, MAY_READ) != 0)
482                     && (current->uid != server->m.mounted_uid))
483                 {
484                         return -EACCES;
485                 }
486                 
487                 if (put_user(server->sign_wanted, (int __user *)argp))
488                         return -EFAULT;
489                 return 0;
490         case NCP_IOC_SET_SIGN_WANTED:
491                 {
492                         int newstate;
493
494                         if ((file_permission(filp, MAY_WRITE) != 0)
495                             && (current->uid != server->m.mounted_uid))
496                         {
497                                 return -EACCES;
498                         }
499                         /* get only low 8 bits... */
500                         if (get_user(newstate, (unsigned char __user *)argp))
501                                 return -EFAULT;
502                         if (server->sign_active) {
503                                 /* cannot turn signatures OFF when active */
504                                 if (!newstate) return -EINVAL;
505                         } else {
506                                 server->sign_wanted = newstate != 0;
507                         }
508                         return 0;
509                 }
510
511 #endif /* CONFIG_NCPFS_PACKET_SIGNING */
512
513 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
514         case NCP_IOC_LOCKUNLOCK:
515                 if ((file_permission(filp, MAY_WRITE) != 0)
516                     && (current->uid != server->m.mounted_uid))
517                 {
518                         return -EACCES;
519                 }
520                 {
521                         struct ncp_lock_ioctl    rqdata;
522                         int result;
523
524                         if (copy_from_user(&rqdata, argp, sizeof(rqdata)))
525                                 return -EFAULT;
526                         if (rqdata.origin != 0)
527                                 return -EINVAL;
528                         /* check for cmd */
529                         switch (rqdata.cmd) {
530                                 case NCP_LOCK_EX:
531                                 case NCP_LOCK_SH:
532                                                 if (rqdata.timeout == 0)
533                                                         rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;
534                                                 else if (rqdata.timeout > NCP_LOCK_MAX_TIMEOUT)
535                                                         rqdata.timeout = NCP_LOCK_MAX_TIMEOUT;
536                                                 break;
537                                 case NCP_LOCK_LOG:
538                                                 rqdata.timeout = NCP_LOCK_DEFAULT_TIMEOUT;      /* has no effect */
539                                 case NCP_LOCK_CLEAR:
540                                                 break;
541                                 default:
542                                                 return -EINVAL;
543                         }
544                         /* locking needs both read and write access */
545                         if ((result = ncp_make_open(inode, O_RDWR)) != 0)
546                         {
547                                 return result;
548                         }
549                         result = -EIO;
550                         if (!ncp_conn_valid(server))
551                                 goto outrel;
552                         result = -EISDIR;
553                         if (!S_ISREG(inode->i_mode))
554                                 goto outrel;
555                         if (rqdata.cmd == NCP_LOCK_CLEAR)
556                         {
557                                 result = ncp_ClearPhysicalRecord(NCP_SERVER(inode),
558                                                         NCP_FINFO(inode)->file_handle, 
559                                                         rqdata.offset,
560                                                         rqdata.length);
561                                 if (result > 0) result = 0;     /* no such lock */
562                         }
563                         else
564                         {
565                                 int lockcmd;
566
567                                 switch (rqdata.cmd)
568                                 {
569                                         case NCP_LOCK_EX:  lockcmd=1; break;
570                                         case NCP_LOCK_SH:  lockcmd=3; break;
571                                         default:           lockcmd=0; break;
572                                 }
573                                 result = ncp_LogPhysicalRecord(NCP_SERVER(inode),
574                                                         NCP_FINFO(inode)->file_handle,
575                                                         lockcmd,
576                                                         rqdata.offset,
577                                                         rqdata.length,
578                                                         rqdata.timeout);
579                                 if (result > 0) result = -EAGAIN;
580                         }
581 outrel:                 
582                         ncp_inode_close(inode);
583                         return result;
584                 }
585 #endif  /* CONFIG_NCPFS_IOCTL_LOCKING */
586
587 #ifdef CONFIG_COMPAT
588         case NCP_IOC_GETOBJECTNAME_32:
589                 if (current->uid != server->m.mounted_uid) {
590                         return -EACCES;
591                 }
592                 {
593                         struct compat_ncp_objectname_ioctl user;
594                         size_t outl;
595
596                         if (copy_from_user(&user, argp, sizeof(user)))
597                                 return -EFAULT;
598                         user.auth_type = server->auth.auth_type;
599                         outl = user.object_name_len;
600                         user.object_name_len = server->auth.object_name_len;
601                         if (outl > user.object_name_len)
602                                 outl = user.object_name_len;
603                         if (outl) {
604                                 if (copy_to_user(compat_ptr(user.object_name),
605                                                  server->auth.object_name,
606                                                  outl)) return -EFAULT;
607                         }
608                         if (copy_to_user(argp, &user, sizeof(user)))
609                                 return -EFAULT;
610                         return 0;
611                 }
612 #endif
613         case NCP_IOC_GETOBJECTNAME:
614                 if (current->uid != server->m.mounted_uid) {
615                         return -EACCES;
616                 }
617                 {
618                         struct ncp_objectname_ioctl user;
619                         size_t outl;
620
621                         if (copy_from_user(&user, argp, sizeof(user)))
622                                 return -EFAULT;
623                         user.auth_type = server->auth.auth_type;
624                         outl = user.object_name_len;
625                         user.object_name_len = server->auth.object_name_len;
626                         if (outl > user.object_name_len)
627                                 outl = user.object_name_len;
628                         if (outl) {
629                                 if (copy_to_user(user.object_name,
630                                                  server->auth.object_name,
631                                                  outl)) return -EFAULT;
632                         }
633                         if (copy_to_user(argp, &user, sizeof(user)))
634                                 return -EFAULT;
635                         return 0;
636                 }
637 #ifdef CONFIG_COMPAT
638         case NCP_IOC_SETOBJECTNAME_32:
639 #endif
640         case NCP_IOC_SETOBJECTNAME:
641                 if (current->uid != server->m.mounted_uid) {
642                         return -EACCES;
643                 }
644                 {
645                         struct ncp_objectname_ioctl user;
646                         void* newname;
647                         void* oldname;
648                         size_t oldnamelen;
649                         void* oldprivate;
650                         size_t oldprivatelen;
651
652 #ifdef CONFIG_COMPAT
653                         if (cmd == NCP_IOC_SETOBJECTNAME_32) {
654                                 struct compat_ncp_objectname_ioctl user32;
655                                 if (copy_from_user(&user32, argp, sizeof(user32)))
656                                         return -EFAULT;
657                                 user.auth_type = user32.auth_type;
658                                 user.object_name_len = user32.object_name_len;
659                                 user.object_name = compat_ptr(user32.object_name);
660                         } else
661 #endif
662                         if (copy_from_user(&user, argp, sizeof(user)))
663                                 return -EFAULT;
664
665                         if (user.object_name_len > NCP_OBJECT_NAME_MAX_LEN)
666                                 return -ENOMEM;
667                         if (user.object_name_len) {
668                                 newname = kmalloc(user.object_name_len, GFP_USER);
669                                 if (!newname)
670                                         return -ENOMEM;
671                                 if (copy_from_user(newname, user.object_name, user.object_name_len)) {
672                                         kfree(newname);
673                                         return -EFAULT;
674                                 }
675                         } else {
676                                 newname = NULL;
677                         }
678                         /* enter critical section */
679                         /* maybe that kfree can sleep so do that this way */
680                         /* it is at least more SMP friendly (in future...) */
681                         oldname = server->auth.object_name;
682                         oldnamelen = server->auth.object_name_len;
683                         oldprivate = server->priv.data;
684                         oldprivatelen = server->priv.len;
685                         server->auth.auth_type = user.auth_type;
686                         server->auth.object_name_len = user.object_name_len;
687                         server->auth.object_name = newname;
688                         server->priv.len = 0;
689                         server->priv.data = NULL;
690                         /* leave critical section */
691                         kfree(oldprivate);
692                         kfree(oldname);
693                         return 0;
694                 }
695 #ifdef CONFIG_COMPAT
696         case NCP_IOC_GETPRIVATEDATA_32:
697 #endif
698         case NCP_IOC_GETPRIVATEDATA:
699                 if (current->uid != server->m.mounted_uid) {
700                         return -EACCES;
701                 }
702                 {
703                         struct ncp_privatedata_ioctl user;
704                         size_t outl;
705
706 #ifdef CONFIG_COMPAT
707                         if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
708                                 struct compat_ncp_privatedata_ioctl user32;
709                                 if (copy_from_user(&user32, argp, sizeof(user32)))
710                                         return -EFAULT;
711                                 user.len = user32.len;
712                                 user.data = compat_ptr(user32.data);
713                         } else
714 #endif
715                         if (copy_from_user(&user, argp, sizeof(user)))
716                                 return -EFAULT;
717
718                         outl = user.len;
719                         user.len = server->priv.len;
720                         if (outl > user.len) outl = user.len;
721                         if (outl) {
722                                 if (copy_to_user(user.data,
723                                                  server->priv.data,
724                                                  outl)) return -EFAULT;
725                         }
726 #ifdef CONFIG_COMPAT
727                         if (cmd == NCP_IOC_GETPRIVATEDATA_32) {
728                                 struct compat_ncp_privatedata_ioctl user32;
729                                 user32.len = user.len;
730                                 user32.data = (unsigned long) user.data;
731                                 if (copy_to_user(argp, &user32, sizeof(user32)))
732                                         return -EFAULT;
733                         } else
734 #endif
735                         if (copy_to_user(argp, &user, sizeof(user)))
736                                 return -EFAULT;
737
738                         return 0;
739                 }
740 #ifdef CONFIG_COMPAT
741         case NCP_IOC_SETPRIVATEDATA_32:
742 #endif
743         case NCP_IOC_SETPRIVATEDATA:
744                 if (current->uid != server->m.mounted_uid) {
745                         return -EACCES;
746                 }
747                 {
748                         struct ncp_privatedata_ioctl user;
749                         void* new;
750                         void* old;
751                         size_t oldlen;
752
753 #ifdef CONFIG_COMPAT
754                         if (cmd == NCP_IOC_SETPRIVATEDATA_32) {
755                                 struct compat_ncp_privatedata_ioctl user32;
756                                 if (copy_from_user(&user32, argp, sizeof(user32)))
757                                         return -EFAULT;
758                                 user.len = user32.len;
759                                 user.data = compat_ptr(user32.data);
760                         } else
761 #endif
762                         if (copy_from_user(&user, argp, sizeof(user)))
763                                 return -EFAULT;
764
765                         if (user.len > NCP_PRIVATE_DATA_MAX_LEN)
766                                 return -ENOMEM;
767                         if (user.len) {
768                                 new = kmalloc(user.len, GFP_USER);
769                                 if (!new)
770                                         return -ENOMEM;
771                                 if (copy_from_user(new, user.data, user.len)) {
772                                         kfree(new);
773                                         return -EFAULT;
774                                 }
775                         } else {
776                                 new = NULL;
777                         }
778                         /* enter critical section */
779                         old = server->priv.data;
780                         oldlen = server->priv.len;
781                         server->priv.len = user.len;
782                         server->priv.data = new;
783                         /* leave critical section */
784                         kfree(old);
785                         return 0;
786                 }
787
788 #ifdef CONFIG_NCPFS_NLS
789         case NCP_IOC_SETCHARSETS:
790                 return ncp_set_charsets(server, argp);
791                 
792         case NCP_IOC_GETCHARSETS:
793                 return ncp_get_charsets(server, argp);
794
795 #endif /* CONFIG_NCPFS_NLS */
796
797         case NCP_IOC_SETDENTRYTTL:
798                 if ((file_permission(filp, MAY_WRITE) != 0) &&
799                                  (current->uid != server->m.mounted_uid))
800                         return -EACCES;
801                 {
802                         u_int32_t user;
803
804                         if (copy_from_user(&user, argp, sizeof(user)))
805                                 return -EFAULT;
806                         /* 20 secs at most... */
807                         if (user > 20000)
808                                 return -EINVAL;
809                         user = (user * HZ) / 1000;
810                         server->dentry_ttl = user;
811                         return 0;
812                 }
813                 
814         case NCP_IOC_GETDENTRYTTL:
815                 {
816                         u_int32_t user = (server->dentry_ttl * 1000) / HZ;
817                         if (copy_to_user(argp, &user, sizeof(user)))
818                                 return -EFAULT;
819                         return 0;
820                 }
821
822         }
823         return -EINVAL;
824 }
825
826 static int ncp_ioctl_need_write(unsigned int cmd)
827 {
828         switch (cmd) {
829         case NCP_IOC_GET_FS_INFO:
830         case NCP_IOC_GET_FS_INFO_V2:
831         case NCP_IOC_NCPREQUEST:
832         case NCP_IOC_SETDENTRYTTL:
833         case NCP_IOC_SIGN_INIT:
834         case NCP_IOC_LOCKUNLOCK:
835         case NCP_IOC_SET_SIGN_WANTED:
836                 return 1;
837         case NCP_IOC_GETOBJECTNAME:
838         case NCP_IOC_SETOBJECTNAME:
839         case NCP_IOC_GETPRIVATEDATA:
840         case NCP_IOC_SETPRIVATEDATA:
841         case NCP_IOC_SETCHARSETS:
842         case NCP_IOC_GETCHARSETS:
843         case NCP_IOC_CONN_LOGGED_IN:
844         case NCP_IOC_GETDENTRYTTL:
845         case NCP_IOC_GETMOUNTUID2:
846         case NCP_IOC_SIGN_WANTED:
847         case NCP_IOC_GETROOT:
848         case NCP_IOC_SETROOT:
849                 return 0;
850         default:
851                 /* unkown IOCTL command, assume write */
852                 return 1;
853         }
854 }
855
856 int ncp_ioctl(struct inode *inode, struct file *filp,
857               unsigned int cmd, unsigned long arg)
858 {
859         int ret;
860
861         if (ncp_ioctl_need_write(cmd)) {
862                 /*
863                  * inside the ioctl(), any failures which
864                  * are because of file_permission() are
865                  * -EACCESS, so it seems consistent to keep
866                  *  that here.
867                  */
868                 if (mnt_want_write(filp->f_path.mnt))
869                         return -EACCES;
870         }
871         ret = __ncp_ioctl(inode, filp, cmd, arg);
872         if (ncp_ioctl_need_write(cmd))
873                 mnt_drop_write(filp->f_path.mnt);
874         return ret;
875 }
876
877 #ifdef CONFIG_COMPAT
878 long ncp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
879 {
880         struct inode *inode = file->f_path.dentry->d_inode;
881         int ret;
882
883         lock_kernel();
884         arg = (unsigned long) compat_ptr(arg);
885         ret = ncp_ioctl(inode, file, cmd, arg);
886         unlock_kernel();
887         return ret;
888 }
889 #endif