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