null.c revision 1.6 1 /* $NetBSD: null.c,v 1.6 2007/02/15 12:51:45 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 * 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 #if !defined(lint)
33 __RCSID("$NetBSD: null.c,v 1.6 2007/02/15 12:51:45 pooka Exp $");
34 #endif /* !lint */
35
36 /*
37 * A "nullfs" using puffs, i.e. maps one location in the hierarchy
38 * to another using standard system calls.
39 */
40
41 #include <sys/types.h>
42 #include <sys/time.h>
43
44 #include <assert.h>
45 #include <dirent.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <puffs.h>
49 #include <stdio.h>
50 #include <unistd.h>
51
52 PUFFSOP_PROTOS(puffs_null)
53
54 /*ARGSUSED*/
55 static void *
56 pathcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
57 {
58 struct puffs_pathobj *po = arg;
59
60 if (strcmp(po->po_path, PNPATH(pn)) == 0)
61 return pn;
62 return NULL;
63 }
64
65 /*
66 * set attributes to what is specified. XXX: no rollback in case of failure
67 */
68 static int
69 processvattr(const char *path, const struct vattr *va, int regular)
70 {
71 struct timeval tv[2];
72
73 /* XXX: -1 == PUFFS_VNOVAL, but shouldn't trust that */
74 if (va->va_uid != (unsigned)-1 || va->va_gid != (unsigned)-1)
75 if (lchown(path, va->va_uid, va->va_gid) == -1)
76 return errno;
77
78 if (va->va_mode != (unsigned)PUFFS_VNOVAL)
79 if (lchmod(path, va->va_mode) == -1)
80 return errno;
81
82 /* sloppy */
83 if (va->va_atime.tv_sec != (unsigned)PUFFS_VNOVAL
84 || va->va_mtime.tv_sec != (unsigned)PUFFS_VNOVAL) {
85 TIMESPEC_TO_TIMEVAL(&tv[0], &va->va_atime);
86 TIMESPEC_TO_TIMEVAL(&tv[1], &va->va_mtime);
87
88 if (lutimes(path, tv) == -1)
89 return errno;
90 }
91
92 if (regular && va->va_size != (u_quad_t)PUFFS_VNOVAL)
93 if (truncate(path, (off_t)va->va_size) == -1)
94 return errno;
95
96 return 0;
97 }
98
99 /*
100 * Kludge to open files which aren't writable *any longer*. This kinda
101 * works because the vfs layer does validation checks based on the file's
102 * permissions to allow writable opening before opening them. However,
103 * the problem arises if we want to create a file, write to it (cache),
104 * adjust permissions and then flush the file.
105 */
106 static int
107 writeableopen(const char *path)
108 {
109 struct stat sb;
110 mode_t origmode;
111 int sverr = 0;
112 int fd;
113
114 fd = open(path, O_WRONLY);
115 if (fd == -1) {
116 if (errno == EACCES) {
117 if (stat(path, &sb) == -1)
118 return errno;
119 origmode = sb.st_mode & ALLPERMS;
120
121 if (chmod(path, 0200) == -1)
122 return errno;
123
124 fd = open(path, O_WRONLY);
125 if (fd == -1)
126 sverr = errno;
127
128 chmod(path, origmode);
129 if (sverr)
130 errno = sverr;
131 } else
132 return errno;
133 }
134
135 return fd;
136 }
137
138 /*ARGSUSED*/
139 int
140 puffs_null_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
141 {
142 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
143
144 if (statvfs(PNPATH(pu->pu_pn_root), svfsb) == -1)
145 return errno;
146
147 return 0;
148 }
149
150 int
151 puffs_null_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
152 enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
153 const struct puffs_cn *pcn)
154 {
155 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
156 struct puffs_node *pn = opc, *pn_res;
157 struct stat sb;
158 int rv;
159
160 assert(pn->pn_va.va_type == VDIR);
161
162 /*
163 * Note to whoever is copypasting this: you must first check
164 * if the node is there and only then do nodewalk. Alternatively
165 * you could make sure that you don't return unlinked/rmdir'd
166 * nodes in some other fashion
167 */
168 rv = lstat(PCNPATH(pcn), &sb);
169 if (rv)
170 return rv;
171
172 /* XXX: UNCONST is wrong, fix the interface */
173 /* XXX2: nodewalk is a bit too slow here */
174 pn_res = puffs_pn_nodewalk(pu, pathcmp, __UNCONST(&pcn->pcn_po_full));
175
176 if (pn_res == NULL) {
177 pn_res = puffs_pn_new(pu, NULL);
178 if (pn_res == NULL)
179 return ENOMEM;
180 puffs_stat2vattr(&pn_res->pn_va, &sb);
181 }
182
183 *newnode = pn_res;
184 *newtype = pn_res->pn_va.va_type;
185 *newsize = pn_res->pn_va.va_size;
186 *newrdev = pn_res->pn_va.va_rdev;
187
188 return 0;
189 }
190
191 /*ARGSUSED*/
192 int
193 puffs_null_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
194 const struct puffs_cn *pcn, const struct vattr *va)
195 {
196 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
197 struct puffs_node *pn;
198 int fd, rv;
199
200 fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
201 if (fd == -1)
202 return errno;
203 close(fd);
204 if ((rv = processvattr(PCNPATH(pcn), va, 1)) != 0) {
205 unlink(PCNPATH(pcn));
206 return rv;
207 }
208
209 pn = puffs_pn_new(pu, NULL);
210 if (!pn) {
211 unlink(PCNPATH(pcn));
212 return ENOMEM;
213 }
214 puffs_setvattr(&pn->pn_va, va);
215
216 *newnode = pn;
217 return 0;
218 }
219
220 /*ARGSUSED*/
221 int
222 puffs_null_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
223 const struct puffs_cn *pcn, const struct vattr *va)
224 {
225 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
226 struct puffs_node *pn;
227 int rv;
228
229 if (mknod(PCNPATH(pcn), va->va_mode, va->va_rdev) == -1)
230 return errno;
231
232 if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
233 unlink(PCNPATH(pcn));
234 return rv;
235 }
236
237 pn = puffs_pn_new(pu, NULL);
238 if (!pn) {
239 unlink(PCNPATH(pcn));
240 return ENOMEM;
241 }
242 puffs_setvattr(&pn->pn_va, va);
243
244 *newnode = pn;
245 return 0;
246 }
247
248 /*ARGSUSED*/
249 int
250 puffs_null_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
251 const struct puffs_cred *pcred, pid_t pid)
252 {
253 struct puffs_node *pn = opc;
254 struct stat sb;
255
256 if (lstat(PNPATH(pn), &sb) == -1)
257 return errno;
258 puffs_stat2vattr(va, &sb);
259
260 return 0;
261 }
262
263 /*ARGSUSED*/
264 int
265 puffs_null_node_setattr(struct puffs_cc *pcc, void *opc,
266 const struct vattr *va, const struct puffs_cred *pcred, pid_t pid)
267 {
268 struct puffs_node *pn = opc;
269 int rv;
270
271 rv = processvattr(PNPATH(pn), va, pn->pn_va.va_type == VREG);
272 if (rv)
273 return rv;
274
275 puffs_setvattr(&pn->pn_va, va);
276
277 return 0;
278 }
279
280 /*ARGSUSED*/
281 int
282 puffs_null_node_fsync(struct puffs_cc *pcc, void *opc,
283 const struct puffs_cred *pcred, int how,
284 off_t offlo, off_t offhi, pid_t pid)
285 {
286 struct puffs_node *pn = opc;
287 int fd, rv;
288 int fflags;
289
290 rv = 0;
291 fd = writeableopen(PNPATH(pn));
292 if (fd == -1)
293 return errno;
294
295 if (how & PUFFS_FSYNC_DATAONLY)
296 fflags = FDATASYNC;
297 else
298 fflags = FFILESYNC;
299 if (how & PUFFS_FSYNC_CACHE)
300 fflags |= FDISKSYNC;
301
302 if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
303 rv = errno;
304
305 close(fd);
306
307 return rv;
308 }
309
310 /*ARGSUSED*/
311 int
312 puffs_null_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
313 const struct puffs_cn *pcn)
314 {
315 struct puffs_node *pn_targ = targ;
316
317 if (unlink(PNPATH(pn_targ)) == -1)
318 return errno;
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
387 return 0;
388 }
389
390 /*ARGSUSED*/
391 int
392 puffs_null_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
393 const struct puffs_cn *pcn, const struct vattr *va,
394 const char *linkname)
395 {
396 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
397 struct puffs_node *pn;
398 int rv;
399
400 if (symlink(linkname, PCNPATH(pcn)) == -1)
401 return errno;
402
403 if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
404 unlink(PCNPATH(pcn));
405 return rv;
406 }
407
408 pn = puffs_pn_new(pu, NULL);
409 if (pn == NULL) {
410 rmdir(PCNPATH(pcn));
411 return ENOMEM;
412 }
413 puffs_setvattr(&pn->pn_va, va);
414
415 *newnode = pn;
416 return 0;
417 }
418
419 /*ARGSUSED*/
420 int
421 puffs_null_node_readlink(struct puffs_cc *pcc, void *opc,
422 const struct puffs_cred *pcred, char *linkname, size_t *linklen)
423 {
424 struct puffs_node *pn = opc;
425 ssize_t rv;
426
427 rv = readlink(PNPATH(pn), linkname, *linklen);
428 if (rv == -1)
429 return errno;
430
431 *linklen -= rv;
432 return 0;
433 }
434
435 /*ARGSUSED*/
436 int
437 puffs_null_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *de,
438 const struct puffs_cred *pcred, off_t *off, size_t *reslen)
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