a36a24ab3369bbfc929790131a1243adfe17384b
[dragonfly.git] / lib / libpuffs / null.c
1 /*      $NetBSD: null.c,v 1.30 2011/06/27 12:06:19 manu 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 /*
29  * A "nullfs" using puffs, i.e. maps one location in the hierarchy
30  * to another using standard system calls.
31  */
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36
37 #include <assert.h>
38 #include <dirent.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <unistd.h>
45
46 #include "puffs.h"
47
48 PUFFSOP_PROTOS(puffs_null)
49
50 /*
51  * set attributes to what is specified.  XXX: no rollback in case of failure
52  */
53 static int
54 processvattr(const char *path, const struct vattr *va, int regular)
55 {
56         struct timeval tv[2];
57
58         /* XXX: -1 == PUFFS_VNOVAL, but shouldn't trust that */
59         if (va->va_uid != (unsigned)-1 || va->va_gid != (unsigned)-1)
60                 if (lchown(path, va->va_uid, va->va_gid) == -1)
61                         return errno;
62
63         if (va->va_mode != (unsigned)PUFFS_VNOVAL)
64                 if (lchmod(path, va->va_mode) == -1)
65                         return errno;
66
67         /* sloppy */
68         if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
69             || va->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) {
70                 TIMESPEC_TO_TIMEVAL(&tv[0], &va->va_atime);
71                 TIMESPEC_TO_TIMEVAL(&tv[1], &va->va_mtime);
72
73                 if (lutimes(path, tv) == -1)
74                         return errno;
75         }
76
77         if (regular && va->va_size != (u_quad_t)PUFFS_VNOVAL)
78                 if (truncate(path, (off_t)va->va_size) == -1)
79                         return errno;
80
81         return 0;
82 }
83
84 /*
85  * Kludge to open files which aren't writable *any longer*.  This kinda
86  * works because the vfs layer does validation checks based on the file's
87  * permissions to allow writable opening before opening them.  However,
88  * the problem arises if we want to create a file, write to it (cache),
89  * adjust permissions and then flush the file.
90  */
91 static int
92 writeableopen(const char *path)
93 {
94         struct stat sb;
95         mode_t origmode;
96         int sverr = 0;
97         int fd;
98
99         fd = open(path, O_WRONLY);
100         if (fd == -1) {
101                 if (errno == EACCES) {
102                         if (stat(path, &sb) == -1)
103                                 return -1;
104                         origmode = sb.st_mode & ALLPERMS;
105
106                         if (chmod(path, 0200) == -1)
107                                 return -1;
108
109                         fd = open(path, O_WRONLY);
110                         if (fd == -1)
111                                 sverr = errno;
112
113                         chmod(path, origmode);
114                         if (sverr)
115                                 errno = sverr;
116                 } else
117                         return -1;
118         }
119
120         return fd;
121 }
122
123 /*ARGSUSED*/
124 static void *
125 inodecmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
126 {
127         ino_t *cmpino = arg;
128
129         if (pn->pn_va.va_fileid == *cmpino)
130                 return pn;
131         return NULL;
132 }
133
134 static int
135 makenode(struct puffs_usermount *pu, struct puffs_newinfo *pni,
136         const struct puffs_cn *pcn, const struct vattr *va, int regular)
137 {
138         struct puffs_node *pn;
139         struct stat sb;
140         int rv;
141
142         if ((rv = processvattr(PCNPATH(pcn), va, regular)) != 0)
143                 return rv;
144
145         pn = puffs_pn_new(pu, NULL);
146         if (!pn)
147                 return ENOMEM;
148         puffs_setvattr(&pn->pn_va, va);
149
150         if (lstat(PCNPATH(pcn), &sb) == -1)
151                 return errno;
152         puffs_stat2vattr(&pn->pn_va, &sb);
153
154         puffs_newinfo_setcookie(pni, pn);
155         return 0;
156 }
157
158 /* This should be called first and overridden from the file system */
159 void
160 puffs_null_setops(struct puffs_ops *pops)
161 {
162
163         PUFFSOP_SET(pops, puffs_null, fs, statvfs);
164         PUFFSOP_SETFSNOP(pops, unmount);
165         PUFFSOP_SETFSNOP(pops, sync);
166         PUFFSOP_SET(pops, puffs_null, fs, fhtonode);
167         PUFFSOP_SET(pops, puffs_null, fs, nodetofh);
168
169         PUFFSOP_SET(pops, puffs_null, node, lookup);
170         PUFFSOP_SET(pops, puffs_null, node, create);
171         PUFFSOP_SET(pops, puffs_null, node, mknod);
172         PUFFSOP_SET(pops, puffs_null, node, getattr);
173         PUFFSOP_SET(pops, puffs_null, node, setattr);
174         PUFFSOP_SET(pops, puffs_null, node, fsync);
175         PUFFSOP_SET(pops, puffs_null, node, remove);
176         PUFFSOP_SET(pops, puffs_null, node, link);
177         PUFFSOP_SET(pops, puffs_null, node, rename);
178         PUFFSOP_SET(pops, puffs_null, node, mkdir);
179         PUFFSOP_SET(pops, puffs_null, node, rmdir);
180         PUFFSOP_SET(pops, puffs_null, node, symlink);
181         PUFFSOP_SET(pops, puffs_null, node, readlink);
182         PUFFSOP_SET(pops, puffs_null, node, readdir);
183         PUFFSOP_SET(pops, puffs_null, node, read);
184         PUFFSOP_SET(pops, puffs_null, node, write);
185         PUFFSOP_SET(pops, puffs_genfs, node, reclaim);
186 }
187
188 /*ARGSUSED*/
189 int
190 puffs_null_fs_statvfs(struct puffs_usermount *pu, struct statvfs *svfsb)
191 {
192
193         if (statvfs(PNPATH(puffs_getroot(pu)), svfsb) == -1)
194                 return errno;
195
196         return 0;
197 }
198
199 /*ARGSUSED*/
200 static void *
201 fhcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
202 {
203         struct fid *kf1, *kf2;
204
205         if ((kf1 = pn->pn_data) == NULL)
206                 return NULL;
207         kf2 = arg;
208
209         if (kf1->fid_len != kf2->fid_len)
210                 return NULL;
211
212         /*LINTED*/
213         if (memcmp(kf1, kf2, kf1->fid_len) == 0)
214                 return pn;
215         return NULL;
216 }
217
218 /*
219  * This routine only supports file handles which have been issued while
220  * the server was alive.  Not really stable ones, that is.
221  */
222 /*ARGSUSED*/
223 int
224 puffs_null_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
225         struct puffs_newinfo *pni)
226 {
227         struct puffs_node *pn_res;
228
229         if (fidsize != sizeof(struct fid))
230                 return EINVAL;
231
232         pn_res = puffs_pn_nodewalk(pu, fhcmp, fid);
233         if (pn_res == NULL)
234                 return ENOENT;
235
236         puffs_newinfo_setcookie(pni, pn_res);
237         puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
238         puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
239         return 0;
240 }
241
242 /*ARGSUSED*/
243 int
244 puffs_null_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t opc,
245         void *fid, size_t *fidsize)
246 {
247         struct puffs_node *pn = opc;
248         fhandle_t fh;
249         int rv;
250
251         if (*fidsize != sizeof(struct fid))
252                 return EINVAL;
253
254         rv = 0;
255         if (getfh(PNPATH(pn), &fh) == -1)
256                 rv = errno;
257         if (rv == 0) {
258                 *(struct fid *)fid = fh.fh_fid;
259                 pn->pn_data = malloc(*fidsize);
260                 if (pn->pn_data == NULL)
261                         abort(); /* lazy */
262                 memcpy(pn->pn_data, fid, *fidsize);
263         }
264
265         return rv;
266 }
267
268 int
269 puffs_null_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
270         struct puffs_newinfo *pni, const struct puffs_cn *pcn)
271 {
272         struct puffs_node *pn = opc, *pn_res;
273         struct stat sb;
274         int rv;
275
276         assert(pn->pn_va.va_type == VDIR);
277
278         /*
279          * Note to whoever is copypasting this: you must first check
280          * if the node is there and only then do nodewalk.  Alternatively
281          * you could make sure that you don't return unlinked/rmdir'd
282          * nodes in some other fashion
283          */
284         rv = lstat(PCNPATH(pcn), &sb);
285         if (rv)
286                 return errno;
287
288         /* XXX2: nodewalk is a bit too slow here */
289         pn_res = puffs_pn_nodewalk(pu, inodecmp, &sb.st_ino);
290
291         if (pn_res == NULL) {
292                 pn_res = puffs_pn_new(pu, NULL);
293                 if (pn_res == NULL)
294                         return ENOMEM;
295                 puffs_stat2vattr(&pn_res->pn_va, &sb);
296         }
297
298         puffs_newinfo_setcookie(pni, pn_res);
299         puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
300         puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
301
302         return 0;
303 }
304
305 int
306 puffs_null_node_lookupdotdot(struct puffs_usermount *pu, puffs_cookie_t opc,
307         struct puffs_newinfo *pni, const struct puffs_cn *pcn)
308 {
309         return puffs_null_node_lookup(pu, opc, pni, pcn);
310 }
311
312 /*ARGSUSED*/
313 int
314 puffs_null_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
315         struct puffs_newinfo *pni, const struct puffs_cn *pcn,
316         const struct vattr *va)
317 {
318         int fd, rv;
319
320         fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
321         if (fd == -1)
322                 return errno;
323         close(fd);
324
325         rv = makenode(pu, pni, pcn, va, 1);
326         if (rv)
327                 unlink(PCNPATH(pcn));
328         return rv;
329 }
330
331 /*ARGSUSED*/
332 int
333 puffs_null_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
334         struct puffs_newinfo *pni, const struct puffs_cn *pcn,
335         const struct vattr *va)
336 {
337         mode_t mode;
338         int rv;
339
340         mode = puffs_addvtype2mode(va->va_mode, va->va_type);
341         switch (va->va_type) {
342         case VFIFO:
343                 if (mkfifo(PCNPATH(pcn), mode) == -1)
344                         return errno;
345                 break;
346         case VCHR:
347         case VBLK:
348                 return ENOTSUP;
349         default:
350                 return EINVAL;
351         }
352
353         rv = makenode(pu, pni, pcn, va, 0);
354         if (rv)
355                 unlink(PCNPATH(pcn));
356         return rv;
357 }
358
359 /*ARGSUSED*/
360 int
361 puffs_null_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
362         struct vattr *va, const struct puffs_cred *pcred)
363 {
364         struct puffs_node *pn = opc;
365         struct stat sb;
366
367         if (lstat(PNPATH(pn), &sb) == -1)
368                 return errno;
369         puffs_stat2vattr(va, &sb);
370
371         return 0;
372 }
373
374 /*ARGSUSED*/
375 int
376 puffs_null_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
377         const struct vattr *va, const struct puffs_cred *pcred)
378 {
379         struct puffs_node *pn = opc;
380         int rv;
381
382         rv = processvattr(PNPATH(pn), va, pn->pn_va.va_type == VREG);
383         if (rv)
384                 return rv;
385
386         puffs_setvattr(&pn->pn_va, va);
387
388         return 0;
389 }
390
391 /*ARGSUSED*/
392 int
393 puffs_null_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
394         int how)
395 {
396         struct puffs_node *pn = opc;
397         int fd, rv;
398         struct stat sb;
399
400         rv = 0;
401         if (stat(PNPATH(pn), &sb) == -1)
402                 return errno;
403         if (S_ISDIR(sb.st_mode)) {
404                 DIR *dirp;
405                 if ((dirp = opendir(PNPATH(pn))) == NULL)
406                         return errno;
407                 fd = dirfd(dirp);
408                 if (fd == -1)
409                         return errno;
410         } else {
411                 fd = writeableopen(PNPATH(pn));
412                 if (fd == -1)
413                         return errno;
414         }
415
416         if (fsync(fd) == -1)
417                 rv = errno;
418
419         close(fd);
420
421         return rv;
422 }
423
424 /*ARGSUSED*/
425 int
426 puffs_null_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
427         puffs_cookie_t targ, const struct puffs_cn *pcn)
428 {
429         struct puffs_node *pn_targ = targ;
430
431         if (unlink(PNPATH(pn_targ)) == -1)
432                 return errno;
433         puffs_pn_remove(pn_targ);
434
435         return 0;
436 }
437
438 /*ARGSUSED*/
439 int
440 puffs_null_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
441         puffs_cookie_t targ, const struct puffs_cn *pcn)
442 {
443         struct puffs_node *pn_targ = targ;
444
445         if (link(PNPATH(pn_targ), PCNPATH(pcn)) == -1)
446                 return errno;
447
448         return 0;
449 }
450
451 /*ARGSUSED*/
452 int
453 puffs_null_node_rename(struct puffs_usermount *pu, puffs_cookie_t opc,
454         puffs_cookie_t src, const struct puffs_cn *pcn_src,
455         puffs_cookie_t targ_dir, puffs_cookie_t targ,
456         const struct puffs_cn *pcn_targ)
457 {
458
459         if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
460                 return errno;
461
462         return 0;
463 }
464
465 /*ARGSUSED*/
466 int
467 puffs_null_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
468         struct puffs_newinfo *pni, const struct puffs_cn *pcn,
469         const struct vattr *va)
470 {
471         int rv;
472
473         if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
474                 return errno;
475
476         rv = makenode(pu, pni, pcn, va, 0);
477         if (rv)
478                 rmdir(PCNPATH(pcn));
479         return rv;
480 }
481
482 /*ARGSUSED*/
483 int
484 puffs_null_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
485         puffs_cookie_t targ, const struct puffs_cn *pcn)
486 {
487         struct puffs_node *pn_targ = targ;
488
489         if (rmdir(PNPATH(pn_targ)) == -1)
490                 return errno;
491         puffs_pn_remove(pn_targ);
492
493         return 0;
494 }
495
496 /*ARGSUSED*/
497 int
498 puffs_null_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
499         struct puffs_newinfo *pni, const struct puffs_cn *pcn,
500         const struct vattr *va, const char *linkname)
501 {
502         int rv;
503
504         if (symlink(linkname, PCNPATH(pcn)) == -1)
505                 return errno;
506
507         rv = makenode(pu, pni, pcn, va, 0);
508         if (rv)
509                 unlink(PCNPATH(pcn));
510         return rv;
511 }
512
513 /*ARGSUSED*/
514 int
515 puffs_null_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
516         const struct puffs_cred *pcred, char *linkname, size_t *linklen)
517 {
518         struct puffs_node *pn = opc;
519         ssize_t rv;
520
521         rv = readlink(PNPATH(pn), linkname, *linklen);
522         if (rv == -1)
523                 return errno;
524
525         *linklen = rv;
526         return 0;
527 }
528
529 /*ARGSUSED*/
530 int
531 puffs_null_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
532         struct dirent *de, off_t *off, size_t *reslen,
533         const struct puffs_cred *pcred, int *eofflag, off_t *cookies,
534         size_t *ncookies)
535 {
536         struct puffs_node *pn = opc;
537         struct dirent entry, *result;
538         DIR *dp;
539         off_t i;
540         int rv;
541
542         *ncookies = 0;
543         dp = opendir(PNPATH(pn));
544         if (dp == NULL)
545                 return errno;
546
547         rv = 0;
548         i = *off;
549
550         /*
551          * XXX: need to do trickery here, telldir/seekdir would be nice, but
552          * then we'd need to keep state, which I'm too lazy to keep
553          */
554         while (i--) {
555                 rv = readdir_r(dp, &entry, &result);
556                 if (rv != 0)
557                         goto out;
558
559                 if (!result) {
560                         *eofflag = 1;
561                         goto out;
562                 }
563         }
564
565         for (;;) {
566                 rv = readdir_r(dp, &entry, &result);
567                 if (rv != 0)
568                         goto out;
569
570                 if (!result) {
571                         *eofflag = 1;
572                         goto out;
573                 }
574
575                 if (_DIRENT_DIRSIZ(result) > *reslen)
576                         goto out;
577
578                 *de = *result;
579                 *reslen -= _DIRENT_DIRSIZ(result);
580                 de = _DIRENT_NEXT(de);
581
582                 (*off)++;
583                 PUFFS_STORE_DCOOKIE(cookies, ncookies, *off);
584         }
585
586  out:
587         closedir(dp);
588         return 0;
589 }
590
591 /*ARGSUSED*/
592 int
593 puffs_null_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
594         uint8_t *buf, off_t offset, size_t *buflen,
595         const struct puffs_cred *pcred, int ioflag)
596 {
597         struct puffs_node *pn = opc;
598         ssize_t n;
599         off_t off;
600         int fd, rv;
601
602         rv = 0;
603         fd = open(PNPATH(pn), O_RDONLY);
604         if (fd == -1)
605                 return errno;
606         off = lseek(fd, offset, SEEK_SET);
607         if (off == -1) {
608                 rv = errno;
609                 goto out;
610         }
611
612         n = read(fd, buf, *buflen);
613         if (n == -1)
614                 rv = errno;
615         else
616                 *buflen -= n;
617
618  out:
619         close(fd);
620         return rv;
621 }
622
623 /*ARGSUSED*/
624 int
625 puffs_null_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
626         uint8_t *buf, off_t offset, size_t *buflen,
627         const struct puffs_cred *pcred, int ioflag)
628 {
629         struct puffs_node *pn = opc;
630         ssize_t n;
631         off_t off;
632         int fd, rv;
633
634         rv = 0;
635         fd = writeableopen(PNPATH(pn));
636         if (fd == -1)
637                 return errno;
638
639         off = lseek(fd, offset, SEEK_SET);
640         if (off == -1) {
641                 rv = errno;
642                 goto out;
643         }
644
645         n = write(fd, buf, *buflen);
646         if (n == -1)
647                 rv = errno;
648         else
649                 *buflen -= n;
650
651  out:
652         close(fd);
653         return rv;
654 }