2 * Copyright (c) 2000-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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/fs/smbfs/smbfs_node.c,v 1.2.2.3 2003/01/17 08:20:26 tjr Exp $
33 * $DragonFly: src/sys/vfs/smbfs/smbfs_node.c,v 1.4 2003/06/26 05:55:12 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
40 #include <sys/mount.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 #include <sys/sysctl.h>
45 #include <vm/vm_extern.h>
46 /*#include <vm/vm_page.h>
47 #include <vm/vm_object.h>*/
48 #include <sys/queue.h>
50 #include <netsmb/smb.h>
51 #include <netsmb/smb_conn.h>
52 #include <netsmb/smb_subr.h>
54 #include <fs/smbfs/smbfs.h>
55 #include <fs/smbfs/smbfs_node.h>
56 #include <fs/smbfs/smbfs_subr.h>
58 #define SMBFS_NOHASH(smp, hval) (&(smp)->sm_hash[(hval) & (smp)->sm_hashlen])
59 #define smbfs_hash_lock(smp, td) lockmgr(&smp->sm_hashlock, LK_EXCLUSIVE, NULL, td)
60 #define smbfs_hash_unlock(smp, td) lockmgr(&smp->sm_hashlock, LK_RELEASE, NULL, td)
63 extern vop_t **smbfs_vnodeop_p;
65 MALLOC_DEFINE(M_SMBNODE, "SMBFS node", "SMBFS vnode private part");
66 static MALLOC_DEFINE(M_SMBNODENAME, "SMBFS nname", "SMBFS node name");
68 int smbfs_hashprint(struct mount *mp);
71 extern struct linker_set sysctl_vfs_smbfs;
73 SYSCTL_DECL(_vfs_smbfs);
75 SYSCTL_PROC(_vfs_smbfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
76 NULL, 0, smbfs_hashprint, "S,vnlist", "vnode hash");
79 #define FNV_32_PRIME ((u_int32_t) 0x01000193UL)
80 #define FNV1_32_INIT ((u_int32_t) 33554467UL)
83 smbfs_hash(const u_char *name, int nmlen)
87 for (v = FNV1_32_INIT; nmlen; name++, nmlen--) {
89 v ^= (u_int32_t)*name;
95 smbfs_hashprint(struct mount *mp)
97 struct smbmount *smp = VFSTOSMBFS(mp);
98 struct smbnode_hashhead *nhpp;
102 for(i = 0; i <= smp->sm_hashlen; i++) {
103 nhpp = &smp->sm_hash[i];
104 LIST_FOREACH(np, nhpp, n_hash)
105 vprint(NULL, SMBTOV(np));
111 smbfs_name_alloc(const u_char *name, int nmlen)
116 #ifdef SMBFS_NAME_DEBUG
117 cp = malloc(nmlen + 2 + sizeof(int), M_SMBNODENAME, M_WAITOK);
122 bcopy(name, cp, nmlen - 1);
125 cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK);
126 bcopy(name, cp, nmlen - 1);
133 smbfs_name_free(u_char *name)
135 #ifdef SMBFS_NAME_DEBUG
142 printf("First byte of name entry '%s' corrupted\n", name);
147 slen = strlen(name) + 1;
149 printf("Name length mismatch: was %d, now %d name '%s'\n",
153 if (name[nmlen] != 0xfe) {
154 printf("Last byte of name entry '%s' corrupted\n", name);
157 free(cp, M_SMBNODENAME);
159 free(name, M_SMBNODENAME);
164 smbfs_node_alloc(struct mount *mp, struct vnode *dvp,
165 const char *name, int nmlen, struct smbfattr *fap, struct vnode **vpp)
167 struct thread *td = curthread; /* XXX */
168 struct smbmount *smp = VFSTOSMBFS(mp);
169 struct smbnode_hashhead *nhpp;
170 struct smbnode *np, *np2, *dnp;
176 if (smp->sm_root != NULL && dvp == NULL) {
177 SMBERROR("do not allocate root vnode twice!\n");
180 if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
183 vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
184 error = vget(vp, LK_EXCLUSIVE, td);
188 } else if (nmlen == 1 && name[0] == '.') {
189 SMBERROR("do not call me with dot!\n");
192 dnp = dvp ? VTOSMB(dvp) : NULL;
193 if (dnp == NULL && dvp != NULL) {
194 vprint("smbfs_node_alloc: dead parent vnode", dvp);
197 hashval = smbfs_hash(name, nmlen);
199 smbfs_hash_lock(smp, td);
201 nhpp = SMBFS_NOHASH(smp, hashval);
202 LIST_FOREACH(np, nhpp, n_hash) {
204 if (np->n_parent != dvp ||
205 np->n_nmlen != nmlen || bcmp(name, np->n_name, nmlen) != 0)
208 smbfs_hash_unlock(smp, td);
209 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0)
214 smbfs_hash_unlock(smp, td);
216 * If we don't have node attributes, then it is an explicit lookup
217 * for an existing vnode.
222 MALLOC(np, struct smbnode *, sizeof *np, M_SMBNODE, M_WAITOK);
223 error = getnewvnode(VT_SMBFS, mp, smbfs_vnodeop_p, &vp);
228 vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
229 bzero(np, sizeof(*np));
232 np->n_mount = VFSTOSMBFS(mp);
234 np->n_name = smbfs_name_alloc(name, nmlen);
235 np->n_ino = fap->fa_ino;
239 if (/*vp->v_type == VDIR &&*/ (dvp->v_flag & VROOT) == 0) {
241 np->n_flag |= NREFPARENT;
243 } else if (vp->v_type == VREG)
244 SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
246 lockinit(&np->n_lock, PINOD, "smbnode", VLKTIMEOUT, LK_CANRECURSE);
247 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
249 smbfs_hash_lock(smp, td);
250 LIST_FOREACH(np2, nhpp, n_hash) {
251 if (np2->n_parent != dvp ||
252 np2->n_nmlen != nmlen || bcmp(name, np2->n_name, nmlen) != 0)
255 /* smb_name_free(np->n_name);
256 FREE(np, M_SMBNODE);*/
259 LIST_INSERT_HEAD(nhpp, np, n_hash);
260 smbfs_hash_unlock(smp, td);
266 smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
267 struct smbfattr *fap, struct vnode **vpp)
274 error = smbfs_node_alloc(mp, dvp, name, nmlen, fap, &vp);
279 smbfs_attr_cacheenter(vp, fap);
285 * Free smbnode, and give vnode back to system
289 struct vop_reclaim_args /* {
294 struct vnode *vp = ap->a_vp;
295 struct thread *td = ap->a_td;
297 struct smbnode *np = VTOSMB(vp);
298 struct smbmount *smp = VTOSMBFS(vp);
300 SMBVDEBUG("%s,%d\n", np->n_name, vp->v_usecount);
302 smbfs_hash_lock(smp, td);
304 dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
307 if (np->n_hash.le_prev)
308 LIST_REMOVE(np, n_hash);
310 if (smp->sm_root == np) {
311 SMBVDEBUG("root vnode\n");
315 smbfs_hash_unlock(smp, td);
317 smbfs_name_free(np->n_name);
321 if (dvp->v_usecount >= 1) {
325 * Indicate that we released something; see comment
326 * in smbfs_unmount().
331 SMBERROR("BUG: negative use count for parent!\n");
339 struct vop_inactive_args /* {
344 struct thread *td = ap->a_td;
346 struct vnode *vp = ap->a_vp;
347 struct smbnode *np = VTOSMB(vp);
348 struct smb_cred scred;
351 KKASSERT(td->td_proc);
352 cred = td->td_proc->p_ucred;
354 SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vp->v_usecount);
355 if (np->n_opencount) {
356 error = smbfs_vinvalbuf(vp, V_SAVE, td, 1);
357 smb_makescred(&scred, td, cred);
358 error = smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
359 &np->n_mtime, &scred);
362 VOP_UNLOCK(vp, 0, td);
366 * routines to maintain vnode attributes cache
367 * smbfs_attr_cacheenter: unpack np.i to vattr structure
370 smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
372 struct smbnode *np = VTOSMB(vp);
374 if (vp->v_type == VREG) {
375 if (np->n_size != fap->fa_size) {
376 np->n_size = fap->fa_size;
377 vnode_pager_setsize(vp, np->n_size);
379 } else if (vp->v_type == VDIR) {
380 np->n_size = 16384; /* should be a better way ... */
383 np->n_mtime = fap->fa_mtime;
384 np->n_dosattr = fap->fa_attr;
385 np->n_attrage = time_second;
390 smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
392 struct smbnode *np = VTOSMB(vp);
393 struct smbmount *smp = VTOSMBFS(vp);
396 diff = time_second - np->n_attrage;
397 if (diff > 2) /* XXX should be configurable */
399 va->va_type = vp->v_type; /* vnode type (for create) */
400 if (vp->v_type == VREG) {
401 va->va_mode = smp->sm_args.file_mode; /* files access mode and type */
402 } else if (vp->v_type == VDIR) {
403 va->va_mode = smp->sm_args.dir_mode; /* files access mode and type */
406 va->va_size = np->n_size;
407 va->va_nlink = 1; /* number of references to file */
408 va->va_uid = smp->sm_args.uid; /* owner user id */
409 va->va_gid = smp->sm_args.gid; /* owner group id */
410 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
411 va->va_fileid = np->n_ino; /* file id */
412 if (va->va_fileid == 0)
414 va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax;
415 va->va_mtime = np->n_mtime;
416 va->va_atime = va->va_ctime = va->va_mtime; /* time file changed */
417 va->va_gen = VNOVAL; /* generation number of file */
418 va->va_flags = 0; /* flags defined for file */
419 va->va_rdev = VNOVAL; /* device the special file represents */
420 va->va_bytes = va->va_size; /* bytes of disk space held by file */
421 va->va_filerev = 0; /* file modification number */
422 va->va_vaflags = 0; /* operations flags */