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
13 #include <linux/config.h>
15 #include "ncplib_kernel.h"
17 static inline void assert_server_locked(struct ncp_server
*server
)
19 if (server
->lock
== 0) {
20 DPRINTK("ncpfs: server not locked!\n");
24 static void ncp_add_byte(struct ncp_server
*server
, __u8 x
)
26 assert_server_locked(server
);
27 *(__u8
*) (&(server
->packet
[server
->current_size
])) = x
;
28 server
->current_size
+= 1;
32 static void ncp_add_word(struct ncp_server
*server
, __le16 x
)
34 assert_server_locked(server
);
35 put_unaligned(x
, (__le16
*) (&(server
->packet
[server
->current_size
])));
36 server
->current_size
+= 2;
40 static void ncp_add_be16(struct ncp_server
*server
, __u16 x
)
42 assert_server_locked(server
);
43 put_unaligned(cpu_to_be16(x
), (__be16
*) (&(server
->packet
[server
->current_size
])));
44 server
->current_size
+= 2;
47 static void ncp_add_dword(struct ncp_server
*server
, __le32 x
)
49 assert_server_locked(server
);
50 put_unaligned(x
, (__le32
*) (&(server
->packet
[server
->current_size
])));
51 server
->current_size
+= 4;
55 static void ncp_add_be32(struct ncp_server
*server
, __u32 x
)
57 assert_server_locked(server
);
58 put_unaligned(cpu_to_be32(x
), (__be32
*)(&(server
->packet
[server
->current_size
])));
59 server
->current_size
+= 4;
62 static inline void ncp_add_dword_lh(struct ncp_server
*server
, __u32 x
) {
63 ncp_add_dword(server
, cpu_to_le32(x
));
66 static void ncp_add_mem(struct ncp_server
*server
, const void *source
, int size
)
68 assert_server_locked(server
);
69 memcpy(&(server
->packet
[server
->current_size
]), source
, size
);
70 server
->current_size
+= size
;
74 static void ncp_add_pstring(struct ncp_server
*server
, const char *s
)
77 assert_server_locked(server
);
79 DPRINTK("ncpfs: string too long: %s\n", s
);
82 ncp_add_byte(server
, len
);
83 ncp_add_mem(server
, s
, len
);
87 static inline void ncp_init_request(struct ncp_server
*server
)
89 ncp_lock_server(server
);
91 server
->current_size
= sizeof(struct ncp_request_header
);
92 server
->has_subfunction
= 0;
95 static inline void ncp_init_request_s(struct ncp_server
*server
, int subfunction
)
97 ncp_lock_server(server
);
99 server
->current_size
= sizeof(struct ncp_request_header
) + 2;
100 ncp_add_byte(server
, subfunction
);
102 server
->has_subfunction
= 1;
106 ncp_reply_data(struct ncp_server
*server
, int offset
)
108 return &(server
->packet
[sizeof(struct ncp_reply_header
) + offset
]);
111 static inline __u8
BVAL(void* data
)
113 return get_unaligned((__u8
*)data
);
117 ncp_reply_byte(struct ncp_server
*server
, int offset
)
119 return get_unaligned((__u8
*) ncp_reply_data(server
, offset
));
122 static inline __u16
WVAL_LH(void* data
)
124 return le16_to_cpu(get_unaligned((__le16
*)data
));
128 ncp_reply_le16(struct ncp_server
*server
, int offset
)
130 return le16_to_cpu(get_unaligned((__le16
*) ncp_reply_data(server
, offset
)));
134 ncp_reply_be16(struct ncp_server
*server
, int offset
)
136 return be16_to_cpu(get_unaligned((__be16
*) ncp_reply_data(server
, offset
)));
139 static inline __u32
DVAL_LH(void* data
)
141 return le32_to_cpu(get_unaligned((__le32
*)data
));
145 ncp_reply_dword(struct ncp_server
*server
, int offset
)
147 return get_unaligned((__le32
*) ncp_reply_data(server
, offset
));
150 static inline __u32
ncp_reply_dword_lh(struct ncp_server
* server
, int offset
) {
151 return le32_to_cpu(ncp_reply_dword(server
, offset
));
155 ncp_negotiate_buffersize(struct ncp_server
*server
, int size
, int *target
)
159 ncp_init_request(server
);
160 ncp_add_be16(server
, size
);
162 if ((result
= ncp_request(server
, 33)) != 0) {
163 ncp_unlock_server(server
);
166 *target
= min_t(unsigned int, ncp_reply_be16(server
, 0), size
);
168 ncp_unlock_server(server
);
175 * bit 1 packet signing
178 ncp_negotiate_size_and_options(struct ncp_server
*server
,
179 int size
, int options
, int *ret_size
, int *ret_options
) {
182 /* there is minimum */
183 if (size
< NCP_BLOCK_SIZE
) size
= NCP_BLOCK_SIZE
;
185 ncp_init_request(server
);
186 ncp_add_be16(server
, size
);
187 ncp_add_byte(server
, options
);
189 if ((result
= ncp_request(server
, 0x61)) != 0)
191 ncp_unlock_server(server
);
195 /* NCP over UDP returns 0 (!!!) */
196 result
= ncp_reply_be16(server
, 0);
197 if (result
>= NCP_BLOCK_SIZE
)
198 size
= min(result
, size
);
200 *ret_options
= ncp_reply_byte(server
, 4);
202 ncp_unlock_server(server
);
206 int ncp_get_volume_info_with_number(struct ncp_server
* server
,
207 int n
, struct ncp_volume_info
* target
) {
211 ncp_init_request_s(server
, 44);
212 ncp_add_byte(server
, n
);
214 if ((result
= ncp_request(server
, 22)) != 0) {
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);
225 memset(&(target
->volume_name
), 0, sizeof(target
->volume_name
));
228 len
= ncp_reply_byte(server
, 29);
229 if (len
> NCP_VOLNAME_LEN
) {
230 DPRINTK("ncpfs: volume name too long: %d\n", len
);
233 memcpy(&(target
->volume_name
), ncp_reply_data(server
, 30), len
);
236 ncp_unlock_server(server
);
240 int ncp_get_directory_info(struct ncp_server
* server
, __u8 n
,
241 struct ncp_volume_info
* target
) {
245 ncp_init_request_s(server
, 45);
246 ncp_add_byte(server
, n
);
248 if ((result
= ncp_request(server
, 22)) != 0) {
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);
259 memset(&(target
->volume_name
), 0, sizeof(target
->volume_name
));
262 len
= ncp_reply_byte(server
, 21);
263 if (len
> NCP_VOLNAME_LEN
) {
264 DPRINTK("ncpfs: volume name too long: %d\n", len
);
267 memcpy(&(target
->volume_name
), ncp_reply_data(server
, 22), len
);
270 ncp_unlock_server(server
);
275 ncp_close_file(struct ncp_server
*server
, const char *file_id
)
279 ncp_init_request(server
);
280 ncp_add_byte(server
, 0);
281 ncp_add_mem(server
, file_id
, 6);
283 result
= ncp_request(server
, 66);
284 ncp_unlock_server(server
);
289 ncp_make_closed(struct inode
*inode
)
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
);
300 PPRINTK("ncp_make_closed: volnum=%d, dirent=%u, error=%d\n",
301 NCP_FINFO(inode
)->volNumber
,
302 NCP_FINFO(inode
)->dirEntNum
, err
);
304 up(&NCP_FINFO(inode
)->open_sem
);
308 static void ncp_add_handle_path(struct ncp_server
*server
, __u8 vol_num
,
309 __le32 dir_base
, int have_dir_base
,
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 */
317 ncp_add_byte(server
, 0xff); /* no handle */
320 ncp_add_byte(server
, 1); /* 1 component */
321 ncp_add_pstring(server
, path
);
323 ncp_add_byte(server
, 0);
327 int ncp_dirhandle_alloc(struct ncp_server
* server
, __u8 volnum
, __le32 dirent
,
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);
340 ncp_unlock_server(server
);
344 int ncp_dirhandle_free(struct ncp_server
* server
, __u8 dirhandle
) {
347 ncp_init_request_s(server
, 20);
348 ncp_add_byte(server
, dirhandle
);
349 result
= ncp_request(server
, 22);
350 ncp_unlock_server(server
);
354 void ncp_extract_file_info(void *structure
, struct nw_info_struct
*target
)
357 const int info_struct_size
= offsetof(struct nw_info_struct
, nameLen
);
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
);
368 #ifdef CONFIG_NCPFS_NFS_NS
369 static inline void ncp_extract_nfs_info(unsigned char *structure
,
370 struct nw_nfs_info
*target
)
372 target
->mode
= DVAL_LH(structure
);
373 target
->rdev
= DVAL_LH(structure
+ 8);
377 int ncp_obtain_nfs_info(struct ncp_server
*server
,
378 struct nw_info_struct
*target
)
382 #ifdef CONFIG_NCPFS_NFS_NS
383 __u32 volnum
= target
->volNumber
;
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
);
397 if ((result
= ncp_request(server
, 87)) == 0) {
398 ncp_extract_nfs_info(ncp_reply_data(server
, 0), &target
->nfs
);
400 "ncp_obtain_nfs_info: (%s) mode=0%o, rdev=0x%x\n",
401 target
->entryName
, target
->nfs
.mode
,
404 target
->nfs
.mode
= 0;
405 target
->nfs
.rdev
= 0;
407 ncp_unlock_server(server
);
412 target
->nfs
.mode
= 0;
413 target
->nfs
.rdev
= 0;
419 * Returns information for a (one-component) name relative to
420 * the specified directory.
422 int ncp_obtain_info(struct ncp_server
*server
, struct inode
*dir
, char *path
,
423 struct nw_info_struct
*target
)
425 __u8 volnum
= NCP_FINFO(dir
)->volNumber
;
426 __le32 dirent
= NCP_FINFO(dir
)->dirEntNum
;
429 if (target
== NULL
) {
430 printk(KERN_ERR
"ncp_obtain_info: invalid call\n");
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
);
441 if ((result
= ncp_request(server
, 87)) != 0)
443 ncp_extract_file_info(ncp_reply_data(server
, 0), target
);
444 ncp_unlock_server(server
);
446 result
= ncp_obtain_nfs_info(server
, target
);
450 ncp_unlock_server(server
);
454 #ifdef CONFIG_NCPFS_NFS_NS
456 ncp_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
)
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
);
471 if ((result
= ncp_request(server
, 87)) == 0)
473 if (DOS_dir_base
) *DOS_dir_base
=ncp_reply_dword(server
, 0x34);
475 ncp_unlock_server(server
);
478 #endif /* CONFIG_NCPFS_NFS_NS */
481 ncp_get_known_namespace(struct ncp_server
*server
, __u8 volume
)
483 #if defined(CONFIG_NCPFS_OS2_NS) || defined(CONFIG_NCPFS_NFS_NS)
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
);
493 if ((result
= ncp_request(server
, 87)) != 0) {
494 ncp_unlock_server(server
);
495 return NW_NS_DOS
; /* not result ?? */
499 no_namespaces
= ncp_reply_le16(server
, 0);
500 namespace = ncp_reply_data(server
, 2);
502 while (no_namespaces
> 0) {
503 DPRINTK("get_namespaces: found %d on %d\n", *namespace, volume
);
505 #ifdef CONFIG_NCPFS_NFS_NS
506 if ((*namespace == NW_NS_NFS
) && !(server
->m
.flags
&NCP_MOUNT_NO_NFS
))
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
))
517 #endif /* CONFIG_NCPFS_OS2_NS */
521 ncp_unlock_server(server
);
523 #else /* neither OS2 nor NFS - only DOS */
525 #endif /* defined(CONFIG_NCPFS_OS2_NS) || defined(CONFIG_NCPFS_NFS_NS) */
529 ncp_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
)
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
);
544 if ((result
= ncp_request(server
, 87)) != 0)
546 ncp_unlock_server(server
);
551 *dirEntNum
= ncp_reply_dword(server
, 0x30);
553 *DosDirNum
= ncp_reply_dword(server
, 0x34);
554 ncp_unlock_server(server
);
559 ncp_mount_subdir(struct ncp_server
*server
,
560 __u8 volNumber
, __u8 srcNS
, __le32 dirEntNum
,
561 __u32
* volume
, __le32
* newDirEnt
, __le32
* newDosEnt
)
566 dstNS
= ncp_get_known_namespace(server
, volNumber
);
567 if ((result
= ncp_ObtainSpecificDirBase(server
, srcNS
, dstNS
, volNumber
,
568 dirEntNum
, NULL
, newDirEnt
, newDosEnt
)) != 0)
572 server
->name_space
[volNumber
] = dstNS
;
574 server
->m
.mounted_vol
[1] = 0;
575 server
->m
.mounted_vol
[0] = 'X';
580 ncp_get_volume_root(struct ncp_server
*server
, const char *volname
,
581 __u32
* volume
, __le32
* dirent
, __le32
* dosdirent
)
586 DPRINTK("ncp_get_volume_root: looking up vol %s\n", volname
);
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 */
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
);
601 if ((result
= ncp_request(server
, 87)) != 0) {
602 ncp_unlock_server(server
);
605 *dirent
= *dosdirent
= ncp_reply_dword(server
, 4);
606 volnum
= ncp_reply_byte(server
, 8);
607 ncp_unlock_server(server
);
610 server
->name_space
[volnum
] = ncp_get_known_namespace(server
, volnum
);
612 DPRINTK("lookup_vol: namespace[%d] = %d\n",
613 volnum
, server
->name_space
[volnum
]);
619 ncp_lookup_volume(struct ncp_server
*server
, const char *volname
,
620 struct nw_info_struct
*target
)
624 memset(target
, 0, sizeof(*target
));
625 result
= ncp_get_volume_root(server
, volname
,
626 &target
->volNumber
, &target
->dirEntNum
, &target
->DosDirNum
);
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;
640 int ncp_modify_file_or_subdir_dos_info_path(struct ncp_server
*server
,
644 const struct nw_modify_dos_info
*info
)
646 __u8 volnum
= NCP_FINFO(dir
)->volNumber
;
647 __le32 dirent
= NCP_FINFO(dir
)->dirEntNum
;
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 */
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
);
660 result
= ncp_request(server
, 87);
661 ncp_unlock_server(server
);
665 int ncp_modify_file_or_subdir_dos_info(struct ncp_server
*server
,
668 const struct nw_modify_dos_info
*info
)
670 return ncp_modify_file_or_subdir_dos_info_path(server
, dir
, NULL
,
674 #ifdef CONFIG_NCPFS_NFS_NS
675 int ncp_modify_nfs_info(struct ncp_server
*server
, __u8 volnum
, __le32 dirent
,
676 __u32 mode
, __u32 rdev
)
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
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
);
703 ncp_DeleteNSEntry(struct ncp_server
*server
,
704 __u8 have_dir_base
, __u8 volnum
, __le32 dirent
,
705 char* name
, __u8 ns
, __le16 attr
)
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
);
716 result
= ncp_request(server
, 87);
717 ncp_unlock_server(server
);
722 ncp_del_file_or_subdir2(struct ncp_server
*server
,
723 struct dentry
*dentry
)
725 struct inode
*inode
= dentry
->d_inode
;
730 #ifdef CONFIG_NCPFS_DEBUGDENTRY
731 PRINTK("ncpfs: ncpdel2: dentry->d_inode == NULL\n");
733 return 0xFF; /* Any error */
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));
741 ncp_del_file_or_subdir(struct ncp_server
*server
,
742 struct inode
*dir
, char *name
)
744 __u8 volnum
= NCP_FINFO(dir
)->volNumber
;
745 __le32 dirent
= NCP_FINFO(dir
)->dirEntNum
;
747 #ifdef CONFIG_NCPFS_NFS_NS
748 if (server
->name_space
[volnum
]==NW_NS_NFS
)
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));
757 #endif /* CONFIG_NCPFS_NFS_NS */
758 return ncp_DeleteNSEntry(server
, 1, volnum
, dirent
, name
, server
->name_space
[volnum
], cpu_to_le16(0x8006));
761 static inline void ConvertToNWfromDWORD(__u16 v0
, __u16 v1
, __u8 ret
[6])
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);
770 /* If both dir and name are NULL, then in target there's already a
771 looked-up entry that wants to be opened. */
772 int 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
)
779 __le16 search_attribs
= cpu_to_le16(0x0006);
784 volnum
= NCP_FINFO(dir
)->volNumber
;
785 dirent
= NCP_FINFO(dir
)->dirEntNum
;
787 if ((create_attributes
& aDIR
) != 0) {
788 search_attribs
|= cpu_to_le16(0x8000);
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
799 ncp_add_word(server
, desired_acc_rights
);
800 ncp_add_handle_path(server
, volnum
, dirent
, 1, name
);
802 if ((result
= ncp_request(server
, 87)) != 0)
804 if (!(create_attributes
& aDIR
))
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
);
814 ncp_unlock_server(server
);
816 (void)ncp_obtain_nfs_info(server
, &(target
->i
));
820 ncp_unlock_server(server
);
825 ncp_initialize_search(struct ncp_server
*server
, struct inode
*dir
,
826 struct nw_search_sequence
*target
)
828 __u8 volnum
= NCP_FINFO(dir
)->volNumber
;
829 __le32 dirent
= NCP_FINFO(dir
)->dirEntNum
;
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
);
838 result
= ncp_request(server
, 87);
841 memcpy(target
, ncp_reply_data(server
, 0), sizeof(*target
));
844 ncp_unlock_server(server
);
848 /* Search for everything */
849 int ncp_search_for_file_or_subdir(struct ncp_server
*server
,
850 struct nw_search_sequence
*seq
,
851 struct nw_info_struct
*target
)
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 */
868 ncp_add_byte(server
, 2); /* 2 byte pattern */
869 ncp_add_byte(server
, 0xff); /* following is a wildcard */
870 ncp_add_byte(server
, '*');
873 if ((result
= ncp_request(server
, 87)) != 0)
875 memcpy(seq
, ncp_reply_data(server
, 0), sizeof(*seq
));
876 ncp_extract_file_info(ncp_reply_data(server
, 10), target
);
878 ncp_unlock_server(server
);
880 result
= ncp_obtain_nfs_info(server
, target
);
884 ncp_unlock_server(server
);
888 int ncp_search_for_fileset(struct ncp_server
*server
,
889 struct nw_search_sequence
*seq
,
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 */
913 ncp_add_byte(server
, 2); /* 2 byte pattern */
914 ncp_add_byte(server
, 0xff); /* following is a wildcard */
915 ncp_add_byte(server
, '*');
917 result
= ncp_request2(server
, 87, buffer
, bufsize
);
919 ncp_unlock_server(server
);
922 if (server
->ncp_reply_size
< 12) {
923 ncp_unlock_server(server
);
926 *rsize
= server
->ncp_reply_size
- 12;
927 ncp_unlock_server(server
);
928 buffer
= buffer
+ sizeof(struct ncp_reply_header
);
930 *cnt
= WVAL_LH(buffer
+ 10);
931 *more
= BVAL(buffer
+ 9);
932 memcpy(seq
, buffer
, 9);
937 ncp_RenameNSEntry(struct ncp_server
*server
,
938 struct inode
*old_dir
, char *old_name
, __le16 old_type
,
939 struct inode
*new_dir
, char *new_name
)
941 int result
= -EINVAL
;
943 if ((old_dir
== NULL
) || (old_name
== NULL
) ||
944 (new_dir
== NULL
) || (new_name
== NULL
))
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 */
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 */
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 */
965 /* source path string */
966 ncp_add_pstring(server
, old_name
);
967 /* dest path string */
968 ncp_add_pstring(server
, new_name
);
970 result
= ncp_request(server
, 87);
971 ncp_unlock_server(server
);
976 int 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
)
981 __le16 old_type
= cpu_to_le16(0x06);
983 /* If somebody can do it atomic, call me... vandrove@vc.cvut.cz */
984 result
= ncp_RenameNSEntry(server
, old_dir
, old_name
, old_type
,
986 if (result
== 0xFF) /* File Not Found, try directory */
988 old_type
= cpu_to_le16(0x16);
989 result
= ncp_RenameNSEntry(server
, old_dir
, old_name
, old_type
,
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
,
1001 /* We have to transfer to/from user space */
1003 ncp_read_kernel(struct ncp_server
*server
, const char *file_id
,
1004 __u32 offset
, __u16 to_read
, char *target
, int *bytes_read
)
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
);
1015 if ((result
= ncp_request(server
, 72)) != 0) {
1018 *bytes_read
= ncp_reply_be16(server
, 0);
1019 source
= ncp_reply_data(server
, 2 + (offset
& 1));
1021 memcpy(target
, source
, *bytes_read
);
1023 ncp_unlock_server(server
);
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...
1039 ncp_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
)
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
);
1053 int len
= be16_to_cpu(get_unaligned((__be16
*)((char*)bounce
+
1054 sizeof(struct ncp_reply_header
))));
1056 if (len
<= to_read
) {
1059 source
= (char*)bounce
+
1060 sizeof(struct ncp_reply_header
) + 2 +
1064 if (copy_to_user(target
, source
, len
))
1072 ncp_write_kernel(struct ncp_server
*server
, const char *file_id
,
1073 __u32 offset
, __u16 to_write
,
1074 const char *source
, int *bytes_written
)
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
);
1085 if ((result
= ncp_request(server
, 73)) == 0)
1086 *bytes_written
= to_write
;
1087 ncp_unlock_server(server
);
1091 #ifdef CONFIG_NCPFS_IOCTL_LOCKING
1093 ncp_LogPhysicalRecord(struct ncp_server
*server
, const char *file_id
,
1094 __u8 locktype
, __u32 offset
, __u32 length
, __u16 timeout
)
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
);
1105 if ((result
= ncp_request(server
, 0x1A)) != 0)
1107 ncp_unlock_server(server
);
1110 ncp_unlock_server(server
);
1115 ncp_ClearPhysicalRecord(struct ncp_server
*server
, const char *file_id
,
1116 __u32 offset
, __u32 length
)
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
);
1126 if ((result
= ncp_request(server
, 0x1E)) != 0)
1128 ncp_unlock_server(server
);
1131 ncp_unlock_server(server
);
1134 #endif /* CONFIG_NCPFS_IOCTL_LOCKING */
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.
1142 ncp__io2vol(struct ncp_server
*server
, unsigned char *vname
, unsigned int *vlen
,
1143 const unsigned char *iname
, unsigned int ilen
, int cc
)
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
;
1151 iname_end
= iname
+ ilen
;
1152 vname_start
= vname
;
1153 vname_end
= vname
+ *vlen
- 1;
1155 while (iname
< iname_end
) {
1159 if (NCP_IS_FLAG(server
, NCP_FLAG_UTF8
)) {
1162 k
= utf8_mbtowc(&ec
, iname
, iname_end
- iname
);
1167 if (*iname
== NCP_ESC
) {
1170 if (iname_end
- iname
< 5)
1174 for (k
= 1; k
< 5; k
++) {
1177 nc
= iname
[k
] - '0';
1179 nc
-= 'A' - '0' - 10;
1180 if ((nc
< 10) || (nc
> 15)) {
1184 ec
= (ec
<< 4) | nc
;
1189 if ( (chl
= in
->char2uni(iname
, iname_end
- iname
, &ec
)) < 0)
1195 /* unitoupper should be here! */
1197 chl
= out
->uni2char(ec
, vname
, vname_end
- vname
);
1201 /* this is wrong... */
1205 for (chi
= 0; chi
< chl
; chi
++){
1206 vname
[chi
] = ncp_toupper(out
, vname
[chi
]);
1213 *vlen
= vname
- vname_start
;
1218 ncp__vol2io(struct ncp_server
*server
, unsigned char *iname
, unsigned int *ilen
,
1219 const unsigned char *vname
, unsigned int vlen
, int cc
)
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
;
1234 /* this is wrong! */
1235 vname_cc
= kmalloc(vlen
, GFP_KERNEL
);
1238 for (i
= 0; i
< vlen
; i
++)
1239 vname_cc
[i
] = ncp_tolower(in
, vname
[i
]);
1243 iname_start
= iname
;
1244 iname_end
= iname
+ *ilen
- 1;
1245 vname_end
= vname
+ vlen
;
1247 while (vname
< vname_end
) {
1251 if ( (chl
= in
->char2uni(vname
, vname_end
- vname
, &ec
)) < 0) {
1257 /* unitolower should be here! */
1259 if (NCP_IS_FLAG(server
, NCP_FLAG_UTF8
)) {
1262 k
= utf8_wctomb(iname
, ec
, iname_end
- iname
);
1264 err
= -ENAMETOOLONG
;
1269 if ( (chl
= out
->uni2char(ec
, iname
, iname_end
- iname
)) >= 0) {
1274 if (iname_end
- iname
< 5) {
1275 err
= -ENAMETOOLONG
;
1279 for (k
= 4; k
> 0; k
--) {
1282 v
= (ec
& 0xF) + '0';
1295 *ilen
= iname
- iname_start
;
1306 ncp__io2vol(unsigned char *vname
, unsigned int *vlen
,
1307 const unsigned char *iname
, unsigned int ilen
, int cc
)
1312 return -ENAMETOOLONG
;
1315 for (i
= 0; i
< ilen
; i
++) {
1316 *vname
= toupper(*iname
);
1321 memmove(vname
, iname
, ilen
);
1331 ncp__vol2io(unsigned char *iname
, unsigned int *ilen
,
1332 const unsigned char *vname
, unsigned int vlen
, int cc
)
1337 return -ENAMETOOLONG
;
1340 for (i
= 0; i
< vlen
; i
++) {
1341 *iname
= tolower(*vname
);
1346 memmove(iname
, vname
, vlen
);