2 * Copyright (c) 1999, 2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/lockmgr.h>
33 #include <sys/malloc.h>
34 #include <sys/clock.h>
37 #include <netncp/ncp.h>
38 #include <netncp/ncp_conn.h>
39 #include <netncp/ncp_ncp.h>
40 #include <netncp/ncp_subr.h>
41 #include <netncp/ncp_rq.h>
42 #include <netncp/nwerror.h>
44 #include <fs/nwfs/nwfs.h>
45 #include <fs/nwfs/nwfs_node.h>
46 #include <fs/nwfs/nwfs_subr.h>
48 #define NCP_INFOSZ (sizeof(struct nw_entry_info) - 257)
50 MALLOC_DEFINE(M_NWFSDATA, "nwfs_data", "NWFS private data");
53 ncp_extract_file_info(struct nwmount *nmp, struct ncp_rq *rqp,
54 struct nw_entry_info *target, int withname)
58 md_get_mem(&rqp->rp, (caddr_t)target, NCP_INFOSZ, MB_MSYSTEM);
61 md_get_uint8(&rqp->rp, &name_len);
62 target->nameLen = name_len;
63 md_get_mem(&rqp->rp, (caddr_t)target->entryName, name_len, MB_MSYSTEM);
64 target->entryName[name_len] = '\0';
65 ncp_path2unix(target->entryName, target->entryName, name_len, &nmp->m.nls);
70 ncp_initsearch(struct vnode *dvp, struct thread *td, struct ucred *cred)
72 struct nwmount *nmp = VTONWFS(dvp);
73 struct ncp_conn *conn = NWFSTOCONN(nmp);
74 struct nwnode *np = VTONW(dvp);
76 u_int8_t volnum = nmp->n_volume;
77 u_int32_t dirent = np->n_fid.f_id;
80 NCPNDEBUG("vol=%d,dir=%d\n", volnum, dirent);
81 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
84 mb_put_uint8(&rqp->rq, 2); /* subfunction */
85 mb_put_uint8(&rqp->rq, nmp->name_space);
86 mb_put_uint8(&rqp->rq, 0); /* reserved */
87 ncp_rq_dbase_path(rqp, volnum, dirent, 0, NULL, NULL);
88 rqp->nr_minrplen = sizeof(np->n_seq);
89 error = ncp_request(rqp);
92 md_get_mem(&rqp->rp, (caddr_t)&np->n_seq, sizeof(np->n_seq), MB_MSYSTEM);
98 ncp_search_for_file_or_subdir(struct nwmount *nmp,
99 struct nw_search_seq *seq,
100 struct nw_entry_info *target,
101 struct thread *td,struct ucred *cred)
103 struct ncp_conn *conn = NWFSTOCONN(nmp);
107 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
110 mb_put_uint8(&rqp->rq, 3); /* subfunction */
111 mb_put_uint8(&rqp->rq, nmp->name_space);
112 mb_put_uint8(&rqp->rq, 0); /* data stream */
113 mb_put_uint16le(&rqp->rq, 0xffff); /* Search attribs */
114 mb_put_uint32le(&rqp->rq, IM_ALL); /* return info mask */
115 mb_put_mem(&rqp->rq, (caddr_t)seq, 9, MB_MSYSTEM);
116 mb_put_uint8(&rqp->rq, 2); /* 2 byte pattern */
117 mb_put_uint8(&rqp->rq, 0xff); /* following is a wildcard */
118 mb_put_uint8(&rqp->rq, '*');
119 rqp->nr_minrplen = sizeof(*seq) + 1 + NCP_INFOSZ + 1;
120 error = ncp_request(rqp);
123 md_get_mem(&rqp->rp, (caddr_t)seq, sizeof(*seq), MB_MSYSTEM);
124 md_get_uint8(&rqp->rp, NULL); /* skip */
125 error = ncp_extract_file_info(nmp, rqp, target, 1);
131 * Returns information for a (one-component) name relative to the specified
135 ncp_obtain_info(struct nwmount *nmp, u_int32_t dirent,
136 int namelen, char *path, struct nw_entry_info *target,
137 struct thread *td,struct ucred *cred)
139 struct ncp_conn *conn=NWFSTOCONN(nmp);
142 u_char volnum = nmp->n_volume, ns;
144 if (target == NULL) {
145 NCPFATAL("target == NULL\n");
148 ns = (path == NULL || path[0] == 0) ? NW_NS_DOS : nmp->name_space;
149 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
152 mb_put_uint8(&rqp->rq, 6); /* subfunction */
153 mb_put_uint8(&rqp->rq, ns);
154 mb_put_uint8(&rqp->rq, ns); /* DestNameSpace */
155 mb_put_uint16le(&rqp->rq, 0xff); /* get all */
156 mb_put_uint32le(&rqp->rq, IM_ALL);
157 ncp_rq_dbase_path(rqp, volnum, dirent, namelen, path, &nmp->m.nls);
158 error = ncp_request(rqp);
161 error = ncp_extract_file_info(nmp, rqp, target, path != NULL);
166 * lookup name pointed by cnp in directory dvp and return file info in np.
167 * May be I should create a little cache, but another way is to minimize
168 * number of calls, on other hand, in multiprocess environment ...
171 ncp_lookup(struct vnode *dvp, int len, char *name, struct nw_entry_info *fap,
172 struct thread *td,struct ucred *cred)
178 if (!dvp || dvp->v_type != VDIR) {
179 nwfs_printf("dvp is NULL or not a directory.\n");
185 if (len == 1 && name[0] == '.') {
186 if (dnp->n_flag & NVOLUME) {
187 error = ncp_obtain_info(nmp, dnp->n_fid.f_id, 0, NULL,
190 error = ncp_obtain_info(nmp, dnp->n_fid.f_parent,
191 dnp->n_nmlen, dnp->n_name, fap, td, cred);
194 } else if (len == 2 && name[0] == '.' && name[1] == '.') {
195 printf("%s: knows NOTHING about '..'\n", __func__);
198 error = ncp_obtain_info(nmp, dnp->n_fid.f_id,
199 len, name, fap, td, cred);
204 static void ConvertToNWfromDWORD(u_int32_t sfd, ncp_fh *fh);
206 ConvertToNWfromDWORD(u_int32_t sfd, ncp_fh *fh) {
207 fh->val1 = (fh->val.val32 = sfd);
212 * If both dir and name are NULL, then in target there's already a looked-up
213 * entry that wants to be opened.
216 ncp_open_create_file_or_subdir(struct nwmount *nmp,struct vnode *dvp,int namelen,
217 char *name, int open_create_mode, u_int32_t create_attributes,
218 int desired_acc_rights, struct ncp_open_info *nop,
219 struct thread *td,struct ucred *cred)
222 struct ncp_conn *conn=NWFSTOCONN(nmp);
224 u_int16_t search_attribs = SA_ALL & (~SA_SUBDIR_FILES);
229 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
232 volnum = nmp->n_volume;
233 dirent = VTONW(dvp)->n_fid.f_id;
234 if ((create_attributes & aDIR) != 0) {
235 search_attribs |= SA_SUBDIR_FILES;
237 mb_put_uint8(&rqp->rq, 1);/* subfunction */
238 mb_put_uint8(&rqp->rq, nmp->name_space);
239 mb_put_uint8(&rqp->rq, open_create_mode);
240 mb_put_uint16le(&rqp->rq, search_attribs);
241 mb_put_uint32le(&rqp->rq, IM_ALL);
242 mb_put_uint32le(&rqp->rq, create_attributes);
244 * The desired acc rights seem to be the inherited rights mask for
247 mb_put_uint16le(&rqp->rq, desired_acc_rights);
248 ncp_rq_dbase_path(rqp, volnum, dirent, namelen, name, &nmp->m.nls);
249 error = ncp_request(rqp);
251 if (error == NWE_FILE_NO_CREATE_PRIV)
255 md_get_uint32le(&rqp->rp, &nop->origfh);
256 md_get_uint8(&rqp->rp, &nop->action);
257 md_get_uint8(&rqp->rp, NULL); /* skip */
258 error = ncp_extract_file_info(nmp, rqp, &nop->fattr, 1);
260 ConvertToNWfromDWORD(nop->origfh, &nop->fh);
265 ncp_close_file(struct ncp_conn *conn, ncp_fh *fh,struct thread *td,struct ucred *cred)
270 error = ncp_rq_alloc(66, conn, td, cred, &rqp);
273 mb_put_uint8(&rqp->rq, 0);
274 mb_put_mem(&rqp->rq, (caddr_t)fh, 6, MB_MSYSTEM);
275 error = ncp_request(rqp);
283 ncp_DeleteNSEntry(struct nwmount *nmp, u_int32_t dirent,
284 int namelen,char *name,struct thread *td,struct ucred *cred)
288 struct ncp_conn *conn=NWFSTOCONN(nmp);
290 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
293 mb_put_uint8(&rqp->rq, 8); /* subfunction */
294 mb_put_uint8(&rqp->rq, nmp->name_space);
295 mb_put_uint8(&rqp->rq, 0); /* reserved */
296 mb_put_uint16le(&rqp->rq, SA_ALL); /* search attribs: all */
297 ncp_rq_dbase_path(rqp, nmp->n_volume, dirent, namelen, name, &nmp->m.nls);
298 error = ncp_request(rqp);
305 ncp_nsrename(struct ncp_conn *conn, int volume, int ns, int oldtype,
306 struct ncp_nlstables *nt,
307 nwdirent fdir, char *old_name, int oldlen,
308 nwdirent tdir, char *new_name, int newlen,
309 struct thread *td, struct ucred *cred)
314 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
317 mb_put_uint8(&rqp->rq, 4);
318 mb_put_uint8(&rqp->rq, ns);
319 mb_put_uint8(&rqp->rq, 1); /* RRenameToMySelf */
320 mb_put_uint16le(&rqp->rq, oldtype);
321 /* source Handle Path */
322 mb_put_uint8(&rqp->rq, volume);
323 mb_put_mem(&rqp->rq, (c_caddr_t)&fdir, sizeof(fdir), MB_MSYSTEM);
324 mb_put_uint8(&rqp->rq, 1);
325 mb_put_uint8(&rqp->rq, 1); /* 1 source component */
326 /* dest Handle Path */
327 mb_put_uint8(&rqp->rq, volume);
328 mb_put_mem(&rqp->rq, (c_caddr_t)&tdir, sizeof(tdir), MB_MSYSTEM);
329 mb_put_uint8(&rqp->rq, 1);
330 mb_put_uint8(&rqp->rq, 1); /* 1 destination component */
331 ncp_rq_pathstring(rqp, oldlen, old_name, nt);
332 ncp_rq_pathstring(rqp, newlen, new_name, nt);
333 error = ncp_request(rqp);
340 ncp_modify_file_or_subdir_dos_info(struct nwmount *nmp, struct vnode *vp,
342 struct nw_modify_dos_info *info,
343 struct thread *td,struct ucred *cred)
345 struct nwnode *np=VTONW(vp);
347 u_int8_t volnum = nmp->n_volume;
348 u_int32_t dirent = np->n_fid.f_id;
349 struct ncp_conn *conn=NWFSTOCONN(nmp);
352 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
355 mb_put_uint8(&rqp->rq, 7); /* subfunction */
356 mb_put_uint8(&rqp->rq, nmp->name_space);
357 mb_put_uint8(&rqp->rq, 0); /* reserved */
358 mb_put_uint16le(&rqp->rq, SA_ALL); /* search attribs: all */
359 mb_put_uint32le(&rqp->rq, info_mask);
360 mb_put_mem(&rqp->rq, (caddr_t)info, sizeof(*info), MB_MSYSTEM);
361 ncp_rq_dbase_path(rqp, volnum, dirent, 0, NULL, NULL);
362 error = ncp_request(rqp);
369 ncp_setattr(vp, vap, cred, td)
375 struct nwmount *nmp=VTONWFS(vp);
376 struct nwnode *np=VTONW(vp);
377 struct ncp_open_info nwn;
378 struct ncp_conn *conn=NWFSTOCONN(nmp);
379 struct nw_modify_dos_info info;
381 int error = 0, info_mask;
383 if (vap->va_size != VNOVAL) {
384 error = ncp_open_create_file_or_subdir(nmp, vp, 0, NULL, OC_MODE_OPEN, 0,
385 AR_WRITE | AR_READ, &nwn,td,cred);
388 error = ncp_rq_alloc(73, conn, td, cred, &rqp);
390 ncp_close_file(conn, &nwn.fh, td, cred);
393 mb_put_uint8(&rqp->rq, 0);
394 mb_put_mem(&rqp->rq, (caddr_t)&nwn.fh, 6, MB_MSYSTEM);
395 mb_put_uint32be(&rqp->rq, vap->va_size);
396 mb_put_uint16be(&rqp->rq, 0);
397 error = ncp_request(rqp);
398 np->n_vattr.va_size = np->n_size = vap->va_size;
401 ncp_close_file(conn, &nwn.fh, td, cred);
406 bzero(&info, sizeof(info));
408 if (vap->va_mtime.tv_sec != VNOVAL) {
409 info_mask |= (DM_MODIFY_TIME | DM_MODIFY_DATE);
410 ncp_unix2dostime(&vap->va_mtime, nmp->m.tz, &info.modifyDate, &info.modifyTime, NULL);
412 if (vap->va_atime.tv_sec != VNOVAL) {
413 info_mask |= (DM_LAST_ACCESS_DATE);
414 ncp_unix2dostime(&vap->va_atime, nmp->m.tz, &info.lastAccessDate, NULL, NULL);
417 error = ncp_modify_file_or_subdir_dos_info(nmp, vp, info_mask, &info,td,cred);
423 ncp_get_volume_info_with_number(struct ncp_conn *conn,
424 int n, struct ncp_volume_info *target,
425 struct thread *td,struct ucred *cred)
432 error = ncp_rq_alloc_subfn(22, 44, conn, td, cred, &rqp);
435 mb_put_uint8(&rqp->rq,n);
436 error = ncp_request(rqp);
439 md_get_uint32le(&rqp->rp, &target->total_blocks);
440 md_get_uint32le(&rqp->rp, &target->free_blocks);
441 md_get_uint32le(&rqp->rp, &target->purgeable_blocks);
442 md_get_uint32le(&rqp->rp, &target->not_yet_purgeable_blocks);
443 md_get_uint32le(&rqp->rp, &target->total_dir_entries);
444 md_get_uint32le(&rqp->rp, &target->available_dir_entries);
445 md_get_uint32le(&rqp->rp, &tmp32);
446 md_get_uint8(&rqp->rp, &target->sectors_per_block);
447 bzero(&target->volume_name, sizeof(target->volume_name));
448 md_get_uint8(&rqp->rp, &len);
449 if (len > NCP_VOLNAME_LEN) {
450 error = ENAMETOOLONG;
452 md_get_mem(&rqp->rp, (caddr_t)&target->volume_name, len, MB_MSYSTEM);
459 ncp_get_namespaces(struct ncp_conn *conn, u_int32_t volume, int *nsf,
460 struct thread *td,struct ucred *cred)
467 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
470 mb_put_uint8(&rqp->rq, 24); /* Subfunction: Get Loaded Name Spaces */
471 mb_put_uint16le(&rqp->rq, 0); /* reserved */
472 mb_put_uint8(&rqp->rq, volume);
473 error = ncp_request(rqp);
476 md_get_uint16le(&rqp->rp, &nscnt);
478 while (nscnt-- > 0) {
479 md_get_uint8(&rqp->rp, &ns);
487 ncp_lookup_volume(struct ncp_conn *conn, char *volname,
488 u_char *volNum, u_int32_t *dirEnt,
489 struct thread *td,struct ucred *cred)
495 NCPNDEBUG("looking up vol %s\n", volname);
496 error = ncp_rq_alloc(87, conn, td, cred, &rqp);
499 mb_put_uint8(&rqp->rq, 22); /* Subfunction: Generate dir handle */
500 mb_put_uint8(&rqp->rq, 0); /* src name space */
501 mb_put_uint8(&rqp->rq, 0); /* dst name space, always zero */
502 mb_put_uint16le(&rqp->rq, 0); /* dstNSIndicator (Jn) */
504 mb_put_uint8(&rqp->rq, 0); /* faked volume number */
505 mb_put_uint32be(&rqp->rq, 0); /* faked dir_base */
506 mb_put_uint8(&rqp->rq, 0xff); /* Don't have a dir_base */
507 mb_put_uint8(&rqp->rq, 1); /* 1 path component */
508 ncp_rq_pstring(rqp, volname);
509 error = ncp_request(rqp);
512 md_get_uint32le(&rqp->rp, &tmp32);
513 md_get_uint32le(&rqp->rp, dirEnt);
514 md_get_uint8(&rqp->rp, volNum);
520 * XXX: I think the timezone in struct nwfs_args is truly bogus, especially
521 * XXX: considering that nwfs_mount(8) picks this up from the kernel in
522 * XXX: the first place. Since I can't test this, I won't attempt to fix it.
527 ncp_unix2dostime(tsp, tzoff, ddp, dtp, dhp)
528 struct timespec *tsp;
538 t.tv_sec = - tzoff * 60 - utc_offset();
539 timespec2fattime(&t, 1, ddp, dtp, dhp);
543 ncp_dos2unixtime(dd, dt, dh, tzoff, tsp)
548 struct timespec *tsp;
551 fattime2timespec(dd, dt, dh, 1, tsp);
552 tsp->tv_sec += tzoff * 60 + utc_offset();