fs.c revision 1.13.4.1 1 /* $NetBSD: fs.c,v 1.13.4.1 2008/01/09 02:02:19 matt 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: fs.c,v 1.13.4.1 2008/01/09 02:02:19 matt Exp $");
31 #endif /* !lint */
32
33 #include <err.h>
34 #include <errno.h>
35 #include <puffs.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "psshfs.h"
42 #include "sftp_proto.h"
43
44 #define DO_IO(fname, a1, a2, a3, a4, rv) \
45 puffs_framebuf_seekset(a2, 0); \
46 *(a4) = 0; \
47 rv = fname(a1, a2, a3, a4); \
48 if (rv || a4 == 0) errx(1, "psshfs_domount io failed %d, %d", rv, *a4)
49
50 int
51 psshfs_domount(struct puffs_usermount *pu)
52 {
53 struct psshfs_ctx *pctx = puffs_getspecific(pu);
54 struct psshfs_node *root = &pctx->psn_root;
55 struct puffs_pathobj *po_root;
56 struct puffs_node *pn_root;
57 struct vattr va;
58 struct vattr *rva;
59 struct puffs_framebuf *pb;
60 char *rootpath;
61 uint32_t count;
62 int rv, done;
63
64 pb = psbuf_makeout();
65 psbuf_put_1(pb, SSH_FXP_INIT);
66 psbuf_put_4(pb, SFTP_PROTOVERSION);
67 DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
68
69 puffs_framebuf_recycle(pb);
70 DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
71 if (psbuf_get_type(pb) != SSH_FXP_VERSION)
72 errx(1, "invalid server response: %d", psbuf_get_type(pb));
73 pctx->protover = psbuf_get_reqid(pb);
74 /* might contain some other stuff, but we're not interested */
75
76 /* scope out our rootpath */
77 psbuf_recycleout(pb);
78 psbuf_put_1(pb, SSH_FXP_REALPATH);
79 psbuf_put_4(pb, NEXTREQ(pctx));
80 psbuf_put_str(pb, pctx->mountpath);
81 DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
82
83 puffs_framebuf_recycle(pb);
84 DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
85 if (psbuf_get_type(pb) != SSH_FXP_NAME)
86 errx(1, "invalid server realpath response for \"%s\"",
87 pctx->mountpath);
88 if (psbuf_get_4(pb, &count) == -1)
89 errx(1, "invalid realpath response: count");
90 if (psbuf_get_str(pb, &rootpath, NULL) == -1)
91 errx(1, "invalid realpath response: rootpath");
92
93 /* stat the rootdir so that we know it's a dir */
94 psbuf_recycleout(pb);
95 psbuf_req_str(pb, SSH_FXP_LSTAT, NEXTREQ(pctx), rootpath);
96 DO_IO(psbuf_write, pu, pb, pctx->sshfd, &done, rv);
97
98 puffs_framebuf_recycle(pb);
99 DO_IO(psbuf_read, pu, pb, pctx->sshfd, &done, rv);
100
101 rv = psbuf_expect_attrs(pb, &va);
102 if (rv)
103 errx(1, "couldn't stat rootpath");
104 puffs_framebuf_destroy(pb);
105
106 if (puffs_mode2vt(va.va_mode) != VDIR)
107 errx(1, "remote path (%s) not a directory", rootpath);
108
109 pctx->nextino = 2;
110
111 memset(root, 0, sizeof(struct psshfs_node));
112 pn_root = puffs_pn_new(pu, root);
113 if (pn_root == NULL)
114 return errno;
115 puffs_setroot(pu, pn_root);
116
117 po_root = puffs_getrootpathobj(pu);
118 if (po_root == NULL)
119 err(1, "getrootpathobj");
120 po_root->po_path = rootpath;
121 po_root->po_len = strlen(rootpath);
122
123 rva = &pn_root->pn_va;
124 puffs_setvattr(rva, &va);
125 rva->va_fileid = pctx->nextino++;
126 rva->va_nlink = 0156; /* XXX */
127
128 return 0;
129 }
130
131 int
132 psshfs_fs_unmount(struct puffs_usermount *pu, int flags)
133 {
134 struct psshfs_ctx *pctx = puffs_getspecific(pu);
135
136 kill(pctx->sshpid, SIGTERM);
137 close(pctx->sshfd);
138 return 0;
139 }
140
141 int
142 psshfs_fs_nodetofh(struct puffs_usermount *pu, void *cookie,
143 void *fid, size_t *fidsize)
144 {
145 struct psshfs_ctx *pctx = puffs_getspecific(pu);
146 struct puffs_node *pn = cookie;
147 struct psshfs_node *psn = pn->pn_data;
148 struct psshfs_fid *pf = fid;
149
150 pf->mounttime = pctx->mounttime;
151 pf->node = pn;
152
153 psn->stat |= PSN_HASFH;
154
155 return 0;
156 }
157
158 int
159 psshfs_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
160 struct puffs_newinfo *pni)
161 {
162 struct psshfs_ctx *pctx = puffs_getspecific(pu);
163 struct psshfs_fid *pf = fid;
164 struct puffs_node *pn = pf->node;
165 struct psshfs_node *psn;
166 int rv;
167
168 if (pf->mounttime != pctx->mounttime)
169 return EINVAL;
170 if (pn == 0)
171 return EINVAL;
172 psn = pn->pn_data;
173 if ((psn->stat & PSN_HASFH) == 0)
174 return EINVAL;
175
176 /* update node attributes */
177 rv = getnodeattr(pu, pn);
178 if (rv)
179 return EINVAL;
180
181 puffs_newinfo_setcookie(pni, pn);
182 puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
183 puffs_newinfo_setsize(pni, pn->pn_va.va_size);
184
185 return 0;
186 }
187