acxcontrol(8) is gone.
[dragonfly.git] / sys / vfs / hammer / hammer_vfsops.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.17 2008/02/05 20:52:01 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/vnode.h>
41 #include <sys/mount.h>
42 #include <sys/malloc.h>
43 #include <sys/nlookup.h>
44 #include <sys/fcntl.h>
45 #include <sys/sysctl.h>
46 #include <sys/buf.h>
47 #include <sys/buf2.h>
48 #include "hammer.h"
49
50 int hammer_debug_general;
51 int hammer_debug_btree;
52 int hammer_debug_tid;
53 int hammer_debug_recover;       /* -1 will disable, +1 will force */
54 int hammer_debug_recover_faults;
55 int hammer_count_inodes;
56 int hammer_count_records;
57 int hammer_count_record_datas;
58 int hammer_count_volumes;
59 int hammer_count_supercls;
60 int hammer_count_clusters;
61 int hammer_count_buffers;
62 int hammer_count_nodes;
63 int hammer_count_spikes;
64
65 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
66 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
67            &hammer_debug_general, 0, "");
68 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
69            &hammer_debug_btree, 0, "");
70 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
71            &hammer_debug_tid, 0, "");
72 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
73            &hammer_debug_recover, 0, "");
74 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
75            &hammer_debug_recover_faults, 0, "");
76 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
77            &hammer_count_inodes, 0, "");
78 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
79            &hammer_count_records, 0, "");
80 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
81            &hammer_count_record_datas, 0, "");
82 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
83            &hammer_count_volumes, 0, "");
84 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_supercls, CTLFLAG_RD,
85            &hammer_count_supercls, 0, "");
86 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_clusters, CTLFLAG_RD,
87            &hammer_count_clusters, 0, "");
88 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
89            &hammer_count_buffers, 0, "");
90 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
91            &hammer_count_nodes, 0, "");
92 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_spikes, CTLFLAG_RD,
93            &hammer_count_spikes, 0, "");
94
95 /*
96  * VFS ABI
97  */
98 static void     hammer_free_hmp(struct mount *mp);
99
100 static int      hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
101                                 struct ucred *cred);
102 static int      hammer_vfs_unmount(struct mount *mp, int mntflags);
103 static int      hammer_vfs_root(struct mount *mp, struct vnode **vpp);
104 static int      hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
105                                 struct ucred *cred);
106 static int      hammer_vfs_sync(struct mount *mp, int waitfor);
107 static int      hammer_vfs_vget(struct mount *mp, ino_t ino,
108                                 struct vnode **vpp);
109 static int      hammer_vfs_init(struct vfsconf *conf);
110 static int      hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
111                                 struct vnode **vpp);
112 static int      hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
113 static int      hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
114                                 int *exflagsp, struct ucred **credanonp);
115
116
117 static struct vfsops hammer_vfsops = {
118         .vfs_mount      = hammer_vfs_mount,
119         .vfs_unmount    = hammer_vfs_unmount,
120         .vfs_root       = hammer_vfs_root,
121         .vfs_statfs     = hammer_vfs_statfs,
122         .vfs_sync       = hammer_vfs_sync,
123         .vfs_vget       = hammer_vfs_vget,
124         .vfs_init       = hammer_vfs_init,
125         .vfs_vptofh     = hammer_vfs_vptofh,
126         .vfs_fhtovp     = hammer_vfs_fhtovp,
127         .vfs_checkexp   = hammer_vfs_checkexp
128 };
129
130 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
131
132 VFS_SET(hammer_vfsops, hammer, 0);
133 MODULE_VERSION(hammer, 1);
134
135 static int
136 hammer_vfs_init(struct vfsconf *conf)
137 {
138         hammer_init_alist_config();
139         return(0);
140 }
141
142 static int
143 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
144                  struct ucred *cred)
145 {
146         struct hammer_mount_info info;
147         hammer_mount_t hmp;
148         hammer_volume_t rootvol;
149         struct vnode *rootvp;
150         const char *upath;      /* volume name in userspace */
151         char *path;             /* volume name in system space */
152         int error;
153         int i;
154
155         if ((error = copyin(data, &info, sizeof(info))) != 0)
156                 return (error);
157         if (info.nvolumes <= 0 || info.nvolumes >= 32768)
158                 return (EINVAL);
159
160         /*
161          * Interal mount data structure
162          */
163         if (mp->mnt_flag & MNT_UPDATE) {
164                 hmp = (void *)mp->mnt_data;
165                 KKASSERT(hmp != NULL);
166         } else {
167                 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
168                 mp->mnt_data = (qaddr_t)hmp;
169                 hmp->mp = mp;
170                 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
171                 hmp->namekey_iterator = mycpu->gd_time_seconds;
172                 /*TAILQ_INIT(&hmp->recycle_list);*/
173         }
174         hmp->hflags = info.hflags;
175         if (info.asof) {
176                 mp->mnt_flag |= MNT_RDONLY;
177                 hmp->asof = info.asof;
178         } else {
179                 hmp->asof = HAMMER_MAX_TID;
180         }
181
182         /*
183          * Re-open read-write if originally read-only, or vise-versa XXX
184          */
185         if (mp->mnt_flag & MNT_UPDATE) {
186                 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
187                         kprintf("HAMMER read-write -> read-only XXX\n");
188                         hmp->ronly = 1;
189                 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
190                         kprintf("HAMMER read-only -> read-write XXX\n");
191                         hmp->ronly = 0;
192                 }
193                 return(0);
194         }
195
196         RB_INIT(&hmp->rb_vols_root);
197         RB_INIT(&hmp->rb_inos_root);
198         hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
199
200         /*
201          * Load volumes
202          */
203         path = objcache_get(namei_oc, M_WAITOK);
204         hmp->nvolumes = info.nvolumes;
205         for (i = 0; i < info.nvolumes; ++i) {
206                 error = copyin(&info.volumes[i], &upath, sizeof(char *));
207                 if (error == 0)
208                         error = copyinstr(upath, path, MAXPATHLEN, NULL);
209                 if (error == 0)
210                         error = hammer_install_volume(hmp, path);
211                 if (error)
212                         break;
213         }
214         objcache_put(namei_oc, path);
215
216         /*
217          * Make sure we found a root volume
218          */
219         if (error == 0 && hmp->rootvol == NULL) {
220                 kprintf("hammer_mount: No root volume found!\n");
221                 error = EINVAL;
222         }
223         if (error == 0 && hmp->rootcl == NULL) {
224                 kprintf("hammer_mount: No root cluster found!\n");
225                 error = EINVAL;
226         }
227         if (error) {
228                 hammer_free_hmp(mp);
229                 return (error);
230         }
231
232         /*
233          * No errors, setup enough of the mount point so we can lookup the
234          * root vnode.
235          */
236         mp->mnt_iosize_max = MAXPHYS;
237         mp->mnt_kern_flag |= MNTK_FSMID;
238
239         /* 
240          * note: f_iosize is used by vnode_pager_haspage() when constructing
241          * its VOP_BMAP call.
242          */
243         mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
244         mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
245         mp->mnt_maxsymlinklen = 255;
246         mp->mnt_flag |= MNT_LOCAL;
247
248         vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
249         vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
250         vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
251
252         /*
253          * The root volume's ondisk pointer is only valid if we hold a
254          * reference to it.
255          */
256         rootvol = hammer_get_root_volume(hmp, &error);
257         if (error)
258                 goto done;
259         ksnprintf(mp->mnt_stat.f_mntfromname,
260                   sizeof(mp->mnt_stat.f_mntfromname), "%s",
261                   rootvol->ondisk->vol_name);
262         mp->mnt_stat.f_fsid.val[0] =
263                 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
264         mp->mnt_stat.f_fsid.val[1] =
265                 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
266         hammer_rel_volume(rootvol, 0);
267
268         /*
269          * Locate the root directory using the root cluster's B-Tree as a
270          * starting point.  The root directory uses an obj_id of 1.
271          *
272          * FUTURE: Leave the root directory cached referenced but unlocked
273          * in hmp->rootvp (need to flush it on unmount).
274          */
275         error = hammer_vfs_vget(mp, 1, &rootvp);
276         if (error)
277                 goto done;
278         vput(rootvp);
279         /*vn_unlock(hmp->rootvp);*/
280
281 done:
282         /*
283          * Cleanup and return.
284          */
285         if (error)
286                 hammer_free_hmp(mp);
287         return (error);
288 }
289
290 static int
291 hammer_vfs_unmount(struct mount *mp, int mntflags)
292 {
293 #if 0
294         struct hammer_mount *hmp = (void *)mp->mnt_data;
295 #endif
296         int flags;
297         int error;
298
299         /*
300          * Clean out the vnodes
301          */
302         flags = 0;
303         if (mntflags & MNT_FORCE)
304                 flags |= FORCECLOSE;
305         if ((error = vflush(mp, 0, flags)) != 0)
306                 return (error);
307
308         /*
309          * Clean up the internal mount structure and related entities.  This
310          * may issue I/O.
311          */
312         hammer_free_hmp(mp);
313         return(0);
314 }
315
316 /*
317  * Clean up the internal mount structure and disassociate it from the mount.
318  * This may issue I/O.
319  */
320 static void
321 hammer_free_hmp(struct mount *mp)
322 {
323         struct hammer_mount *hmp = (void *)mp->mnt_data;
324
325 #if 0
326         /*
327          * Clean up the root vnode
328          */
329         if (hmp->rootvp) {
330                 vrele(hmp->rootvp);
331                 hmp->rootvp = NULL;
332         }
333 #endif
334
335         /*
336          * Unload & flush inodes
337          */
338         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
339                 hammer_unload_inode, (void *)MNT_WAIT);
340
341         /*
342          * Unload & flush volumes
343          */
344         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
345                 hammer_unload_volume, NULL);
346
347         mp->mnt_data = NULL;
348         mp->mnt_flag &= ~MNT_LOCAL;
349         hmp->mp = NULL;
350         kfree(hmp->zbuf, M_HAMMER);
351         kfree(hmp, M_HAMMER);
352 }
353
354 /*
355  * Obtain a vnode for the specified inode number.  An exclusively locked
356  * vnode is returned.
357  */
358 int
359 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
360 {
361         struct hammer_mount *hmp = (void *)mp->mnt_data;
362         struct hammer_inode *ip;
363         int error;
364
365         /*
366          * Get/allocate the hammer_inode structure.  The structure must be
367          * unlocked while we manipulate the related vnode to avoid a
368          * deadlock.
369          */
370         ip = hammer_get_inode(hmp, NULL, ino, hmp->asof, 0, &error);
371         if (ip == NULL) {
372                 *vpp = NULL;
373                 return(error);
374         }
375         error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
376         hammer_rel_inode(ip, 0);
377         return (error);
378 }
379
380 /*
381  * Return the root vnode for the filesystem.
382  *
383  * HAMMER stores the root vnode in the hammer_mount structure so
384  * getting it is easy.
385  */
386 static int
387 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
388 {
389         struct hammer_mount *hmp = (void *)mp->mnt_data;
390         int error;
391
392         if (hmp->rootcl == NULL)
393                 error = EIO;
394         else
395                 error = hammer_vfs_vget(mp, 1, vpp);
396         return (error);
397 #if 0
398         /* FUTURE - cached root vnode */
399         if ((vp = hmp->rootvp) != NULL) {
400                 vref(vp);
401                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
402                 *vpp = vp;
403                 return (0);
404         } else {
405                 *vpp = NULL;
406                 return (EIO);
407         }
408 #endif
409 }
410
411 static int
412 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
413 {
414         struct hammer_mount *hmp = (void *)mp->mnt_data;
415         hammer_volume_t volume;
416         hammer_volume_ondisk_t ondisk;
417         int error;
418
419         volume = hammer_get_root_volume(hmp, &error);
420         if (error)
421                 return(error);
422
423         ondisk = volume->ondisk;
424
425         mp->mnt_stat.f_bfree = mp->mnt_stat.f_blocks -
426                                 ondisk->vol0_stat_idx_bufs -
427                                 ondisk->vol0_stat_rec_bufs -
428                                 ondisk->vol0_stat_data_bufs;
429         if (mp->mnt_stat.f_bfree < 0)
430                 mp->mnt_stat.f_bfree = 0;
431         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
432         mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
433         if (mp->mnt_stat.f_files < 0)
434                 mp->mnt_stat.f_files = 0;
435
436         hammer_rel_volume(volume, 0);
437         *sbp = mp->mnt_stat;
438         return(0);
439 }
440
441 static int
442 hammer_vfs_sync(struct mount *mp, int waitfor)
443 {
444         struct hammer_mount *hmp = (void *)mp->mnt_data;
445         int error;
446
447         error = hammer_sync_hmp(hmp, waitfor);
448         return(error);
449 }
450
451 /*
452  * Convert a vnode to a file handle.
453  */
454 static int
455 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
456 {
457         hammer_inode_t ip;
458
459         KKASSERT(MAXFIDSZ >= 16);
460         ip = VTOI(vp);
461         fhp->fid_len = offsetof(struct fid, fid_data[16]);
462         fhp->fid_reserved = 0;
463         bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
464         bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
465         return(0);
466 }
467
468
469 /*
470  * Convert a file handle back to a vnode.
471  */
472 static int
473 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
474 {
475         struct hammer_mount *hmp = (void *)mp->mnt_data;
476         struct hammer_inode *ip;
477         struct hammer_inode_info info;
478         int error;
479
480         bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
481         bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
482
483         /*
484          * Get/allocate the hammer_inode structure.  The structure must be
485          * unlocked while we manipulate the related vnode to avoid a
486          * deadlock.
487          */
488         ip = hammer_get_inode(hmp, NULL, info.obj_id, info.obj_asof, 0, &error);
489         if (ip == NULL) {
490                 *vpp = NULL;
491                 return(error);
492         }
493         error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
494         hammer_rel_inode(ip, 0);
495         return (error);
496 }
497
498 static int
499 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
500                     int *exflagsp, struct ucred **credanonp)
501 {
502         hammer_mount_t hmp = (void *)mp->mnt_data;
503         struct netcred *np;
504         int error;
505
506         np = vfs_export_lookup(mp, &hmp->export, nam);
507         if (np) {
508                 *exflagsp = np->netc_exflags;
509                 *credanonp = &np->netc_anon;
510                 error = 0;
511         } else {
512                 error = EACCES;
513         }
514         return (error);
515
516 }
517
518 int
519 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
520 {
521         hammer_mount_t hmp = (void *)mp->mnt_data;
522         int error;
523
524         switch(op) {
525         case MOUNTCTL_SET_EXPORT:
526                 error = vfs_export(mp, &hmp->export, export);
527                 break;
528         default:
529                 error = EOPNOTSUPP;
530                 break;
531         }
532         return(error);
533 }
534