1d167cf6fSWarner Losh /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d63027b6SPedro F. Giffuni *
4681a5bbeSBoris Popov * Copyright (c) 2000-2001 Boris Popov
5681a5bbeSBoris Popov * All rights reserved.
6681a5bbeSBoris Popov *
7681a5bbeSBoris Popov * Redistribution and use in source and binary forms, with or without
8681a5bbeSBoris Popov * modification, are permitted provided that the following conditions
9681a5bbeSBoris Popov * are met:
10681a5bbeSBoris Popov * 1. Redistributions of source code must retain the above copyright
11681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer.
12681a5bbeSBoris Popov * 2. Redistributions in binary form must reproduce the above copyright
13681a5bbeSBoris Popov * notice, this list of conditions and the following disclaimer in the
14681a5bbeSBoris Popov * documentation and/or other materials provided with the distribution.
15681a5bbeSBoris Popov *
16681a5bbeSBoris Popov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17681a5bbeSBoris Popov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18681a5bbeSBoris Popov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19681a5bbeSBoris Popov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20681a5bbeSBoris Popov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21681a5bbeSBoris Popov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22681a5bbeSBoris Popov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23681a5bbeSBoris Popov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24681a5bbeSBoris Popov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25681a5bbeSBoris Popov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26681a5bbeSBoris Popov * SUCH DAMAGE.
27681a5bbeSBoris Popov */
28681a5bbeSBoris Popov #include <sys/param.h>
29681a5bbeSBoris Popov #include <sys/systm.h>
308680dc80SDavide Italiano #include <sys/fnv_hash.h>
31681a5bbeSBoris Popov #include <sys/kernel.h>
32fb919e4dSMark Murray #include <sys/lock.h>
33681a5bbeSBoris Popov #include <sys/malloc.h>
34fb919e4dSMark Murray #include <sys/mount.h>
35fb919e4dSMark Murray #include <sys/proc.h>
36681a5bbeSBoris Popov #include <sys/queue.h>
37fb8e9eadSBoris Popov #include <sys/stat.h>
387947229fSRobert Watson #include <sys/sx.h>
39fb919e4dSMark Murray #include <sys/sysctl.h>
40fb919e4dSMark Murray #include <sys/time.h>
41fb919e4dSMark Murray #include <sys/vnode.h>
42681a5bbeSBoris Popov
43681a5bbeSBoris Popov #include <netsmb/smb.h>
44681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
45681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
46681a5bbeSBoris Popov
47fb919e4dSMark Murray #include <vm/vm.h>
48fb919e4dSMark Murray #include <vm/vm_extern.h>
49fb919e4dSMark Murray /*#include <vm/vm_page.h>
50fb919e4dSMark Murray #include <vm/vm_object.h>*/
51fb919e4dSMark Murray
52681a5bbeSBoris Popov #include <fs/smbfs/smbfs.h>
53681a5bbeSBoris Popov #include <fs/smbfs/smbfs_node.h>
54681a5bbeSBoris Popov #include <fs/smbfs/smbfs_subr.h>
55681a5bbeSBoris Popov
56aec0fb7bSPoul-Henning Kamp extern struct vop_vector smbfs_vnodeops; /* XXX -> .h file */
57681a5bbeSBoris Popov
58d745c852SEd Schouten static MALLOC_DEFINE(M_SMBNODE, "smbufs_node", "SMBFS vnode private part");
595bb84bc8SRobert Watson static MALLOC_DEFINE(M_SMBNODENAME, "smbufs_nname", "SMBFS node name");
60681a5bbeSBoris Popov
618680dc80SDavide Italiano u_int32_t __inline
smbfs_hash(const u_char * name,int nmlen)62681a5bbeSBoris Popov smbfs_hash(const u_char *name, int nmlen)
63681a5bbeSBoris Popov {
648680dc80SDavide Italiano return (fnv_32_buf(name, nmlen, FNV1_32_INIT));
65681a5bbeSBoris Popov }
66681a5bbeSBoris Popov
67681a5bbeSBoris Popov static char *
smbfs_name_alloc(const u_char * name,int nmlen)68681a5bbeSBoris Popov smbfs_name_alloc(const u_char *name, int nmlen)
69681a5bbeSBoris Popov {
70681a5bbeSBoris Popov u_char *cp;
71681a5bbeSBoris Popov
72681a5bbeSBoris Popov nmlen++;
73a163d034SWarner Losh cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK);
74681a5bbeSBoris Popov bcopy(name, cp, nmlen - 1);
75681a5bbeSBoris Popov cp[nmlen - 1] = 0;
76681a5bbeSBoris Popov return cp;
77681a5bbeSBoris Popov }
78681a5bbeSBoris Popov
79681a5bbeSBoris Popov static void
smbfs_name_free(u_char * name)80681a5bbeSBoris Popov smbfs_name_free(u_char *name)
81681a5bbeSBoris Popov {
82681a5bbeSBoris Popov
83681a5bbeSBoris Popov free(name, M_SMBNODENAME);
84681a5bbeSBoris Popov }
85681a5bbeSBoris Popov
868680dc80SDavide Italiano static int __inline
smbfs_vnode_cmp(struct vnode * vp,void * _sc)878680dc80SDavide Italiano smbfs_vnode_cmp(struct vnode *vp, void *_sc)
888680dc80SDavide Italiano {
898680dc80SDavide Italiano struct smbnode *np;
908680dc80SDavide Italiano struct smbcmp *sc;
918680dc80SDavide Italiano
92bbc6d2c1SDavide Italiano np = (struct smbnode *) vp->v_data;
938680dc80SDavide Italiano sc = (struct smbcmp *) _sc;
948680dc80SDavide Italiano if (np->n_parent != sc->n_parent || np->n_nmlen != sc->n_nmlen ||
958680dc80SDavide Italiano bcmp(sc->n_name, np->n_name, sc->n_nmlen) != 0)
968680dc80SDavide Italiano return 1;
978680dc80SDavide Italiano return 0;
988680dc80SDavide Italiano }
998680dc80SDavide Italiano
100681a5bbeSBoris Popov static int
smbfs_node_alloc(struct mount * mp,struct vnode * dvp,const char * dirnm,int dirlen,const char * name,int nmlen,char sep,struct smbfattr * fap,struct vnode ** vpp)10180704a47SDavide Italiano smbfs_node_alloc(struct mount *mp, struct vnode *dvp, const char *dirnm,
10280704a47SDavide Italiano int dirlen, const char *name, int nmlen, char sep,
10380704a47SDavide Italiano struct smbfattr *fap, struct vnode **vpp)
104681a5bbeSBoris Popov {
105b4484bf0STim J. Robbins struct vattr vattr;
106b1c996c4SBoris Popov struct thread *td = curthread; /* XXX */
107681a5bbeSBoris Popov struct smbmount *smp = VFSTOSMBFS(mp);
1088680dc80SDavide Italiano struct smbnode *np, *dnp;
1098680dc80SDavide Italiano struct vnode *vp, *vp2;
1108680dc80SDavide Italiano struct smbcmp sc;
11180704a47SDavide Italiano char *p, *rpath;
11280704a47SDavide Italiano int error, rplen;
113681a5bbeSBoris Popov
1148680dc80SDavide Italiano sc.n_parent = dvp;
1158680dc80SDavide Italiano sc.n_nmlen = nmlen;
1168680dc80SDavide Italiano sc.n_name = name;
117681a5bbeSBoris Popov if (smp->sm_root != NULL && dvp == NULL) {
118681a5bbeSBoris Popov SMBERROR("do not allocate root vnode twice!\n");
119681a5bbeSBoris Popov return EINVAL;
120681a5bbeSBoris Popov }
121681a5bbeSBoris Popov if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
122681a5bbeSBoris Popov if (dvp == NULL)
123681a5bbeSBoris Popov return EINVAL;
12411de0c59STim J. Robbins vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
125a92a971bSMateusz Guzik error = vget(vp, LK_EXCLUSIVE);
126681a5bbeSBoris Popov if (error == 0)
127681a5bbeSBoris Popov *vpp = vp;
128681a5bbeSBoris Popov return error;
129681a5bbeSBoris Popov } else if (nmlen == 1 && name[0] == '.') {
130681a5bbeSBoris Popov SMBERROR("do not call me with dot!\n");
131681a5bbeSBoris Popov return EINVAL;
132681a5bbeSBoris Popov }
133681a5bbeSBoris Popov dnp = dvp ? VTOSMB(dvp) : NULL;
134681a5bbeSBoris Popov if (dnp == NULL && dvp != NULL) {
135411455a8SEdward Tomasz Napierala vn_printf(dvp, "smbfs_node_alloc: dead parent vnode ");
136681a5bbeSBoris Popov return EINVAL;
137681a5bbeSBoris Popov }
1388680dc80SDavide Italiano error = vfs_hash_get(mp, smbfs_hash(name, nmlen), LK_EXCLUSIVE, td,
1398680dc80SDavide Italiano vpp, smbfs_vnode_cmp, &sc);
1408680dc80SDavide Italiano if (error)
1418680dc80SDavide Italiano return (error);
1428680dc80SDavide Italiano if (*vpp) {
1438680dc80SDavide Italiano np = VTOSMB(*vpp);
144b4484bf0STim J. Robbins /* Force cached attributes to be refreshed if stale. */
1458680dc80SDavide Italiano (void)VOP_GETATTR(*vpp, &vattr, td->td_ucred);
146b4484bf0STim J. Robbins /*
147b4484bf0STim J. Robbins * If the file type on the server is inconsistent with
148b4484bf0STim J. Robbins * what it was when we created the vnode, kill the
149b4484bf0STim J. Robbins * bogus vnode now and fall through to the code below
150b4484bf0STim J. Robbins * to create a new one with the right type.
151b4484bf0STim J. Robbins */
1528680dc80SDavide Italiano if (((*vpp)->v_type == VDIR &&
1538680dc80SDavide Italiano (np->n_dosattr & SMB_FA_DIR) == 0) ||
1548680dc80SDavide Italiano ((*vpp)->v_type == VREG &&
1558680dc80SDavide Italiano (np->n_dosattr & SMB_FA_DIR) != 0)) {
1568680dc80SDavide Italiano vgone(*vpp);
1578680dc80SDavide Italiano vput(*vpp);
158b4484bf0STim J. Robbins }
1598680dc80SDavide Italiano else {
1608680dc80SDavide Italiano SMBVDEBUG("vnode taken from the hashtable\n");
1618680dc80SDavide Italiano return (0);
162681a5bbeSBoris Popov }
1638680dc80SDavide Italiano }
164681a5bbeSBoris Popov /*
165681a5bbeSBoris Popov * If we don't have node attributes, then it is an explicit lookup
166681a5bbeSBoris Popov * for an existing vnode.
167681a5bbeSBoris Popov */
168681a5bbeSBoris Popov if (fap == NULL)
169681a5bbeSBoris Popov return ENOENT;
170681a5bbeSBoris Popov
1718680dc80SDavide Italiano error = getnewvnode("smbfs", mp, &smbfs_vnodeops, vpp);
1728680dc80SDavide Italiano if (error)
17361b9d89fSTor Egge return (error);
1748680dc80SDavide Italiano vp = *vpp;
17501cc0b65SChristian Brueffer np = malloc(sizeof *np, M_SMBNODE, M_WAITOK | M_ZERO);
17680704a47SDavide Italiano rplen = dirlen;
17780704a47SDavide Italiano if (sep != '\0')
17880704a47SDavide Italiano rplen++;
17980704a47SDavide Italiano rplen += nmlen;
18080704a47SDavide Italiano rpath = malloc(rplen + 1, M_SMBNODENAME, M_WAITOK);
18180704a47SDavide Italiano p = rpath;
18280704a47SDavide Italiano bcopy(dirnm, p, dirlen);
18380704a47SDavide Italiano p += dirlen;
18480704a47SDavide Italiano if (sep != '\0')
18580704a47SDavide Italiano *p++ = sep;
18680704a47SDavide Italiano if (name != NULL) {
18780704a47SDavide Italiano bcopy(name, p, nmlen);
18880704a47SDavide Italiano p += nmlen;
18980704a47SDavide Italiano }
19042039c5bSDavide Italiano *p = '\0';
19180704a47SDavide Italiano MPASS(p == rpath + rplen);
1928680dc80SDavide Italiano lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1938680dc80SDavide Italiano /* Vnode initialization */
194681a5bbeSBoris Popov vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
195681a5bbeSBoris Popov vp->v_data = np;
196681a5bbeSBoris Popov np->n_vnode = vp;
197681a5bbeSBoris Popov np->n_mount = VFSTOSMBFS(mp);
19880704a47SDavide Italiano np->n_rpath = rpath;
19980704a47SDavide Italiano np->n_rplen = rplen;
200681a5bbeSBoris Popov np->n_nmlen = nmlen;
201681a5bbeSBoris Popov np->n_name = smbfs_name_alloc(name, nmlen);
202681a5bbeSBoris Popov np->n_ino = fap->fa_ino;
203681a5bbeSBoris Popov if (dvp) {
204e6e370a7SJeff Roberson ASSERT_VOP_LOCKED(dvp, "smbfs_node_alloc");
20511de0c59STim J. Robbins np->n_parent = dvp;
20680704a47SDavide Italiano np->n_parentino = VTOSMB(dvp)->n_ino;
207e6e370a7SJeff Roberson if (/*vp->v_type == VDIR &&*/ (dvp->v_vflag & VV_ROOT) == 0) {
208681a5bbeSBoris Popov vref(dvp);
209681a5bbeSBoris Popov np->n_flag |= NREFPARENT;
210681a5bbeSBoris Popov }
211681a5bbeSBoris Popov } else if (vp->v_type == VREG)
212681a5bbeSBoris Popov SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
2138680dc80SDavide Italiano error = insmntque(vp, mp);
2148680dc80SDavide Italiano if (error) {
2158680dc80SDavide Italiano free(np, M_SMBNODE);
2168680dc80SDavide Italiano return (error);
217681a5bbeSBoris Popov }
218829f0bcbSMateusz Guzik vn_set_state(vp, VSTATE_CONSTRUCTED);
2198680dc80SDavide Italiano error = vfs_hash_insert(vp, smbfs_hash(name, nmlen), LK_EXCLUSIVE,
2208680dc80SDavide Italiano td, &vp2, smbfs_vnode_cmp, &sc);
2218680dc80SDavide Italiano if (error)
2228680dc80SDavide Italiano return (error);
2238680dc80SDavide Italiano if (vp2 != NULL)
2248680dc80SDavide Italiano *vpp = vp2;
2258680dc80SDavide Italiano return (0);
226681a5bbeSBoris Popov }
227681a5bbeSBoris Popov
228681a5bbeSBoris Popov int
smbfs_nget(struct mount * mp,struct vnode * dvp,const char * name,int nmlen,struct smbfattr * fap,struct vnode ** vpp)229681a5bbeSBoris Popov smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
230681a5bbeSBoris Popov struct smbfattr *fap, struct vnode **vpp)
231681a5bbeSBoris Popov {
232771e4a86SJohn Baldwin struct smbnode *dnp;
233681a5bbeSBoris Popov struct vnode *vp;
23480704a47SDavide Italiano int error, sep;
235681a5bbeSBoris Popov
23680704a47SDavide Italiano dnp = (dvp) ? VTOSMB(dvp) : NULL;
23780704a47SDavide Italiano sep = 0;
23880704a47SDavide Italiano if (dnp != NULL) {
23980704a47SDavide Italiano sep = SMBFS_DNP_SEP(dnp);
24080704a47SDavide Italiano error = smbfs_node_alloc(mp, dvp, dnp->n_rpath, dnp->n_rplen,
24180704a47SDavide Italiano name, nmlen, sep, fap, &vp);
24280704a47SDavide Italiano } else
24380704a47SDavide Italiano error = smbfs_node_alloc(mp, NULL, "\\", 1, name, nmlen,
24480704a47SDavide Italiano sep, fap, &vp);
245681a5bbeSBoris Popov if (error)
246681a5bbeSBoris Popov return error;
24780704a47SDavide Italiano MPASS(vp != NULL);
248681a5bbeSBoris Popov if (fap)
249681a5bbeSBoris Popov smbfs_attr_cacheenter(vp, fap);
250681a5bbeSBoris Popov *vpp = vp;
251681a5bbeSBoris Popov return 0;
252681a5bbeSBoris Popov }
253681a5bbeSBoris Popov
254681a5bbeSBoris Popov /*
255681a5bbeSBoris Popov * Free smbnode, and give vnode back to system
256681a5bbeSBoris Popov */
257681a5bbeSBoris Popov int
smbfs_reclaim(struct vop_reclaim_args * ap)258b09b03a1SMateusz Guzik smbfs_reclaim(struct vop_reclaim_args *ap)
259681a5bbeSBoris Popov {
260681a5bbeSBoris Popov struct vnode *vp = ap->a_vp;
261681a5bbeSBoris Popov struct vnode *dvp;
262681a5bbeSBoris Popov struct smbnode *np = VTOSMB(vp);
263681a5bbeSBoris Popov struct smbmount *smp = VTOSMBFS(vp);
264681a5bbeSBoris Popov
2654d93c0beSJeff Roberson SMBVDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
266681a5bbeSBoris Popov
2672a4ad258STim J. Robbins KASSERT((np->n_flag & NOPEN) == 0, ("file not closed before reclaim"));
2682a4ad258STim J. Robbins
269681a5bbeSBoris Popov dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
27011de0c59STim J. Robbins np->n_parent : NULL;
271681a5bbeSBoris Popov
2728680dc80SDavide Italiano /*
2738680dc80SDavide Italiano * Remove the vnode from its hash chain.
2748680dc80SDavide Italiano */
2758680dc80SDavide Italiano vfs_hash_remove(vp);
276681a5bbeSBoris Popov if (np->n_name)
277681a5bbeSBoris Popov smbfs_name_free(np->n_name);
27880704a47SDavide Italiano if (np->n_rpath)
27980704a47SDavide Italiano free(np->n_rpath, M_SMBNODENAME);
2801ede983cSDag-Erling Smørgrav free(np, M_SMBNODE);
2818680dc80SDavide Italiano vp->v_data = NULL;
28270a3c70aSTim J. Robbins if (dvp != NULL) {
283681a5bbeSBoris Popov vrele(dvp);
284578dcf0cSTim J. Robbins /*
285578dcf0cSTim J. Robbins * Indicate that we released something; see comment
286578dcf0cSTim J. Robbins * in smbfs_unmount().
287578dcf0cSTim J. Robbins */
288578dcf0cSTim J. Robbins smp->sm_didrele = 1;
289681a5bbeSBoris Popov }
290681a5bbeSBoris Popov return 0;
291681a5bbeSBoris Popov }
292681a5bbeSBoris Popov
293681a5bbeSBoris Popov int
smbfs_inactive(struct vop_inactive_args * ap)294b09b03a1SMateusz Guzik smbfs_inactive(struct vop_inactive_args *ap)
295681a5bbeSBoris Popov {
296ab21ed17SMateusz Guzik struct thread *td = curthread;
297a854ed98SJohn Baldwin struct ucred *cred = td->td_ucred;
298681a5bbeSBoris Popov struct vnode *vp = ap->a_vp;
299681a5bbeSBoris Popov struct smbnode *np = VTOSMB(vp);
300afe09751SDavide Italiano struct smb_cred *scred;
3012a4ad258STim J. Robbins struct vattr va;
302681a5bbeSBoris Popov
3034d93c0beSJeff Roberson SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vrefcnt(vp));
3042a4ad258STim J. Robbins if ((np->n_flag & NOPEN) != 0) {
305afe09751SDavide Italiano scred = smbfs_malloc_scred();
306afe09751SDavide Italiano smb_makescred(scred, td, cred);
307e50508dfSPoul-Henning Kamp smbfs_vinvalbuf(vp, td);
3082a4ad258STim J. Robbins if (vp->v_type == VREG) {
3090359a12eSAttilio Rao VOP_GETATTR(vp, &va, cred);
3102a4ad258STim J. Robbins smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
311afe09751SDavide Italiano &np->n_mtime, scred);
3122a4ad258STim J. Robbins } else if (vp->v_type == VDIR) {
3132a4ad258STim J. Robbins if (np->n_dirseq != NULL) {
314afe09751SDavide Italiano smbfs_findclose(np->n_dirseq, scred);
3152a4ad258STim J. Robbins np->n_dirseq = NULL;
316681a5bbeSBoris Popov }
3172a4ad258STim J. Robbins }
3182a4ad258STim J. Robbins np->n_flag &= ~NOPEN;
3192a4ad258STim J. Robbins smbfs_attr_cacheremove(vp);
320afe09751SDavide Italiano smbfs_free_scred(scred);
321208a7a97STim J. Robbins }
322b4484bf0STim J. Robbins if (np->n_flag & NGONE)
323af6e6b87SEdward Tomasz Napierala vrecycle(vp);
324681a5bbeSBoris Popov return (0);
325681a5bbeSBoris Popov }
326681a5bbeSBoris Popov /*
327681a5bbeSBoris Popov * routines to maintain vnode attributes cache
328681a5bbeSBoris Popov * smbfs_attr_cacheenter: unpack np.i to vattr structure
329681a5bbeSBoris Popov */
330681a5bbeSBoris Popov void
smbfs_attr_cacheenter(struct vnode * vp,struct smbfattr * fap)331681a5bbeSBoris Popov smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
332681a5bbeSBoris Popov {
333681a5bbeSBoris Popov struct smbnode *np = VTOSMB(vp);
334681a5bbeSBoris Popov
335681a5bbeSBoris Popov if (vp->v_type == VREG) {
336681a5bbeSBoris Popov if (np->n_size != fap->fa_size) {
337681a5bbeSBoris Popov np->n_size = fap->fa_size;
338681a5bbeSBoris Popov vnode_pager_setsize(vp, np->n_size);
339681a5bbeSBoris Popov }
340681a5bbeSBoris Popov } else if (vp->v_type == VDIR) {
341681a5bbeSBoris Popov np->n_size = 16384; /* should be a better way ... */
342681a5bbeSBoris Popov } else
343681a5bbeSBoris Popov return;
344681a5bbeSBoris Popov np->n_mtime = fap->fa_mtime;
345681a5bbeSBoris Popov np->n_dosattr = fap->fa_attr;
346681a5bbeSBoris Popov np->n_attrage = time_second;
347681a5bbeSBoris Popov return;
348681a5bbeSBoris Popov }
349681a5bbeSBoris Popov
350681a5bbeSBoris Popov int
smbfs_attr_cachelookup(struct vnode * vp,struct vattr * va)351681a5bbeSBoris Popov smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
352681a5bbeSBoris Popov {
353681a5bbeSBoris Popov struct smbnode *np = VTOSMB(vp);
354681a5bbeSBoris Popov struct smbmount *smp = VTOSMBFS(vp);
355681a5bbeSBoris Popov int diff;
356681a5bbeSBoris Popov
357681a5bbeSBoris Popov diff = time_second - np->n_attrage;
358681a5bbeSBoris Popov if (diff > 2) /* XXX should be configurable */
359681a5bbeSBoris Popov return ENOENT;
360681a5bbeSBoris Popov va->va_type = vp->v_type; /* vnode type (for create) */
3617da1a731SKenneth D. Merry va->va_flags = 0; /* flags defined for file */
362681a5bbeSBoris Popov if (vp->v_type == VREG) {
363d14c8441SPoul-Henning Kamp va->va_mode = smp->sm_file_mode; /* files access mode and type */
3647da1a731SKenneth D. Merry if (np->n_dosattr & SMB_FA_RDONLY) {
365fb8e9eadSBoris Popov va->va_mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
3667da1a731SKenneth D. Merry va->va_flags |= UF_READONLY;
3677da1a731SKenneth D. Merry }
368681a5bbeSBoris Popov } else if (vp->v_type == VDIR) {
369d14c8441SPoul-Henning Kamp va->va_mode = smp->sm_dir_mode; /* files access mode and type */
370681a5bbeSBoris Popov } else
371681a5bbeSBoris Popov return EINVAL;
372681a5bbeSBoris Popov va->va_size = np->n_size;
373681a5bbeSBoris Popov va->va_nlink = 1; /* number of references to file */
374d14c8441SPoul-Henning Kamp va->va_uid = smp->sm_uid; /* owner user id */
375d14c8441SPoul-Henning Kamp va->va_gid = smp->sm_gid; /* owner group id */
376681a5bbeSBoris Popov va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
377681a5bbeSBoris Popov va->va_fileid = np->n_ino; /* file id */
378681a5bbeSBoris Popov if (va->va_fileid == 0)
379681a5bbeSBoris Popov va->va_fileid = 2;
380681a5bbeSBoris Popov va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax;
381681a5bbeSBoris Popov va->va_mtime = np->n_mtime;
382681a5bbeSBoris Popov va->va_atime = va->va_ctime = va->va_mtime; /* time file changed */
383681a5bbeSBoris Popov va->va_gen = VNOVAL; /* generation number of file */
3847da1a731SKenneth D. Merry if (np->n_dosattr & SMB_FA_HIDDEN)
3857da1a731SKenneth D. Merry va->va_flags |= UF_HIDDEN;
3867da1a731SKenneth D. Merry if (np->n_dosattr & SMB_FA_SYSTEM)
3877da1a731SKenneth D. Merry va->va_flags |= UF_SYSTEM;
3887da1a731SKenneth D. Merry /*
3897da1a731SKenneth D. Merry * We don't set the archive bit for directories.
3907da1a731SKenneth D. Merry */
3917da1a731SKenneth D. Merry if ((vp->v_type != VDIR) && (np->n_dosattr & SMB_FA_ARCHIVE))
3927da1a731SKenneth D. Merry va->va_flags |= UF_ARCHIVE;
3934c5a20e3SKonstantin Belousov va->va_rdev = NODEV; /* device the special file represents */
394681a5bbeSBoris Popov va->va_bytes = va->va_size; /* bytes of disk space held by file */
395681a5bbeSBoris Popov va->va_filerev = 0; /* file modification number */
396681a5bbeSBoris Popov va->va_vaflags = 0; /* operations flags */
397681a5bbeSBoris Popov return 0;
398681a5bbeSBoris Popov }
399