fs.c revision 1.5 1 /* $NetBSD: fs.c,v 1.5 2007/04/12 20:42:46 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2006 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the company nor the name of the author may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: fs.c,v 1.5 2007/04/12 20:42:46 pooka Exp $");
34 #endif /* !lint */
35
36 #include <err.h>
37 #include <errno.h>
38 #include <puffs.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 #include "psshfs.h"
45 #include "sftp_proto.h"
46
47 int
48 psshfs_domount(struct puffs_usermount *pu)
49 {
50 struct statvfs sb;
51 struct psshfs_ctx *pctx = puffs_getspecific(pu);
52 struct psshfs_node *root = &pctx->psn_root;
53 struct puffs_pathobj *po_root;
54 struct puffs_node *pn_root;
55 struct vattr va;
56 struct vattr *rva;
57 struct psbuf *pb;
58 char *rootpath;
59 uint32_t count;
60 int rv;
61
62 pb = psbuf_make(PSB_OUT);
63 psbuf_put_1(pb, SSH_FXP_INIT);
64 psbuf_put_4(pb, SFTP_PROTOVERSION);
65
66 while ((rv = psbuf_write(pctx, pb)) != 1)
67 if (rv == -1)
68 err(1, "write handshake");
69
70 psbuf_destroy(pb);
71 pb = psbuf_make(PSB_IN);
72
73 while ((rv = psbuf_read(pctx, pb)) != 1)
74 if (rv == -1)
75 err(1, "read handshake response");
76
77 if (pb->type != SSH_FXP_VERSION)
78 errx(1, "invalid server response");
79 pctx->protover = pb->reqid;
80
81 /* might contain some other stuff, but we're not interested */
82
83 /* scope out our rootpath */
84 psbuf_recycle(pb, PSB_OUT);
85 psbuf_put_1(pb, SSH_FXP_REALPATH);
86 psbuf_put_4(pb, NEXTREQ(pctx));
87 psbuf_put_str(pb, pctx->mountpath);
88 while ((rv = psbuf_write(pctx, pb)) != 1)
89 if (rv == -1)
90 err(1, "realpath query");
91
92 psbuf_recycle(pb, PSB_IN);
93
94 while ((rv = psbuf_read(pctx, pb)) != 1)
95 if (rv == -1)
96 err(1, "read realpath query response");
97 if (pb->type != SSH_FXP_NAME)
98 errx(1, "invalid server realpath response for \"%s\"",
99 pctx->mountpath);
100
101 if (!psbuf_get_4(pb, &count))
102 errx(1, "invalid realpath response: count");
103 if (!psbuf_get_str(pb, &rootpath, NULL))
104 errx(1, "invalid realpath response: rootpath");
105
106 /* stat the rootdir so that we know it's a dir */
107 psbuf_recycle(pb, PSB_OUT);
108 psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
109 while ((rv == psbuf_write(pctx, pb)) != 1)
110 if (rv == -1)
111 errx(1, "lstat");
112
113 psbuf_recycle(pb, PSB_IN);
114
115 while ((rv = psbuf_read(pctx, pb)) != 1)
116 if (rv == -1)
117 errx(1, "read lstat response");
118
119 rv = psbuf_expect_attrs(pb, &va);
120 if (rv)
121 errx(1, "couldn't stat rootpath");
122 psbuf_destroy(pb);
123
124 if (puffs_mode2vt(va.va_mode) != VDIR)
125 errx(1, "remote path (%s) not a directory", rootpath);
126
127 pctx->nextino = 2;
128
129 memset(root, 0, sizeof(struct psshfs_node));
130 pn_root = puffs_pn_new(pu, root);
131 if (pn_root == NULL)
132 return errno;
133 puffs_setroot(pu, pn_root);
134
135 po_root = puffs_getrootpathobj(pu);
136 if (po_root == NULL)
137 err(1, "getrootpathobj");
138 po_root->po_path = rootpath;
139 po_root->po_len = strlen(rootpath);
140
141 rva = &pn_root->pn_va;
142 puffs_setvattr(rva, &va);
143 rva->va_fileid = pctx->nextino++;
144 rva->va_nlink = 0156; /* XXX */
145
146 puffs_zerostatvfs(&sb);
147 if (puffs_start(pu, pn_root, &sb) != 0)
148 return errno;
149
150 return 0;
151 }
152
153 int
154 psshfs_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
155 {
156 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
157 struct psshfs_ctx *pctx = puffs_getspecific(pu);
158
159 kill(pctx->sshpid, SIGTERM);
160 close(pctx->sshfd);
161 return 0;
162 }
163
164 int
165 psshfs_fs_nodetofh(struct puffs_cc *pcc, void *cookie,
166 void *fid, size_t *fidsize)
167 {
168 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
169 struct psshfs_ctx *pctx = puffs_getspecific(pu);
170 struct puffs_node *pn = cookie;
171 struct psshfs_node *psn = pn->pn_data;
172 struct psshfs_fid *pf = fid;
173
174 pf->mounttime = pctx->mounttime;
175 pf->node = pn;
176
177 psn->hasfh = 1;
178
179 return 0;
180 }
181
182 int
183 psshfs_fs_fhtonode(struct puffs_cc *pcc, void *fid, size_t fidsize,
184 void **fcookie, enum vtype *ftype, voff_t *fsize, dev_t *fdev)
185 {
186 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
187 struct psshfs_ctx *pctx = puffs_getspecific(pu);
188 struct psshfs_fid *pf = fid;
189 struct puffs_node *pn = pf->node;
190 struct psshfs_node *psn;
191 int rv;
192
193 if (pf->mounttime != pctx->mounttime)
194 return EINVAL;
195 if (pn == 0)
196 return EINVAL;
197 psn = pn->pn_data;
198 if (psn->hasfh == 0)
199 return EINVAL;
200
201 /* update node attributes */
202 rv = getnodeattr(pcc, pn);
203 if (rv) {
204 psn->hasfh = 0;
205 if (psn->reclaimed == 2)
206 psshfs_node_reclaim(pcc, pn, 0);
207
208 return EINVAL;
209 }
210
211 *fcookie = pn;
212 *ftype = pn->pn_va.va_type;
213 *fsize = pn->pn_va.va_size;
214
215 return 0;
216 }
217