null.c revision 1.16 1 /* $NetBSD: null.c,v 1.16 2007/06/24 17:55:07 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007 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 #if !defined(lint)
30 __RCSID("$NetBSD: null.c,v 1.16 2007/06/24 17:55:07 pooka Exp $");
31 #endif /* !lint */
32
33 /*
34 * A "nullfs" using puffs, i.e. maps one location in the hierarchy
35 * to another using standard system calls.
36 */
37
38 #include <sys/types.h>
39 #include <sys/time.h>
40
41 #include <assert.h>
42 #include <dirent.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <puffs.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49
50 PUFFSOP_PROTOS(puffs_null)
51
52 /*
53 * set attributes to what is specified. XXX: no rollback in case of failure
54 */
55 static int
56 processvattr(const char *path, const struct vattr *va, int regular)
57 {
58 struct timeval tv[2];
59
60 /* XXX: -1 == PUFFS_VNOVAL, but shouldn't trust that */
61 if (va->va_uid != (unsigned)-1 || va->va_gid != (unsigned)-1)
62 if (lchown(path, va->va_uid, va->va_gid) == -1)
63 return errno;
64
65 if (va->va_mode != (unsigned)PUFFS_VNOVAL)
66 if (lchmod(path, va->va_mode) == -1)
67 return errno;
68
69 /* sloppy */
70 if (va->va_atime.tv_sec != (unsigned)PUFFS_VNOVAL
71 || va->va_mtime.tv_sec != (unsigned)PUFFS_VNOVAL) {
72 TIMESPEC_TO_TIMEVAL(&tv[0], &va->va_atime);
73 TIMESPEC_TO_TIMEVAL(&tv[1], &va->va_mtime);
74
75 if (lutimes(path, tv) == -1)
76 return errno;
77 }
78
79 if (regular && va->va_size != (u_quad_t)PUFFS_VNOVAL)
80 if (truncate(path, (off_t)va->va_size) == -1)
81 return errno;
82
83 return 0;
84 }
85
86 /*
87 * Kludge to open files which aren't writable *any longer*. This kinda
88 * works because the vfs layer does validation checks based on the file's
89 * permissions to allow writable opening before opening them. However,
90 * the problem arises if we want to create a file, write to it (cache),
91 * adjust permissions and then flush the file.
92 */
93 static int
94 writeableopen(const char *path)
95 {
96 struct stat sb;
97 mode_t origmode;
98 int sverr = 0;
99 int fd;
100
101 fd = open(path, O_WRONLY);
102 if (fd == -1) {
103 if (errno == EACCES) {
104 if (stat(path, &sb) == -1)
105 return -1;
106 origmode = sb.st_mode & ALLPERMS;
107
108 if (chmod(path, 0200) == -1)
109 return -1;
110
111 fd = open(path, O_WRONLY);
112 if (fd == -1)
113 sverr = errno;
114
115 chmod(path, origmode);
116 if (sverr)
117 errno = sverr;
118 } else
119 return -1;
120 }
121
122 return fd;
123 }
124
125 /*ARGSUSED*/
126 static void *
127 inodecmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
128 {
129 ino_t *cmpino = arg;
130
131 if (pn->pn_va.va_fileid == *cmpino)
132 return pn;
133 return NULL;
134 }
135
136 /*ARGSUSED*/
137 int
138 puffs_null_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
139 {
140 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
141
142 if (statvfs(PNPATH(puffs_getroot(pu)), svfsb) == -1)
143 return errno;
144
145 return 0;
146 }
147
148 int
149 puffs_null_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
150 enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
151 const struct puffs_cn *pcn)
152 {
153 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
154 struct puffs_node *pn = opc, *pn_res;
155 struct stat sb;
156 int rv;
157
158 assert(pn->pn_va.va_type == VDIR);
159
160 /*
161 * Note to whoever is copypasting this: you must first check
162 * if the node is there and only then do nodewalk. Alternatively
163 * you could make sure that you don't return unlinked/rmdir'd
164 * nodes in some other fashion
165 */
166 rv = lstat(PCNPATH(pcn), &sb);
167 if (rv)
168 return errno;
169
170 /* XXX2: nodewalk is a bit too slow here */
171 pn_res = puffs_pn_nodewalk(pu, inodecmp, &sb.st_ino);
172
173 if (pn_res == NULL) {
174 pn_res = puffs_pn_new(pu, NULL);
175 if (pn_res == NULL)
176 return ENOMEM;
177 puffs_stat2vattr(&pn_res->pn_va, &sb);
178 }
179
180 *newnode = pn_res;
181 *newtype = pn_res->pn_va.va_type;
182 *newsize = pn_res->pn_va.va_size;
183 *newrdev = pn_res->pn_va.va_rdev;
184
185 return 0;
186 }
187
188 /*ARGSUSED*/
189 int
190 puffs_null_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
191 const struct puffs_cn *pcn, const struct vattr *va)
192 {
193 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
194 struct puffs_node *pn;
195 int fd, rv;
196
197 fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
198 if (fd == -1)
199 return errno;
200 close(fd);
201 if ((rv = processvattr(PCNPATH(pcn), va, 1)) != 0) {
202 unlink(PCNPATH(pcn));
203 return rv;
204 }
205
206 pn = puffs_pn_new(pu, NULL);
207 if (!pn) {
208 unlink(PCNPATH(pcn));
209 return ENOMEM;
210 }
211 puffs_setvattr(&pn->pn_va, va);
212
213 *newnode = pn;
214 return 0;
215 }
216
217 /*ARGSUSED*/
218 int
219 puffs_null_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
220 const struct puffs_cn *pcn, const struct vattr *va)
221 {
222 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
223 struct puffs_node *pn;
224 mode_t mode;
225 int rv;
226
227 mode = puffs_addvtype2mode(va->va_mode, va->va_type);
228 if (mknod(PCNPATH(pcn), mode, va->va_rdev) == -1)
229 return errno;
230
231 if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
232 unlink(PCNPATH(pcn));
233 return rv;
234 }
235
236 pn = puffs_pn_new(pu, NULL);
237 if (!pn) {
238 unlink(PCNPATH(pcn));
239 return ENOMEM;
240 }
241 puffs_setvattr(&pn->pn_va, va);
242
243 *newnode = pn;
244 return 0;
245 }
246
247 /*ARGSUSED*/
248 int
249 puffs_null_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
250 const struct puffs_cred *pcred, pid_t pid)
251 {
252 struct puffs_node *pn = opc;
253 struct stat sb;
254
255 if (lstat(PNPATH(pn), &sb) == -1)
256 return errno;
257 puffs_stat2vattr(va, &sb);
258
259 return 0;
260 }
261
262 /*ARGSUSED*/
263 int
264 puffs_null_node_setattr(struct puffs_cc *pcc, void *opc,
265 const struct vattr *va, const struct puffs_cred *pcred, pid_t pid)
266 {
267 struct puffs_node *pn = opc;
268 int rv;
269
270 rv = processvattr(PNPATH(pn), va, pn->pn_va.va_type == VREG);
271 if (rv)
272 return rv;
273
274 puffs_setvattr(&pn->pn_va, va);
275
276 return 0;
277 }
278
279 /*ARGSUSED*/
280 int
281 puffs_null_node_fsync(struct puffs_cc *pcc, void *opc,
282 const struct puffs_cred *pcred, int how,
283 off_t offlo, off_t offhi, pid_t pid)
284 {
285 struct puffs_node *pn = opc;
286 int fd, rv;
287 int fflags;
288
289 rv = 0;
290 fd = writeableopen(PNPATH(pn));
291 if (fd == -1)
292 return errno;
293
294 if (how & PUFFS_FSYNC_DATAONLY)
295 fflags = FDATASYNC;
296 else
297 fflags = FFILESYNC;
298 if (how & PUFFS_FSYNC_CACHE)
299 fflags |= FDISKSYNC;
300
301 if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
302 rv = errno;
303
304 close(fd);
305
306 return rv;
307 }
308
309 /*ARGSUSED*/
310 int
311 puffs_null_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
312 const struct puffs_cn *pcn)
313 {
314 struct puffs_node *pn_targ = targ;
315
316 if (unlink(PNPATH(pn_targ)) == -1)
317 return errno;
318 puffs_pn_remove(pn_targ);
319
320 return 0;
321 }
322
323 /*ARGSUSED*/
324 int
325 puffs_null_node_link(struct puffs_cc *pcc, void *opc, void *targ,
326 const struct puffs_cn *pcn)
327 {
328 struct puffs_node *pn_targ = targ;
329
330 if (link(PNPATH(pn_targ), PCNPATH(pcn)) == -1)
331 return errno;
332
333 return 0;
334 }
335
336 /*ARGSUSED*/
337 int
338 puffs_null_node_rename(struct puffs_cc *pcc, void *opc, void *src,
339 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
340 const struct puffs_cn *pcn_targ)
341 {
342
343 if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
344 return errno;
345
346 return 0;
347 }
348
349 /*ARGSUSED*/
350 int
351 puffs_null_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
352 const struct puffs_cn *pcn, const struct vattr *va)
353 {
354 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
355 struct puffs_node *pn;
356 int rv;
357
358 if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
359 return errno;
360
361 if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
362 rmdir(PCNPATH(pcn));
363 return rv;
364 }
365
366 pn = puffs_pn_new(pu, NULL);
367 if (pn == NULL) {
368 rmdir(PCNPATH(pcn));
369 return ENOMEM;
370 }
371 puffs_setvattr(&pn->pn_va, va);
372
373 *newnode = pn;
374 return 0;
375 }
376
377 /*ARGSUSED*/
378 int
379 puffs_null_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
380 const struct puffs_cn *pcn)
381 {
382 struct puffs_node *pn_targ = targ;
383
384 if (rmdir(PNPATH(pn_targ)) == -1)
385 return errno;
386 puffs_pn_remove(pn_targ);
387
388 return 0;
389 }
390
391 /*ARGSUSED*/
392 int
393 puffs_null_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
394 const struct puffs_cn *pcn, const struct vattr *va,
395 const char *linkname)
396 {
397 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
398 struct puffs_node *pn;
399 int rv;
400
401 if (symlink(linkname, PCNPATH(pcn)) == -1)
402 return errno;
403
404 if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
405 unlink(PCNPATH(pcn));
406 return rv;
407 }
408
409 pn = puffs_pn_new(pu, NULL);
410 if (pn == NULL) {
411 rmdir(PCNPATH(pcn));
412 return ENOMEM;
413 }
414 puffs_setvattr(&pn->pn_va, va);
415
416 *newnode = pn;
417 return 0;
418 }
419
420 /*ARGSUSED*/
421 int
422 puffs_null_node_readlink(struct puffs_cc *pcc, void *opc,
423 const struct puffs_cred *pcred, char *linkname, size_t *linklen)
424 {
425 struct puffs_node *pn = opc;
426 ssize_t rv;
427
428 rv = readlink(PNPATH(pn), linkname, *linklen);
429 if (rv == -1)
430 return errno;
431
432 *linklen = rv;
433 return 0;
434 }
435
436 /*ARGSUSED*/
437 int
438 puffs_null_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *de,
439 off_t *off, size_t *reslen, const struct puffs_cred *pcred,
440 int *eofflag, off_t *cookies, size_t *ncookies)
441 {
442 struct puffs_node *pn = opc;
443 struct dirent entry, *result;
444 DIR *dp;
445 off_t i;
446 int rv;
447
448 dp = opendir(PNPATH(pn));
449 if (dp == NULL)
450 return errno;
451
452 rv = 0;
453 i = *off;
454
455 /*
456 * XXX: need to do trickery here, telldir/seekdir would be nice, but
457 * then we'd need to keep state, which I'm too lazy to keep
458 */
459 while (i--) {
460 rv = readdir_r(dp, &entry, &result);
461 if (rv || !result)
462 goto out;
463 }
464
465 for (;;) {
466 rv = readdir_r(dp, &entry, &result);
467 if (rv != 0)
468 goto out;
469
470 if (!result)
471 goto out;
472
473 if (_DIRENT_SIZE(result) > *reslen)
474 goto out;
475
476 *de = *result;
477 *reslen -= _DIRENT_SIZE(result);
478 de = _DIRENT_NEXT(de);
479
480 (*off)++;
481 }
482
483 out:
484 closedir(dp);
485 return 0;
486 }
487
488 /*ARGSUSED*/
489 int
490 puffs_null_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
491 off_t offset, size_t *buflen, const struct puffs_cred *pcred,
492 int ioflag)
493 {
494 struct puffs_node *pn = opc;
495 ssize_t n;
496 off_t off;
497 int fd, rv;
498
499 rv = 0;
500 fd = open(PNPATH(pn), O_RDONLY);
501 if (fd == -1)
502 return errno;
503 off = lseek(fd, offset, SEEK_SET);
504 if (off == -1) {
505 rv = errno;
506 goto out;
507 }
508
509 n = read(fd, buf, *buflen);
510 if (n == -1)
511 rv = errno;
512 else
513 *buflen -= n;
514
515 out:
516 close(fd);
517 return rv;
518 }
519
520 /*ARGSUSED*/
521 int
522 puffs_null_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
523 off_t offset, size_t *buflen, const struct puffs_cred *pcred,
524 int ioflag)
525 {
526 struct puffs_node *pn = opc;
527 ssize_t n;
528 off_t off;
529 int fd, rv;
530
531 rv = 0;
532 fd = writeableopen(PNPATH(pn));
533 if (fd == -1)
534 return errno;
535
536 off = lseek(fd, offset, SEEK_SET);
537 if (off == -1) {
538 rv = errno;
539 goto out;
540 }
541
542 n = write(fd, buf, *buflen);
543 if (n == -1)
544 rv = errno;
545 else
546 *buflen -= n;
547
548 out:
549 close(fd);
550 return rv;
551 }
552
553 /*ARGSUSED*/
554 int
555 puffs_null_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
556 {
557
558 return puffs_genfs_node_reclaim(pcc, opc, pid);
559 }
560