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