null.c revision 1.15 1 /* $NetBSD: null.c,v 1.15 2007/06/24 17:41:09 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.15 2007/06/24 17:41:09 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
319 return 0;
320 }
321
322 /*ARGSUSED*/
323 int
324 puffs_null_node_link(struct puffs_cc *pcc, void *opc, void *targ,
325 const struct puffs_cn *pcn)
326 {
327 struct puffs_node *pn_targ = targ;
328
329 if (link(PNPATH(pn_targ), PCNPATH(pcn)) == -1)
330 return errno;
331
332 return 0;
333 }
334
335 /*ARGSUSED*/
336 int
337 puffs_null_node_rename(struct puffs_cc *pcc, void *opc, void *src,
338 const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
339 const struct puffs_cn *pcn_targ)
340 {
341
342 if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
343 return errno;
344
345 return 0;
346 }
347
348 /*ARGSUSED*/
349 int
350 puffs_null_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
351 const struct puffs_cn *pcn, const struct vattr *va)
352 {
353 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
354 struct puffs_node *pn;
355 int rv;
356
357 if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
358 return errno;
359
360 if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
361 rmdir(PCNPATH(pcn));
362 return rv;
363 }
364
365 pn = puffs_pn_new(pu, NULL);
366 if (pn == NULL) {
367 rmdir(PCNPATH(pcn));
368 return ENOMEM;
369 }
370 puffs_setvattr(&pn->pn_va, va);
371
372 *newnode = pn;
373 return 0;
374 }
375
376 /*ARGSUSED*/
377 int
378 puffs_null_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
379 const struct puffs_cn *pcn)
380 {
381 struct puffs_node *pn_targ = targ;
382
383 if (rmdir(PNPATH(pn_targ)) == -1)
384 return errno;
385
386 return 0;
387 }
388
389 /*ARGSUSED*/
390 int
391 puffs_null_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
392 const struct puffs_cn *pcn, const struct vattr *va,
393 const char *linkname)
394 {
395 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
396 struct puffs_node *pn;
397 int rv;
398
399 if (symlink(linkname, PCNPATH(pcn)) == -1)
400 return errno;
401
402 if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
403 unlink(PCNPATH(pcn));
404 return rv;
405 }
406
407 pn = puffs_pn_new(pu, NULL);
408 if (pn == NULL) {
409 rmdir(PCNPATH(pcn));
410 return ENOMEM;
411 }
412 puffs_setvattr(&pn->pn_va, va);
413
414 *newnode = pn;
415 return 0;
416 }
417
418 /*ARGSUSED*/
419 int
420 puffs_null_node_readlink(struct puffs_cc *pcc, void *opc,
421 const struct puffs_cred *pcred, char *linkname, size_t *linklen)
422 {
423 struct puffs_node *pn = opc;
424 ssize_t rv;
425
426 rv = readlink(PNPATH(pn), linkname, *linklen);
427 if (rv == -1)
428 return errno;
429
430 *linklen = rv;
431 return 0;
432 }
433
434 /*ARGSUSED*/
435 int
436 puffs_null_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *de,
437 off_t *off, size_t *reslen, const struct puffs_cred *pcred,
438 int *eofflag, off_t *cookies, size_t *ncookies)
439 {
440 struct puffs_node *pn = opc;
441 struct dirent entry, *result;
442 DIR *dp;
443 off_t i;
444 int rv;
445
446 dp = opendir(PNPATH(pn));
447 if (dp == NULL)
448 return errno;
449
450 rv = 0;
451 i = *off;
452
453 /*
454 * XXX: need to do trickery here, telldir/seekdir would be nice, but
455 * then we'd need to keep state, which I'm too lazy to keep
456 */
457 while (i--) {
458 rv = readdir_r(dp, &entry, &result);
459 if (rv || !result)
460 goto out;
461 }
462
463 for (;;) {
464 rv = readdir_r(dp, &entry, &result);
465 if (rv != 0)
466 goto out;
467
468 if (!result)
469 goto out;
470
471 if (_DIRENT_SIZE(result) > *reslen)
472 goto out;
473
474 *de = *result;
475 *reslen -= _DIRENT_SIZE(result);
476 de = _DIRENT_NEXT(de);
477
478 (*off)++;
479 }
480
481 out:
482 closedir(dp);
483 return 0;
484 }
485
486 /*ARGSUSED*/
487 int
488 puffs_null_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
489 off_t offset, size_t *buflen, const struct puffs_cred *pcred,
490 int ioflag)
491 {
492 struct puffs_node *pn = opc;
493 ssize_t n;
494 off_t off;
495 int fd, rv;
496
497 rv = 0;
498 fd = open(PNPATH(pn), O_RDONLY);
499 if (fd == -1)
500 return errno;
501 off = lseek(fd, offset, SEEK_SET);
502 if (off == -1) {
503 rv = errno;
504 goto out;
505 }
506
507 n = read(fd, buf, *buflen);
508 if (n == -1)
509 rv = errno;
510 else
511 *buflen -= n;
512
513 out:
514 close(fd);
515 return rv;
516 }
517
518 /*ARGSUSED*/
519 int
520 puffs_null_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
521 off_t offset, size_t *buflen, const struct puffs_cred *pcred,
522 int ioflag)
523 {
524 struct puffs_node *pn = opc;
525 ssize_t n;
526 off_t off;
527 int fd, rv;
528
529 rv = 0;
530 fd = writeableopen(PNPATH(pn));
531 if (fd == -1)
532 return errno;
533
534 off = lseek(fd, offset, SEEK_SET);
535 if (off == -1) {
536 rv = errno;
537 goto out;
538 }
539
540 n = write(fd, buf, *buflen);
541 if (n == -1)
542 rv = errno;
543 else
544 *buflen -= n;
545
546 out:
547 close(fd);
548 return rv;
549 }
550
551 /*ARGSUSED*/
552 int
553 puffs_null_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
554 {
555
556 return puffs_genfs_node_reclaim(pcc, opc, pid);
557 }
558