1 /*- 2 * Copyright (c) 2000-2001 Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kdb.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/mount.h> 41 #include <sys/proc.h> 42 #include <sys/queue.h> 43 #include <sys/stat.h> 44 #include <sys/sysctl.h> 45 #include <sys/time.h> 46 #include <sys/vnode.h> 47 48 #include <netsmb/smb.h> 49 #include <netsmb/smb_conn.h> 50 #include <netsmb/smb_subr.h> 51 52 #include <vm/vm.h> 53 #include <vm/vm_extern.h> 54 /*#include <vm/vm_page.h> 55 #include <vm/vm_object.h>*/ 56 57 #include <fs/smbfs/smbfs.h> 58 #include <fs/smbfs/smbfs_node.h> 59 #include <fs/smbfs/smbfs_subr.h> 60 61 #define SMBFS_NOHASH(smp, hval) (&(smp)->sm_hash[(hval) & (smp)->sm_hashlen]) 62 #define smbfs_hash_lock(smp, td) lockmgr(&smp->sm_hashlock, LK_EXCLUSIVE, NULL, td) 63 #define smbfs_hash_unlock(smp, td) lockmgr(&smp->sm_hashlock, LK_RELEASE, NULL, td) 64 65 66 extern struct vop_vector smbfs_vnodeops; /* XXX -> .h file */ 67 68 MALLOC_DEFINE(M_SMBNODE, "smbufs_node", "SMBFS vnode private part"); 69 static MALLOC_DEFINE(M_SMBNODENAME, "smbufs_nname", "SMBFS node name"); 70 71 int smbfs_hashprint(struct mount *mp); 72 73 #if 0 74 #ifdef SYSCTL_DECL 75 SYSCTL_DECL(_vfs_smbfs); 76 #endif 77 SYSCTL_PROC(_vfs_smbfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE, 78 NULL, 0, smbfs_hashprint, "S,vnlist", "vnode hash"); 79 #endif 80 81 #define FNV_32_PRIME ((u_int32_t) 0x01000193UL) 82 #define FNV1_32_INIT ((u_int32_t) 33554467UL) 83 84 u_int32_t 85 smbfs_hash(const u_char *name, int nmlen) 86 { 87 u_int32_t v; 88 89 for (v = FNV1_32_INIT; nmlen; name++, nmlen--) { 90 v *= FNV_32_PRIME; 91 v ^= (u_int32_t)*name; 92 } 93 return v; 94 } 95 96 int 97 smbfs_hashprint(struct mount *mp) 98 { 99 struct smbmount *smp = VFSTOSMBFS(mp); 100 struct smbnode_hashhead *nhpp; 101 struct smbnode *np; 102 int i; 103 104 for(i = 0; i <= smp->sm_hashlen; i++) { 105 nhpp = &smp->sm_hash[i]; 106 LIST_FOREACH(np, nhpp, n_hash) 107 vprint("", SMBTOV(np)); 108 } 109 return 0; 110 } 111 112 static char * 113 smbfs_name_alloc(const u_char *name, int nmlen) 114 { 115 u_char *cp; 116 117 nmlen++; 118 #ifdef SMBFS_NAME_DEBUG 119 cp = malloc(nmlen + 2 + sizeof(int), M_SMBNODENAME, M_WAITOK); 120 *(int*)cp = nmlen; 121 cp += sizeof(int); 122 cp[0] = 0xfc; 123 cp++; 124 bcopy(name, cp, nmlen - 1); 125 cp[nmlen] = 0xfe; 126 #else 127 cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK); 128 bcopy(name, cp, nmlen - 1); 129 #endif 130 cp[nmlen - 1] = 0; 131 return cp; 132 } 133 134 static void 135 smbfs_name_free(u_char *name) 136 { 137 #ifdef SMBFS_NAME_DEBUG 138 int nmlen, slen; 139 u_char *cp; 140 141 cp = name; 142 cp--; 143 if (*cp != 0xfc) { 144 printf("First byte of name entry '%s' corrupted\n", name); 145 kdb_enter("ditto"); 146 } 147 cp -= sizeof(int); 148 nmlen = *(int*)cp; 149 slen = strlen(name) + 1; 150 if (nmlen != slen) { 151 printf("Name length mismatch: was %d, now %d name '%s'\n", 152 nmlen, slen, name); 153 kdb_enter("ditto"); 154 } 155 if (name[nmlen] != 0xfe) { 156 printf("Last byte of name entry '%s' corrupted\n", name); 157 kdb_enter("ditto"); 158 } 159 free(cp, M_SMBNODENAME); 160 #else 161 free(name, M_SMBNODENAME); 162 #endif 163 } 164 165 static int 166 smbfs_node_alloc(struct mount *mp, struct vnode *dvp, 167 const char *name, int nmlen, struct smbfattr *fap, struct vnode **vpp) 168 { 169 struct vattr vattr; 170 struct thread *td = curthread; /* XXX */ 171 struct smbmount *smp = VFSTOSMBFS(mp); 172 struct smbnode_hashhead *nhpp; 173 struct smbnode *np, *np2, *dnp; 174 struct vnode *vp; 175 u_long hashval; 176 int error; 177 178 *vpp = NULL; 179 if (smp->sm_root != NULL && dvp == NULL) { 180 SMBERROR("do not allocate root vnode twice!\n"); 181 return EINVAL; 182 } 183 if (nmlen == 2 && bcmp(name, "..", 2) == 0) { 184 if (dvp == NULL) 185 return EINVAL; 186 vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode; 187 error = vget(vp, LK_EXCLUSIVE, td); 188 if (error == 0) 189 *vpp = vp; 190 return error; 191 } else if (nmlen == 1 && name[0] == '.') { 192 SMBERROR("do not call me with dot!\n"); 193 return EINVAL; 194 } 195 dnp = dvp ? VTOSMB(dvp) : NULL; 196 if (dnp == NULL && dvp != NULL) { 197 vprint("smbfs_node_alloc: dead parent vnode", dvp); 198 return EINVAL; 199 } 200 hashval = smbfs_hash(name, nmlen); 201 retry: 202 smbfs_hash_lock(smp, td); 203 loop: 204 nhpp = SMBFS_NOHASH(smp, hashval); 205 LIST_FOREACH(np, nhpp, n_hash) { 206 vp = SMBTOV(np); 207 if (np->n_parent != dvp || 208 np->n_nmlen != nmlen || bcmp(name, np->n_name, nmlen) != 0) 209 continue; 210 VI_LOCK(vp); 211 smbfs_hash_unlock(smp, td); 212 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0) 213 goto retry; 214 /* Force cached attributes to be refreshed if stale. */ 215 (void)VOP_GETATTR(vp, &vattr, td->td_ucred, td); 216 /* 217 * If the file type on the server is inconsistent with 218 * what it was when we created the vnode, kill the 219 * bogus vnode now and fall through to the code below 220 * to create a new one with the right type. 221 */ 222 if ((vp->v_type == VDIR && (np->n_dosattr & SMB_FA_DIR) == 0) || 223 (vp->v_type == VREG && (np->n_dosattr & SMB_FA_DIR) != 0)) { 224 vgone(vp); 225 vput(vp); 226 break; 227 } 228 *vpp = vp; 229 return 0; 230 } 231 smbfs_hash_unlock(smp, td); 232 /* 233 * If we don't have node attributes, then it is an explicit lookup 234 * for an existing vnode. 235 */ 236 if (fap == NULL) 237 return ENOENT; 238 239 MALLOC(np, struct smbnode *, sizeof *np, M_SMBNODE, M_WAITOK); 240 error = getnewvnode("smbfs", mp, &smbfs_vnodeops, &vp); 241 if (error) { 242 FREE(np, M_SMBNODE); 243 return error; 244 } 245 vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG; 246 bzero(np, sizeof(*np)); 247 vp->v_data = np; 248 np->n_vnode = vp; 249 np->n_mount = VFSTOSMBFS(mp); 250 np->n_nmlen = nmlen; 251 np->n_name = smbfs_name_alloc(name, nmlen); 252 np->n_ino = fap->fa_ino; 253 254 if (dvp) { 255 ASSERT_VOP_LOCKED(dvp, "smbfs_node_alloc"); 256 np->n_parent = dvp; 257 if (/*vp->v_type == VDIR &&*/ (dvp->v_vflag & VV_ROOT) == 0) { 258 vref(dvp); 259 np->n_flag |= NREFPARENT; 260 } 261 } else if (vp->v_type == VREG) 262 SMBERROR("new vnode '%s' born without parent ?\n", np->n_name); 263 264 vp->v_vnlock->lk_flags |= LK_CANRECURSE; 265 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 266 267 smbfs_hash_lock(smp, td); 268 LIST_FOREACH(np2, nhpp, n_hash) { 269 if (np2->n_parent != dvp || 270 np2->n_nmlen != nmlen || bcmp(name, np2->n_name, nmlen) != 0) 271 continue; 272 vput(vp); 273 /* smb_name_free(np->n_name); 274 FREE(np, M_SMBNODE);*/ 275 goto loop; 276 } 277 LIST_INSERT_HEAD(nhpp, np, n_hash); 278 smbfs_hash_unlock(smp, td); 279 *vpp = vp; 280 return 0; 281 } 282 283 int 284 smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen, 285 struct smbfattr *fap, struct vnode **vpp) 286 { 287 struct smbnode *np; 288 struct vnode *vp; 289 int error; 290 291 *vpp = NULL; 292 error = smbfs_node_alloc(mp, dvp, name, nmlen, fap, &vp); 293 if (error) 294 return error; 295 np = VTOSMB(vp); 296 if (fap) 297 smbfs_attr_cacheenter(vp, fap); 298 *vpp = vp; 299 return 0; 300 } 301 302 /* 303 * Free smbnode, and give vnode back to system 304 */ 305 int 306 smbfs_reclaim(ap) 307 struct vop_reclaim_args /* { 308 struct vnode *a_vp; 309 struct thread *a_p; 310 } */ *ap; 311 { 312 struct vnode *vp = ap->a_vp; 313 struct thread *td = ap->a_td; 314 struct vnode *dvp; 315 struct smbnode *np = VTOSMB(vp); 316 struct smbmount *smp = VTOSMBFS(vp); 317 318 SMBVDEBUG("%s,%d\n", np->n_name, vrefcnt(vp)); 319 320 KASSERT((np->n_flag & NOPEN) == 0, ("file not closed before reclaim")); 321 322 smbfs_hash_lock(smp, td); 323 /* 324 * Destroy the vm object and flush associated pages. 325 */ 326 vnode_destroy_vobject(vp); 327 328 dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ? 329 np->n_parent : NULL; 330 331 if (np->n_hash.le_prev) 332 LIST_REMOVE(np, n_hash); 333 if (smp->sm_root == np) { 334 SMBVDEBUG("root vnode\n"); 335 smp->sm_root = NULL; 336 } 337 vp->v_data = NULL; 338 smbfs_hash_unlock(smp, td); 339 if (np->n_name) 340 smbfs_name_free(np->n_name); 341 FREE(np, M_SMBNODE); 342 if (dvp != NULL) { 343 vrele(dvp); 344 /* 345 * Indicate that we released something; see comment 346 * in smbfs_unmount(). 347 */ 348 smp->sm_didrele = 1; 349 } 350 return 0; 351 } 352 353 int 354 smbfs_inactive(ap) 355 struct vop_inactive_args /* { 356 struct vnode *a_vp; 357 struct thread *a_td; 358 } */ *ap; 359 { 360 struct thread *td = ap->a_td; 361 struct ucred *cred = td->td_ucred; 362 struct vnode *vp = ap->a_vp; 363 struct smbnode *np = VTOSMB(vp); 364 struct smb_cred scred; 365 struct vattr va; 366 367 SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vrefcnt(vp)); 368 if ((np->n_flag & NOPEN) != 0) { 369 smb_makescred(&scred, td, cred); 370 smbfs_vinvalbuf(vp, td); 371 if (vp->v_type == VREG) { 372 VOP_GETATTR(vp, &va, cred, td); 373 smbfs_smb_close(np->n_mount->sm_share, np->n_fid, 374 &np->n_mtime, &scred); 375 } else if (vp->v_type == VDIR) { 376 if (np->n_dirseq != NULL) { 377 smbfs_findclose(np->n_dirseq, &scred); 378 np->n_dirseq = NULL; 379 } 380 } 381 np->n_flag &= ~NOPEN; 382 smbfs_attr_cacheremove(vp); 383 } 384 if (np->n_flag & NGONE) 385 vrecycle(vp, td); 386 return (0); 387 } 388 /* 389 * routines to maintain vnode attributes cache 390 * smbfs_attr_cacheenter: unpack np.i to vattr structure 391 */ 392 void 393 smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap) 394 { 395 struct smbnode *np = VTOSMB(vp); 396 397 if (vp->v_type == VREG) { 398 if (np->n_size != fap->fa_size) { 399 np->n_size = fap->fa_size; 400 vnode_pager_setsize(vp, np->n_size); 401 } 402 } else if (vp->v_type == VDIR) { 403 np->n_size = 16384; /* should be a better way ... */ 404 } else 405 return; 406 np->n_mtime = fap->fa_mtime; 407 np->n_dosattr = fap->fa_attr; 408 np->n_attrage = time_second; 409 return; 410 } 411 412 int 413 smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va) 414 { 415 struct smbnode *np = VTOSMB(vp); 416 struct smbmount *smp = VTOSMBFS(vp); 417 int diff; 418 419 diff = time_second - np->n_attrage; 420 if (diff > 2) /* XXX should be configurable */ 421 return ENOENT; 422 va->va_type = vp->v_type; /* vnode type (for create) */ 423 if (vp->v_type == VREG) { 424 va->va_mode = smp->sm_file_mode; /* files access mode and type */ 425 if (np->n_dosattr & SMB_FA_RDONLY) 426 va->va_mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH); 427 } else if (vp->v_type == VDIR) { 428 va->va_mode = smp->sm_dir_mode; /* files access mode and type */ 429 } else 430 return EINVAL; 431 va->va_size = np->n_size; 432 va->va_nlink = 1; /* number of references to file */ 433 va->va_uid = smp->sm_uid; /* owner user id */ 434 va->va_gid = smp->sm_gid; /* owner group id */ 435 va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 436 va->va_fileid = np->n_ino; /* file id */ 437 if (va->va_fileid == 0) 438 va->va_fileid = 2; 439 va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax; 440 va->va_mtime = np->n_mtime; 441 va->va_atime = va->va_ctime = va->va_mtime; /* time file changed */ 442 va->va_gen = VNOVAL; /* generation number of file */ 443 va->va_flags = 0; /* flags defined for file */ 444 va->va_rdev = VNOVAL; /* device the special file represents */ 445 va->va_bytes = va->va_size; /* bytes of disk space held by file */ 446 va->va_filerev = 0; /* file modification number */ 447 va->va_vaflags = 0; /* operations flags */ 448 return 0; 449 } 450