aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ncpfs/ncplib_kernel.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ncpfs/ncplib_kernel.c')
-rw-r--r--fs/ncpfs/ncplib_kernel.c1355
1 files changed, 1355 insertions, 0 deletions
diff --git a/fs/ncpfs/ncplib_kernel.c b/fs/ncpfs/ncplib_kernel.c
new file mode 100644
index 000000000000..e4eb5ed4bee4
--- /dev/null
+++ b/fs/ncpfs/ncplib_kernel.c
@@ -0,0 +1,1355 @@
1/*
2 * ncplib_kernel.c
3 *
4 * Copyright (C) 1995, 1996 by Volker Lendecke
5 * Modified for big endian by J.F. Chadima and David S. Miller
6 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
7 * Modified 1999 Wolfram Pienkoss for NLS
8 * Modified 2000 Ben Harris, University of Cambridge for NFS NS meta-info
9 *
10 */
11
12
13#include <linux/config.h>
14
15#include "ncplib_kernel.h"
16
17static inline void assert_server_locked(struct ncp_server *server)
18{
19 if (server->lock == 0) {
20 DPRINTK("ncpfs: server not locked!\n");
21 }
22}
23
24static void ncp_add_byte(struct ncp_server *server, __u8 x)
25{
26 assert_server_locked(server);
27 *(__u8 *) (&(server->packet[server->current_size])) = x;
28 server->current_size += 1;
29 return;
30}
31
32static void ncp_add_word(struct ncp_server *server, __le16 x)
33{
34 assert_server_locked(server);
35 put_unaligned(x, (__le16 *) (&(server->packet[server->current_size])));
36 server->current_size += 2;
37 return;
38}
39
40static void ncp_add_be16(struct ncp_server *server, __u16 x)
41{
42 assert_server_locked(server);
43 put_unaligned(cpu_to_be16(x), (__be16 *) (&(server->packet[server->current_size])));
44 server->current_size += 2;
45}
46
47static void ncp_add_dword(struct ncp_server *server, __le32 x)
48{
49 assert_server_locked(server);
50 put_unaligned(x, (__le32 *) (&(server->packet[server->current_size])));
51 server->current_size += 4;
52 return;
53}
54
55static void ncp_add_be32(struct ncp_server *server, __u32 x)
56{
57 assert_server_locked(server);
58 put_unaligned(cpu_to_be32(x), (__be32 *)(&(server->packet[server->current_size])));
59 server->current_size += 4;
60}
61
62static inline void ncp_add_dword_lh(struct ncp_server *server, __u32 x) {
63 ncp_add_dword(server, cpu_to_le32(x));
64}
65
66static void ncp_add_mem(struct ncp_server *server, const void *source, int size)
67{
68 assert_server_locked(server);
69 memcpy(&(server->packet[server->current_size]), source, size);
70 server->current_size += size;
71 return;
72}
73
74static void ncp_add_pstring(struct ncp_server *server, const char *s)
75{
76 int len = strlen(s);
77 assert_server_locked(server);
78 if (len > 255) {
79 DPRINTK("ncpfs: string too long: %s\n", s);
80 len = 255;
81 }
82 ncp_add_byte(server, len);
83 ncp_add_mem(server, s, len);
84 return;
85}
86
87static inline void ncp_init_request(struct ncp_server *server)
88{
89 ncp_lock_server(server);
90
91 server->current_size = sizeof(struct ncp_request_header);
92 server->has_subfunction = 0;
93}
94
95static inline void ncp_init_request_s(struct ncp_server *server, int subfunction)
96{
97 ncp_lock_server(server);
98
99 server->current_size = sizeof(struct ncp_request_header) + 2;
100 ncp_add_byte(server, subfunction);
101
102 server->has_subfunction = 1;
103}
104
105static inline char *
106 ncp_reply_data(struct ncp_server *server, int offset)
107{
108 return &(server->packet[sizeof(struct ncp_reply_header) + offset]);
109}
110
111static inline __u8 BVAL(void* data)
112{
113 return get_unaligned((__u8*)data);
114}
115
116static __u8
117 ncp_reply_byte(struct ncp_server *server, int offset)
118{
119 return get_unaligned((__u8 *) ncp_reply_data(server, offset));
120}
121
122static inline __u16 WVAL_LH(void* data)
123{
124 return le16_to_cpu(get_unaligned((__le16*)data));
125}
126
127static __u16
128 ncp_reply_le16(struct ncp_server *server, int offset)
129{
130 return le16_to_cpu(get_unaligned((__le16 *) ncp_reply_data(server, offset)));
131}
132
133static __u16
134 ncp_reply_be16(struct ncp_server *server, int offset)
135{
136 return be16_to_cpu(get_unaligned((__be16 *) ncp_reply_data(server, offset)));
137}
138
139static inline __u32 DVAL_LH(void* data)
140{
141 return le32_to_cpu(get_unaligned((__le32*)data));
142}
143
144static __le32
145 ncp_reply_dword(struct ncp_server *server, int offset)
146{
147 return get_unaligned((__le32 *) ncp_reply_data(server, offset));
148}
149
150static inline __u32 ncp_reply_dword_lh(struct ncp_server* server, int offset) {
151 return le32_to_cpu(ncp_reply_dword(server, offset));
152}
153
154int
155ncp_negotiate_buffersize(struct ncp_server *server, int size, int *target)
156{
157 int result;
158
159 ncp_init_request(server);
160 ncp_add_be16(server, size);
161
162 if ((result = ncp_request(server, 33)) != 0) {
163 ncp_unlock_server(server);
164 return result;
165 }
166 *target = min_t(unsigned int, ncp_reply_be16(server, 0), size);
167
168 ncp_unlock_server(server);
169 return 0;
170}
171
172
173/* options:
174 * bit 0 ipx checksum
175 * bit 1 packet signing
176 */
177int
178ncp_negotiate_size_and_options(struct ncp_server *server,
179 int size, int options, int *ret_size, int *ret_options) {
180 int result;
181
182 /* there is minimum */
183 if (size < NCP_BLOCK_SIZE) size = NCP_BLOCK_SIZE;
184
185 ncp_init_request(server);
186 ncp_add_be16(server, size);
187 ncp_add_byte(server, options);
188
189 if ((result = ncp_request(server, 0x61)) != 0)
190 {
191 ncp_unlock_server(server);
192 return result;
193 }
194
195 /* NCP over UDP returns 0 (!!!) */
196 result = ncp_reply_be16(server, 0);
197 if (result >= NCP_BLOCK_SIZE)
198 size = min(result, size);
199 *ret_size = size;
200 *ret_options = ncp_reply_byte(server, 4);
201
202 ncp_unlock_server(server);
203 return 0;
204}
205
206int ncp_get_volume_info_with_number(struct ncp_server* server,
207 int n, struct ncp_volume_info* target) {
208 int result;
209 int len;
210
211 ncp_init_request_s(server, 44);
212 ncp_add_byte(server, n);
213
214 if ((result = ncp_request(server, 22)) != 0) {
215 goto out;
216 }
217 target->total_blocks = ncp_reply_dword_lh(server, 0);
218 target->free_blocks = ncp_reply_dword_lh(server, 4);
219 target->purgeable_blocks = ncp_reply_dword_lh(server, 8);
220 target->not_yet_purgeable_blocks = ncp_reply_dword_lh(server, 12);
221 target->total_dir_entries = ncp_reply_dword_lh(server, 16);
222 target->available_dir_entries = ncp_reply_dword_lh(server, 20);
223 target->sectors_per_block = ncp_reply_byte(server, 28);
224
225 memset(&(target->volume_name), 0, sizeof(target->volume_name));
226
227 result = -EIO;
228 len = ncp_reply_byte(server, 29);
229 if (len > NCP_VOLNAME_LEN) {
230 DPRINTK("ncpfs: volume name too long: %d\n", len);
231 goto out;
232 }
233 memcpy(&(target->volume_name), ncp_reply_data(server, 30), len);
234 result = 0;
235out:
236 ncp_unlock_server(server);
237 return result;
238}
239
240int ncp_get_directory_info(struct ncp_server* server, __u8 n,
241 struct ncp_volume_info* target) {
242 int result;
243 int len;
244
245 ncp_init_request_s(server, 45);
246 ncp_add_byte(server, n);
247
248 if ((result = ncp_request(server, 22)) != 0) {
249 goto out;
250 }
251 target->total_blocks = ncp_reply_dword_lh(server, 0);
252 target->free_blocks = ncp_reply_dword_lh(server, 4);
253 target->purgeable_blocks = 0;
254 target->not_yet_purgeable_blocks = 0;
255 target->total_dir_entries = ncp_reply_dword_lh(server, 8);
256 target->available_dir_entries = ncp_reply_dword_lh(server, 12);
257 target->sectors_per_block = ncp_reply_byte(server, 20);
258
259 memset(&(target->volume_name), 0, sizeof(target->volume_name));
260
261 result = -EIO;
262 len = ncp_reply_byte(server, 21);
263 if (len > NCP_VOLNAME_LEN) {
264 DPRINTK("ncpfs: volume name too long: %d\n", len);
265 goto out;
266 }
267 memcpy(&(target->volume_name), ncp_reply_data(server, 22), len);
268 result = 0;
269out:
270 ncp_unlock_server(server);
271 return result;
272}
273
274int
275ncp_close_file(struct ncp_server *server, const char *file_id)
276{
277 int result;
278
279 ncp_init_request(server);
280 ncp_add_byte(server, 0);
281 ncp_add_mem(server, file_id, 6);
282
283 result = ncp_request(server, 66);
284 ncp_unlock_server(server);
285 return result;
286}
287
288int
289ncp_make_closed(struct inode *inode)
290{
291 int err;
292
293 err = 0;
294 down(&NCP_FINFO(inode)->open_sem);
295 if (atomic_read(&NCP_FINFO(inode)->opened) == 1) {
296 atomic_set(&NCP_FINFO(inode)->opened, 0);
297 err = ncp_close_file(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle);
298
299 if (!err)
300 PPRINTK("ncp_make_closed: volnum=%d, dirent=%u, error=%d\n",
301 NCP_FINFO(inode)->volNumber,
302 NCP_FINFO(inode)->dirEntNum, err);
303 }
304 up(&NCP_FINFO(inode)->open_sem);
305 return err;
306}
307
308static void ncp_add_handle_path(struct ncp_server *server, __u8 vol_num,
309 __le32 dir_base, int have_dir_base,
310 const char *path)
311{
312 ncp_add_byte(server, vol_num);
313 ncp_add_dword(server, dir_base);
314 if (have_dir_base != 0) {
315 ncp_add_byte(server, 1); /* dir_base */
316 } else {
317 ncp_add_byte(server, 0xff); /* no handle */
318 }
319 if (path != NULL) {
320 ncp_add_byte(server, 1); /* 1 component */
321 ncp_add_pstring(server, path);
322 } else {
323 ncp_add_byte(server, 0);
324 }
325}
326
327int ncp_dirhandle_alloc(struct ncp_server* server, __u8 volnum, __le32 dirent,
328 __u8* dirhandle) {
329 int result;
330
331 ncp_init_request(server);
332 ncp_add_byte(server, 12); /* subfunction */
333 ncp_add_byte(server, NW_NS_DOS);
334 ncp_add_byte(server, 0);
335 ncp_add_word(server, 0);
336 ncp_add_handle_path(server, volnum, dirent, 1, NULL);
337 if ((result = ncp_request(server, 87)) == 0) {
338 *dirhandle = ncp_reply_byte(server, 0);
339 }
340 ncp_unlock_server(server);
341 return result;
342}
343
344int ncp_dirhandle_free(struct ncp_server* server, __u8 dirhandle) {
345 int result;
346
347 ncp_init_request_s(server, 20);
348 ncp_add_byte(server, dirhandle);
349 result = ncp_request(server, 22);
350 ncp_unlock_server(server);
351 return result;
352}
353
354void ncp_extract_file_info(void *structure, struct nw_info_struct *target)
355{
356 __u8 *name_len;
357 const int info_struct_size = offsetof(struct nw_info_struct, nameLen);
358
359 memcpy(target, structure, info_struct_size);
360 name_len = structure + info_struct_size;
361 target->nameLen = *name_len;
362 memcpy(target->entryName, name_len + 1, *name_len);
363 target->entryName[*name_len] = '\0';
364 target->volNumber = le32_to_cpu(target->volNumber);
365 return;
366}
367
368#ifdef CONFIG_NCPFS_NFS_NS
369static inline void ncp_extract_nfs_info(unsigned char *structure,
370 struct nw_nfs_info *target)
371{
372 target->mode = DVAL_LH(structure);
373 target->rdev = DVAL_LH(structure + 8);
374}
375#endif
376
377int ncp_obtain_nfs_info(struct ncp_server *server,
378 struct nw_info_struct *target)
379
380{
381 int result = 0;
382#ifdef CONFIG_NCPFS_NFS_NS
383 __u32 volnum = target->volNumber;
384
385 if (ncp_is_nfs_extras(server, volnum)) {
386 ncp_init_request(server);
387 ncp_add_byte(server, 19); /* subfunction */
388 ncp_add_byte(server, server->name_space[volnum]);
389 ncp_add_byte(server, NW_NS_NFS);
390 ncp_add_byte(server, 0);
391 ncp_add_byte(server, volnum);
392 ncp_add_dword(server, target->dirEntNum);
393 /* We must retrieve both nlinks and rdev, otherwise some server versions
394 report zeroes instead of valid data */
395 ncp_add_dword_lh(server, NSIBM_NFS_MODE | NSIBM_NFS_NLINKS | NSIBM_NFS_RDEV);
396
397 if ((result = ncp_request(server, 87)) == 0) {
398 ncp_extract_nfs_info(ncp_reply_data(server, 0), &target->nfs);
399 DPRINTK(KERN_DEBUG
400 "ncp_obtain_nfs_info: (%s) mode=0%o, rdev=0x%x\n",
401 target->entryName, target->nfs.mode,
402 target->nfs.rdev);
403 } else {
404 target->nfs.mode = 0;
405 target->nfs.rdev = 0;
406 }
407 ncp_unlock_server(server);
408
409 } else
410#endif
411 {
412 target->nfs.mode = 0;
413 target->nfs.rdev = 0;
414 }
415 return result;
416}
417
418/*
419 * Returns information for a (one-component) name relative to
420 * the specified directory.
421 */
422int ncp_obtain_info(struct ncp_server *server, struct inode *dir, char *path,
423 struct nw_info_struct *target)
424{
425 __u8 volnum = NCP_FINFO(dir)->volNumber;
426 __le32 dirent = NCP_FINFO(dir)->dirEntNum;
427 int result;
428
429 if (target == NULL) {
430 printk(KERN_ERR "ncp_obtain_info: invalid call\n");
431 return -EINVAL;
432 }
433 ncp_init_request(server);
434 ncp_add_byte(server, 6); /* subfunction */
435 ncp_add_byte(server, server->name_space[volnum]);
436 ncp_add_byte(server, server->name_space[volnum]); /* N.B. twice ?? */
437 ncp_add_word(server, cpu_to_le16(0x8006)); /* get all */
438 ncp_add_dword(server, RIM_ALL);
439 ncp_add_handle_path(server, volnum, dirent, 1, path);
440
441 if ((result = ncp_request(server, 87)) != 0)
442 goto out;
443 ncp_extract_file_info(ncp_reply_data(server, 0), target);
444 ncp_unlock_server(server);
445
446 result = ncp_obtain_nfs_info(server, target);
447 return result;
448
449out:
450 ncp_unlock_server(server);
451 return result;
452}
453
454#ifdef CONFIG_NCPFS_NFS_NS
455static int
456ncp_obtain_DOS_dir_base(struct ncp_server *server,
457 __u8 volnum, __le32 dirent,
458 char *path, /* At most 1 component */
459 __le32 *DOS_dir_base)
460{
461 int result;
462
463 ncp_init_request(server);
464 ncp_add_byte(server, 6); /* subfunction */
465 ncp_add_byte(server, server->name_space[volnum]);
466 ncp_add_byte(server, server->name_space[volnum]);
467 ncp_add_word(server, cpu_to_le16(0x8006)); /* get all */
468 ncp_add_dword(server, RIM_DIRECTORY);
469 ncp_add_handle_path(server, volnum, dirent, 1, path);
470
471 if ((result = ncp_request(server, 87)) == 0)
472 {
473 if (DOS_dir_base) *DOS_dir_base=ncp_reply_dword(server, 0x34);
474 }
475 ncp_unlock_server(server);
476 return result;
477}
478#endif /* CONFIG_NCPFS_NFS_NS */
479
480static inline int
481ncp_get_known_namespace(struct ncp_server *server, __u8 volume)
482{
483#if defined(CONFIG_NCPFS_OS2_NS) || defined(CONFIG_NCPFS_NFS_NS)
484 int result;
485 __u8 *namespace;
486 __u16 no_namespaces;
487
488 ncp_init_request(server);
489 ncp_add_byte(server, 24); /* Subfunction: Get Name Spaces Loaded */
490 ncp_add_word(server, 0);
491 ncp_add_byte(server, volume);
492
493 if ((result = ncp_request(server, 87)) != 0) {
494 ncp_unlock_server(server);
495 return NW_NS_DOS; /* not result ?? */
496 }
497
498 result = NW_NS_DOS;
499 no_namespaces = ncp_reply_le16(server, 0);
500 namespace = ncp_reply_data(server, 2);
501
502 while (no_namespaces > 0) {
503 DPRINTK("get_namespaces: found %d on %d\n", *namespace, volume);
504
505#ifdef CONFIG_NCPFS_NFS_NS
506 if ((*namespace == NW_NS_NFS) && !(server->m.flags&NCP_MOUNT_NO_NFS))
507 {
508 result = NW_NS_NFS;
509 break;
510 }
511#endif /* CONFIG_NCPFS_NFS_NS */
512#ifdef CONFIG_NCPFS_OS2_NS
513 if ((*namespace == NW_NS_OS2) && !(server->m.flags&NCP_MOUNT_NO_OS2))
514 {
515 result = NW_NS_OS2;
516 }
517#endif /* CONFIG_NCPFS_OS2_NS */
518 namespace += 1;
519 no_namespaces -= 1;
520 }
521 ncp_unlock_server(server);
522 return result;
523#else /* neither OS2 nor NFS - only DOS */
524 return NW_NS_DOS;
525#endif /* defined(CONFIG_NCPFS_OS2_NS) || defined(CONFIG_NCPFS_NFS_NS) */
526}
527
528static int
529ncp_ObtainSpecificDirBase(struct ncp_server *server,
530 __u8 nsSrc, __u8 nsDst, __u8 vol_num, __le32 dir_base,
531 char *path, /* At most 1 component */
532 __le32 *dirEntNum, __le32 *DosDirNum)
533{
534 int result;
535
536 ncp_init_request(server);
537 ncp_add_byte(server, 6); /* subfunction */
538 ncp_add_byte(server, nsSrc);
539 ncp_add_byte(server, nsDst);
540 ncp_add_word(server, cpu_to_le16(0x8006)); /* get all */
541 ncp_add_dword(server, RIM_ALL);
542 ncp_add_handle_path(server, vol_num, dir_base, 1, path);
543
544 if ((result = ncp_request(server, 87)) != 0)
545 {
546 ncp_unlock_server(server);
547 return result;
548 }
549
550 if (dirEntNum)
551 *dirEntNum = ncp_reply_dword(server, 0x30);
552 if (DosDirNum)
553 *DosDirNum = ncp_reply_dword(server, 0x34);
554 ncp_unlock_server(server);
555 return 0;
556}
557
558int
559ncp_mount_subdir(struct ncp_server *server,
560 __u8 volNumber, __u8 srcNS, __le32 dirEntNum,
561 __u32* volume, __le32* newDirEnt, __le32* newDosEnt)
562{
563 int dstNS;
564 int result;
565
566 dstNS = ncp_get_known_namespace(server, volNumber);
567 if ((result = ncp_ObtainSpecificDirBase(server, srcNS, dstNS, volNumber,
568 dirEntNum, NULL, newDirEnt, newDosEnt)) != 0)
569 {
570 return result;
571 }
572 server->name_space[volNumber] = dstNS;
573 *volume = volNumber;
574 server->m.mounted_vol[1] = 0;
575 server->m.mounted_vol[0] = 'X';
576 return 0;
577}
578
579int
580ncp_get_volume_root(struct ncp_server *server, const char *volname,
581 __u32* volume, __le32* dirent, __le32* dosdirent)
582{
583 int result;
584 __u8 volnum;
585
586 DPRINTK("ncp_get_volume_root: looking up vol %s\n", volname);
587
588 ncp_init_request(server);
589 ncp_add_byte(server, 22); /* Subfunction: Generate dir handle */
590 ncp_add_byte(server, 0); /* DOS namespace */
591 ncp_add_byte(server, 0); /* reserved */
592 ncp_add_byte(server, 0); /* reserved */
593 ncp_add_byte(server, 0); /* reserved */
594
595 ncp_add_byte(server, 0); /* faked volume number */
596 ncp_add_dword(server, 0); /* faked dir_base */
597 ncp_add_byte(server, 0xff); /* Don't have a dir_base */
598 ncp_add_byte(server, 1); /* 1 path component */
599 ncp_add_pstring(server, volname);
600
601 if ((result = ncp_request(server, 87)) != 0) {
602 ncp_unlock_server(server);
603 return result;
604 }
605 *dirent = *dosdirent = ncp_reply_dword(server, 4);
606 volnum = ncp_reply_byte(server, 8);
607 ncp_unlock_server(server);
608 *volume = volnum;
609
610 server->name_space[volnum] = ncp_get_known_namespace(server, volnum);
611
612 DPRINTK("lookup_vol: namespace[%d] = %d\n",
613 volnum, server->name_space[volnum]);
614
615 return 0;
616}
617
618int
619ncp_lookup_volume(struct ncp_server *server, const char *volname,
620 struct nw_info_struct *target)
621{
622 int result;
623
624 memset(target, 0, sizeof(*target));
625 result = ncp_get_volume_root(server, volname,
626 &target->volNumber, &target->dirEntNum, &target->DosDirNum);
627 if (result) {
628 return result;
629 }
630 target->nameLen = strlen(volname);
631 memcpy(target->entryName, volname, target->nameLen+1);
632 target->attributes = aDIR;
633 /* set dates to Jan 1, 1986 00:00 */
634 target->creationTime = target->modifyTime = cpu_to_le16(0x0000);
635 target->creationDate = target->modifyDate = target->lastAccessDate = cpu_to_le16(0x0C21);
636 target->nfs.mode = 0;
637 return 0;
638}
639
640int ncp_modify_file_or_subdir_dos_info_path(struct ncp_server *server,
641 struct inode *dir,
642 const char *path,
643 __le32 info_mask,
644 const struct nw_modify_dos_info *info)
645{
646 __u8 volnum = NCP_FINFO(dir)->volNumber;
647 __le32 dirent = NCP_FINFO(dir)->dirEntNum;
648 int result;
649
650 ncp_init_request(server);
651 ncp_add_byte(server, 7); /* subfunction */
652 ncp_add_byte(server, server->name_space[volnum]);
653 ncp_add_byte(server, 0); /* reserved */
654 ncp_add_word(server, cpu_to_le16(0x8006)); /* search attribs: all */
655
656 ncp_add_dword(server, info_mask);
657 ncp_add_mem(server, info, sizeof(*info));
658 ncp_add_handle_path(server, volnum, dirent, 1, path);
659
660 result = ncp_request(server, 87);
661 ncp_unlock_server(server);
662 return result;
663}
664
665int ncp_modify_file_or_subdir_dos_info(struct ncp_server *server,
666 struct inode *dir,
667 __le32 info_mask,
668 const struct nw_modify_dos_info *info)
669{
670 return ncp_modify_file_or_subdir_dos_info_path(server, dir, NULL,
671 info_mask, info);
672}
673
674#ifdef CONFIG_NCPFS_NFS_NS
675int ncp_modify_nfs_info(struct ncp_server *server, __u8 volnum, __le32 dirent,
676 __u32 mode, __u32 rdev)
677
678{
679 int result = 0;
680
681 if (server->name_space[volnum] == NW_NS_NFS) {
682 ncp_init_request(server);
683 ncp_add_byte(server, 25); /* subfunction */
684 ncp_add_byte(server, server->name_space[volnum]);
685 ncp_add_byte(server, NW_NS_NFS);
686 ncp_add_byte(server, volnum);
687 ncp_add_dword(server, dirent);
688 /* we must always operate on both nlinks and rdev, otherwise
689 rdev is not set */
690 ncp_add_dword_lh(server, NSIBM_NFS_MODE | NSIBM_NFS_NLINKS | NSIBM_NFS_RDEV);
691 ncp_add_dword_lh(server, mode);
692 ncp_add_dword_lh(server, 1); /* nlinks */
693 ncp_add_dword_lh(server, rdev);
694 result = ncp_request(server, 87);
695 ncp_unlock_server(server);
696 }
697 return result;
698}
699#endif
700
701
702static int
703ncp_DeleteNSEntry(struct ncp_server *server,
704 __u8 have_dir_base, __u8 volnum, __le32 dirent,
705 char* name, __u8 ns, __le16 attr)
706{
707 int result;
708
709 ncp_init_request(server);
710 ncp_add_byte(server, 8); /* subfunction */
711 ncp_add_byte(server, ns);
712 ncp_add_byte(server, 0); /* reserved */
713 ncp_add_word(server, attr); /* search attribs: all */
714 ncp_add_handle_path(server, volnum, dirent, have_dir_base, name);
715
716 result = ncp_request(server, 87);
717 ncp_unlock_server(server);
718 return result;
719}
720
721int
722ncp_del_file_or_subdir2(struct ncp_server *server,
723 struct dentry *dentry)
724{
725 struct inode *inode = dentry->d_inode;
726 __u8 volnum;
727 __le32 dirent;
728
729 if (!inode) {
730#ifdef CONFIG_NCPFS_DEBUGDENTRY
731 PRINTK("ncpfs: ncpdel2: dentry->d_inode == NULL\n");
732#endif
733 return 0xFF; /* Any error */
734 }
735 volnum = NCP_FINFO(inode)->volNumber;
736 dirent = NCP_FINFO(inode)->DosDirNum;
737 return ncp_DeleteNSEntry(server, 1, volnum, dirent, NULL, NW_NS_DOS, cpu_to_le16(0x8006));
738}
739
740int
741ncp_del_file_or_subdir(struct ncp_server *server,
742 struct inode *dir, char *name)
743{
744 __u8 volnum = NCP_FINFO(dir)->volNumber;
745 __le32 dirent = NCP_FINFO(dir)->dirEntNum;
746
747#ifdef CONFIG_NCPFS_NFS_NS
748 if (server->name_space[volnum]==NW_NS_NFS)
749 {
750 int result;
751
752 result=ncp_obtain_DOS_dir_base(server, volnum, dirent, name, &dirent);
753 if (result) return result;
754 return ncp_DeleteNSEntry(server, 1, volnum, dirent, NULL, NW_NS_DOS, cpu_to_le16(0x8006));
755 }
756 else
757#endif /* CONFIG_NCPFS_NFS_NS */
758 return ncp_DeleteNSEntry(server, 1, volnum, dirent, name, server->name_space[volnum], cpu_to_le16(0x8006));
759}
760
761static inline void ConvertToNWfromDWORD(__u16 v0, __u16 v1, __u8 ret[6])
762{
763 __le16 *dest = (__le16 *) ret;
764 dest[1] = cpu_to_le16(v0);
765 dest[2] = cpu_to_le16(v1);
766 dest[0] = cpu_to_le16(v0 + 1);
767 return;
768}
769
770/* If both dir and name are NULL, then in target there's already a
771 looked-up entry that wants to be opened. */
772int ncp_open_create_file_or_subdir(struct ncp_server *server,
773 struct inode *dir, char *name,
774 int open_create_mode,
775 __le32 create_attributes,
776 __le16 desired_acc_rights,
777 struct ncp_entry_info *target)
778{
779 __le16 search_attribs = cpu_to_le16(0x0006);
780 __u8 volnum;
781 __le32 dirent;
782 int result;
783
784 volnum = NCP_FINFO(dir)->volNumber;
785 dirent = NCP_FINFO(dir)->dirEntNum;
786
787 if ((create_attributes & aDIR) != 0) {
788 search_attribs |= cpu_to_le16(0x8000);
789 }
790 ncp_init_request(server);
791 ncp_add_byte(server, 1); /* subfunction */
792 ncp_add_byte(server, server->name_space[volnum]);
793 ncp_add_byte(server, open_create_mode);
794 ncp_add_word(server, search_attribs);
795 ncp_add_dword(server, RIM_ALL);
796 ncp_add_dword(server, create_attributes);
797 /* The desired acc rights seem to be the inherited rights mask
798 for directories */
799 ncp_add_word(server, desired_acc_rights);
800 ncp_add_handle_path(server, volnum, dirent, 1, name);
801
802 if ((result = ncp_request(server, 87)) != 0)
803 goto out;
804 if (!(create_attributes & aDIR))
805 target->opened = 1;
806
807 /* in target there's a new finfo to fill */
808 ncp_extract_file_info(ncp_reply_data(server, 6), &(target->i));
809 target->volume = target->i.volNumber;
810 ConvertToNWfromDWORD(ncp_reply_le16(server, 0),
811 ncp_reply_le16(server, 2),
812 target->file_handle);
813
814 ncp_unlock_server(server);
815
816 (void)ncp_obtain_nfs_info(server, &(target->i));
817 return 0;
818
819out:
820 ncp_unlock_server(server);
821 return result;
822}
823
824int
825ncp_initialize_search(struct ncp_server *server, struct inode *dir,
826 struct nw_search_sequence *target)
827{
828 __u8 volnum = NCP_FINFO(dir)->volNumber;
829 __le32 dirent = NCP_FINFO(dir)->dirEntNum;
830 int result;
831
832 ncp_init_request(server);
833 ncp_add_byte(server, 2); /* subfunction */
834 ncp_add_byte(server, server->name_space[volnum]);
835 ncp_add_byte(server, 0); /* reserved */
836 ncp_add_handle_path(server, volnum, dirent, 1, NULL);
837
838 result = ncp_request(server, 87);
839 if (result)
840 goto out;
841 memcpy(target, ncp_reply_data(server, 0), sizeof(*target));
842
843out:
844 ncp_unlock_server(server);
845 return result;
846}
847
848/* Search for everything */
849int ncp_search_for_file_or_subdir(struct ncp_server *server,
850 struct nw_search_sequence *seq,
851 struct nw_info_struct *target)
852{
853 int result;
854
855 ncp_init_request(server);
856 ncp_add_byte(server, 3); /* subfunction */
857 ncp_add_byte(server, server->name_space[seq->volNumber]);
858 ncp_add_byte(server, 0); /* data stream (???) */
859 ncp_add_word(server, cpu_to_le16(0x8006)); /* Search attribs */
860 ncp_add_dword(server, RIM_ALL); /* return info mask */
861 ncp_add_mem(server, seq, 9);
862#ifdef CONFIG_NCPFS_NFS_NS
863 if (server->name_space[seq->volNumber] == NW_NS_NFS) {
864 ncp_add_byte(server, 0); /* 0 byte pattern */
865 } else
866#endif
867 {
868 ncp_add_byte(server, 2); /* 2 byte pattern */
869 ncp_add_byte(server, 0xff); /* following is a wildcard */
870 ncp_add_byte(server, '*');
871 }
872
873 if ((result = ncp_request(server, 87)) != 0)
874 goto out;
875 memcpy(seq, ncp_reply_data(server, 0), sizeof(*seq));
876 ncp_extract_file_info(ncp_reply_data(server, 10), target);
877
878 ncp_unlock_server(server);
879
880 result = ncp_obtain_nfs_info(server, target);
881 return result;
882
883out:
884 ncp_unlock_server(server);
885 return result;
886}
887
888int ncp_search_for_fileset(struct ncp_server *server,
889 struct nw_search_sequence *seq,
890 int* more,
891 int* cnt,
892 char* buffer,
893 size_t bufsize,
894 char** rbuf,
895 size_t* rsize)
896{
897 int result;
898
899 ncp_init_request(server);
900 ncp_add_byte(server, 20);
901 ncp_add_byte(server, server->name_space[seq->volNumber]);
902 ncp_add_byte(server, 0); /* datastream */
903 ncp_add_word(server, cpu_to_le16(0x8006));
904 ncp_add_dword(server, RIM_ALL);
905 ncp_add_word(server, cpu_to_le16(32767)); /* max returned items */
906 ncp_add_mem(server, seq, 9);
907#ifdef CONFIG_NCPFS_NFS_NS
908 if (server->name_space[seq->volNumber] == NW_NS_NFS) {
909 ncp_add_byte(server, 0); /* 0 byte pattern */
910 } else
911#endif
912 {
913 ncp_add_byte(server, 2); /* 2 byte pattern */
914 ncp_add_byte(server, 0xff); /* following is a wildcard */
915 ncp_add_byte(server, '*');
916 }
917 result = ncp_request2(server, 87, buffer, bufsize);
918 if (result) {
919 ncp_unlock_server(server);
920 return result;
921 }
922 if (server->ncp_reply_size < 12) {
923 ncp_unlock_server(server);
924 return 0xFF;
925 }
926 *rsize = server->ncp_reply_size - 12;
927 ncp_unlock_server(server);
928 buffer = buffer + sizeof(struct ncp_reply_header);
929 *rbuf = buffer + 12;
930 *cnt = WVAL_LH(buffer + 10);
931 *more = BVAL(buffer + 9);
932 memcpy(seq, buffer, 9);
933 return 0;
934}
935
936static int
937ncp_RenameNSEntry(struct ncp_server *server,
938 struct inode *old_dir, char *old_name, __le16 old_type,
939 struct inode *new_dir, char *new_name)
940{
941 int result = -EINVAL;
942
943 if ((old_dir == NULL) || (old_name == NULL) ||
944 (new_dir == NULL) || (new_name == NULL))
945 goto out;
946
947 ncp_init_request(server);
948 ncp_add_byte(server, 4); /* subfunction */
949 ncp_add_byte(server, server->name_space[NCP_FINFO(old_dir)->volNumber]);
950 ncp_add_byte(server, 1); /* rename flag */
951 ncp_add_word(server, old_type); /* search attributes */
952
953 /* source Handle Path */
954 ncp_add_byte(server, NCP_FINFO(old_dir)->volNumber);
955 ncp_add_dword(server, NCP_FINFO(old_dir)->dirEntNum);
956 ncp_add_byte(server, 1);
957 ncp_add_byte(server, 1); /* 1 source component */
958
959 /* dest Handle Path */
960 ncp_add_byte(server, NCP_FINFO(new_dir)->volNumber);
961 ncp_add_dword(server, NCP_FINFO(new_dir)->dirEntNum);
962 ncp_add_byte(server, 1);
963 ncp_add_byte(server, 1); /* 1 destination component */
964
965 /* source path string */
966 ncp_add_pstring(server, old_name);
967 /* dest path string */
968 ncp_add_pstring(server, new_name);
969
970 result = ncp_request(server, 87);
971 ncp_unlock_server(server);
972out:
973 return result;
974}
975
976int ncp_ren_or_mov_file_or_subdir(struct ncp_server *server,
977 struct inode *old_dir, char *old_name,
978 struct inode *new_dir, char *new_name)
979{
980 int result;
981 __le16 old_type = cpu_to_le16(0x06);
982
983/* If somebody can do it atomic, call me... vandrove@vc.cvut.cz */
984 result = ncp_RenameNSEntry(server, old_dir, old_name, old_type,
985 new_dir, new_name);
986 if (result == 0xFF) /* File Not Found, try directory */
987 {
988 old_type = cpu_to_le16(0x16);
989 result = ncp_RenameNSEntry(server, old_dir, old_name, old_type,
990 new_dir, new_name);
991 }
992 if (result != 0x92) return result; /* All except NO_FILES_RENAMED */
993 result = ncp_del_file_or_subdir(server, new_dir, new_name);
994 if (result != 0) return -EACCES;
995 result = ncp_RenameNSEntry(server, old_dir, old_name, old_type,
996 new_dir, new_name);
997 return result;
998}
999
1000
1001/* We have to transfer to/from user space */
1002int
1003ncp_read_kernel(struct ncp_server *server, const char *file_id,
1004 __u32 offset, __u16 to_read, char *target, int *bytes_read)
1005{
1006 char *source;
1007 int result;
1008
1009 ncp_init_request(server);
1010 ncp_add_byte(server, 0);
1011 ncp_add_mem(server, file_id, 6);
1012 ncp_add_be32(server, offset);
1013 ncp_add_be16(server, to_read);
1014
1015 if ((result = ncp_request(server, 72)) != 0) {
1016 goto out;
1017 }
1018 *bytes_read = ncp_reply_be16(server, 0);
1019 source = ncp_reply_data(server, 2 + (offset & 1));
1020
1021 memcpy(target, source, *bytes_read);
1022out:
1023 ncp_unlock_server(server);
1024 return result;
1025}
1026
1027/* There is a problem... egrep and some other silly tools do:
1028 x = mmap(NULL, MAP_PRIVATE, PROT_READ|PROT_WRITE, <ncpfs fd>, 32768);
1029 read(<ncpfs fd>, x, 32768);
1030 Now copying read result by copy_to_user causes pagefault. This pagefault
1031 could not be handled because of server was locked due to read. So we have
1032 to use temporary buffer. So ncp_unlock_server must be done before
1033 copy_to_user (and for write, copy_from_user must be done before
1034 ncp_init_request... same applies for send raw packet ioctl). Because of
1035 file is normally read in bigger chunks, caller provides kmalloced
1036 (vmalloced) chunk of memory with size >= to_read...
1037 */
1038int
1039ncp_read_bounce(struct ncp_server *server, const char *file_id,
1040 __u32 offset, __u16 to_read, char __user *target, int *bytes_read,
1041 void* bounce, __u32 bufsize)
1042{
1043 int result;
1044
1045 ncp_init_request(server);
1046 ncp_add_byte(server, 0);
1047 ncp_add_mem(server, file_id, 6);
1048 ncp_add_be32(server, offset);
1049 ncp_add_be16(server, to_read);
1050 result = ncp_request2(server, 72, bounce, bufsize);
1051 ncp_unlock_server(server);
1052 if (!result) {
1053 int len = be16_to_cpu(get_unaligned((__be16*)((char*)bounce +
1054 sizeof(struct ncp_reply_header))));
1055 result = -EIO;
1056 if (len <= to_read) {
1057 char* source;
1058
1059 source = (char*)bounce +
1060 sizeof(struct ncp_reply_header) + 2 +
1061 (offset & 1);
1062 *bytes_read = len;
1063 result = 0;
1064 if (copy_to_user(target, source, len))
1065 result = -EFAULT;
1066 }
1067 }
1068 return result;
1069}
1070
1071int
1072ncp_write_kernel(struct ncp_server *server, const char *file_id,
1073 __u32 offset, __u16 to_write,
1074 const char *source, int *bytes_written)
1075{
1076 int result;
1077
1078 ncp_init_request(server);
1079 ncp_add_byte(server, 0);
1080 ncp_add_mem(server, file_id, 6);
1081 ncp_add_be32(server, offset);
1082 ncp_add_be16(server, to_write);
1083 ncp_add_mem(server, source, to_write);
1084
1085 if ((result = ncp_request(server, 73)) == 0)
1086 *bytes_written = to_write;
1087 ncp_unlock_server(server);
1088 return result;
1089}
1090
1091#ifdef CONFIG_NCPFS_IOCTL_LOCKING
1092int
1093ncp_LogPhysicalRecord(struct ncp_server *server, const char *file_id,
1094 __u8 locktype, __u32 offset, __u32 length, __u16 timeout)
1095{
1096 int result;
1097
1098 ncp_init_request(server);
1099 ncp_add_byte(server, locktype);
1100 ncp_add_mem(server, file_id, 6);
1101 ncp_add_be32(server, offset);
1102 ncp_add_be32(server, length);
1103 ncp_add_be16(server, timeout);
1104
1105 if ((result = ncp_request(server, 0x1A)) != 0)
1106 {
1107 ncp_unlock_server(server);
1108 return result;
1109 }
1110 ncp_unlock_server(server);
1111 return 0;
1112}
1113
1114int
1115ncp_ClearPhysicalRecord(struct ncp_server *server, const char *file_id,
1116 __u32 offset, __u32 length)
1117{
1118 int result;
1119
1120 ncp_init_request(server);
1121 ncp_add_byte(server, 0); /* who knows... lanalyzer says that */
1122 ncp_add_mem(server, file_id, 6);
1123 ncp_add_be32(server, offset);
1124 ncp_add_be32(server, length);
1125
1126 if ((result = ncp_request(server, 0x1E)) != 0)
1127 {
1128 ncp_unlock_server(server);
1129 return result;
1130 }
1131 ncp_unlock_server(server);
1132 return 0;
1133}
1134#endif /* CONFIG_NCPFS_IOCTL_LOCKING */
1135
1136#ifdef CONFIG_NCPFS_NLS
1137/* This are the NLS conversion routines with inspirations and code parts
1138 * from the vfat file system and hints from Petr Vandrovec.
1139 */
1140
1141int
1142ncp__io2vol(struct ncp_server *server, unsigned char *vname, unsigned int *vlen,
1143 const unsigned char *iname, unsigned int ilen, int cc)
1144{
1145 struct nls_table *in = server->nls_io;
1146 struct nls_table *out = server->nls_vol;
1147 unsigned char *vname_start;
1148 unsigned char *vname_end;
1149 const unsigned char *iname_end;
1150
1151 iname_end = iname + ilen;
1152 vname_start = vname;
1153 vname_end = vname + *vlen - 1;
1154
1155 while (iname < iname_end) {
1156 int chl;
1157 wchar_t ec;
1158
1159 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8)) {
1160 int k;
1161
1162 k = utf8_mbtowc(&ec, iname, iname_end - iname);
1163 if (k < 0)
1164 return -EINVAL;
1165 iname += k;
1166 } else {
1167 if (*iname == NCP_ESC) {
1168 int k;
1169
1170 if (iname_end - iname < 5)
1171 goto nospec;
1172
1173 ec = 0;
1174 for (k = 1; k < 5; k++) {
1175 unsigned char nc;
1176
1177 nc = iname[k] - '0';
1178 if (nc >= 10) {
1179 nc -= 'A' - '0' - 10;
1180 if ((nc < 10) || (nc > 15)) {
1181 goto nospec;
1182 }
1183 }
1184 ec = (ec << 4) | nc;
1185 }
1186 iname += 5;
1187 } else {
1188nospec:;
1189 if ( (chl = in->char2uni(iname, iname_end - iname, &ec)) < 0)
1190 return chl;
1191 iname += chl;
1192 }
1193 }
1194
1195 /* unitoupper should be here! */
1196
1197 chl = out->uni2char(ec, vname, vname_end - vname);
1198 if (chl < 0)
1199 return chl;
1200
1201 /* this is wrong... */
1202 if (cc) {
1203 int chi;
1204
1205 for (chi = 0; chi < chl; chi++){
1206 vname[chi] = ncp_toupper(out, vname[chi]);
1207 }
1208 }
1209 vname += chl;
1210 }
1211
1212 *vname = 0;
1213 *vlen = vname - vname_start;
1214 return 0;
1215}
1216
1217int
1218ncp__vol2io(struct ncp_server *server, unsigned char *iname, unsigned int *ilen,
1219 const unsigned char *vname, unsigned int vlen, int cc)
1220{
1221 struct nls_table *in = server->nls_vol;
1222 struct nls_table *out = server->nls_io;
1223 const unsigned char *vname_end;
1224 unsigned char *iname_start;
1225 unsigned char *iname_end;
1226 unsigned char *vname_cc;
1227 int err;
1228
1229 vname_cc = NULL;
1230
1231 if (cc) {
1232 int i;
1233
1234 /* this is wrong! */
1235 vname_cc = kmalloc(vlen, GFP_KERNEL);
1236 if (!vname_cc)
1237 return -ENOMEM;
1238 for (i = 0; i < vlen; i++)
1239 vname_cc[i] = ncp_tolower(in, vname[i]);
1240 vname = vname_cc;
1241 }
1242
1243 iname_start = iname;
1244 iname_end = iname + *ilen - 1;
1245 vname_end = vname + vlen;
1246
1247 while (vname < vname_end) {
1248 wchar_t ec;
1249 int chl;
1250
1251 if ( (chl = in->char2uni(vname, vname_end - vname, &ec)) < 0) {
1252 err = chl;
1253 goto quit;
1254 }
1255 vname += chl;
1256
1257 /* unitolower should be here! */
1258
1259 if (NCP_IS_FLAG(server, NCP_FLAG_UTF8)) {
1260 int k;
1261
1262 k = utf8_wctomb(iname, ec, iname_end - iname);
1263 if (k < 0) {
1264 err = -ENAMETOOLONG;
1265 goto quit;
1266 }
1267 iname += k;
1268 } else {
1269 if ( (chl = out->uni2char(ec, iname, iname_end - iname)) >= 0) {
1270 iname += chl;
1271 } else {
1272 int k;
1273
1274 if (iname_end - iname < 5) {
1275 err = -ENAMETOOLONG;
1276 goto quit;
1277 }
1278 *iname = NCP_ESC;
1279 for (k = 4; k > 0; k--) {
1280 unsigned char v;
1281
1282 v = (ec & 0xF) + '0';
1283 if (v > '9') {
1284 v += 'A' - '9' - 1;
1285 }
1286 iname[k] = v;
1287 ec >>= 4;
1288 }
1289 iname += 5;
1290 }
1291 }
1292 }
1293
1294 *iname = 0;
1295 *ilen = iname - iname_start;
1296 err = 0;
1297quit:;
1298 if (cc)
1299 kfree(vname_cc);
1300 return err;
1301}
1302
1303#else
1304
1305int
1306ncp__io2vol(unsigned char *vname, unsigned int *vlen,
1307 const unsigned char *iname, unsigned int ilen, int cc)
1308{
1309 int i;
1310
1311 if (*vlen <= ilen)
1312 return -ENAMETOOLONG;
1313
1314 if (cc)
1315 for (i = 0; i < ilen; i++) {
1316 *vname = toupper(*iname);
1317 vname++;
1318 iname++;
1319 }
1320 else {
1321 memmove(vname, iname, ilen);
1322 vname += ilen;
1323 }
1324
1325 *vlen = ilen;
1326 *vname = 0;
1327 return 0;
1328}
1329
1330int
1331ncp__vol2io(unsigned char *iname, unsigned int *ilen,
1332 const unsigned char *vname, unsigned int vlen, int cc)
1333{
1334 int i;
1335
1336 if (*ilen <= vlen)
1337 return -ENAMETOOLONG;
1338
1339 if (cc)
1340 for (i = 0; i < vlen; i++) {
1341 *iname = tolower(*vname);
1342 iname++;
1343 vname++;
1344 }
1345 else {
1346 memmove(iname, vname, vlen);
1347 iname += vlen;
1348 }
1349
1350 *ilen = vlen;
1351 *iname = 0;
1352 return 0;
1353}
1354
1355#endif