HAMMER: MFC all changes through 20080924
[dragonfly.git] / sys / vfs / hammer / hammer_vfsops.c
1 /*
2  * Copyright (c) 2007-2008 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.63.2.7 2008/09/25 01:42:52 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_io;
51 int hammer_debug_general;
52 int hammer_debug_debug = 1;             /* medium-error panics */ 
53 int hammer_debug_inode;
54 int hammer_debug_locks;
55 int hammer_debug_btree;
56 int hammer_debug_tid;
57 int hammer_debug_recover;               /* -1 will disable, +1 will force */
58 int hammer_debug_recover_faults;
59 int hammer_cluster_enable = 1;          /* enable read clustering by default */
60 int hammer_count_fsyncs;
61 int hammer_count_inodes;
62 int hammer_count_iqueued;
63 int hammer_count_reclaiming;
64 int hammer_count_records;
65 int hammer_count_record_datas;
66 int hammer_count_volumes;
67 int hammer_count_buffers;
68 int hammer_count_nodes;
69 int64_t hammer_count_extra_space_used;
70 int64_t hammer_stats_btree_lookups;
71 int64_t hammer_stats_btree_searches;
72 int64_t hammer_stats_btree_inserts;
73 int64_t hammer_stats_btree_deletes;
74 int64_t hammer_stats_btree_elements;
75 int64_t hammer_stats_btree_splits;
76 int64_t hammer_stats_btree_iterations;
77 int64_t hammer_stats_record_iterations;
78
79 int64_t hammer_stats_file_read;
80 int64_t hammer_stats_file_write;
81 int64_t hammer_stats_file_iopsr;
82 int64_t hammer_stats_file_iopsw;
83 int64_t hammer_stats_disk_read;
84 int64_t hammer_stats_disk_write;
85 int64_t hammer_stats_inode_flushes;
86 int64_t hammer_stats_commits;
87
88 int hammer_count_dirtybufspace;         /* global */
89 int hammer_count_refedbufs;             /* global */
90 int hammer_count_reservations;
91 int hammer_count_io_running_read;
92 int hammer_count_io_running_write;
93 int hammer_count_io_locked;
94 int hammer_limit_dirtybufspace;         /* per-mount */
95 int hammer_limit_recs;                  /* as a whole XXX */
96 int hammer_limit_iqueued;               /* per-mount */
97 int hammer_autoflush = 2000;            /* auto flush */
98 int hammer_bio_count;
99 int hammer_verify_zone;
100 int hammer_verify_data = 1;
101 int hammer_write_mode;
102 int64_t hammer_contention_count;
103 int64_t hammer_zone_limit;
104
105 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
106 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
107            &hammer_debug_general, 0, "");
108 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
109            &hammer_debug_io, 0, "");
110 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
111            &hammer_debug_debug, 0, "");
112 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
113            &hammer_debug_inode, 0, "");
114 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
115            &hammer_debug_locks, 0, "");
116 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
117            &hammer_debug_btree, 0, "");
118 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
119            &hammer_debug_tid, 0, "");
120 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
121            &hammer_debug_recover, 0, "");
122 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
123            &hammer_debug_recover_faults, 0, "");
124 SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW,
125            &hammer_cluster_enable, 0, "");
126
127 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
128            &hammer_limit_dirtybufspace, 0, "");
129 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
130            &hammer_limit_recs, 0, "");
131 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_iqueued, CTLFLAG_RW,
132            &hammer_limit_iqueued, 0, "");
133
134 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD,
135            &hammer_count_fsyncs, 0, "");
136 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
137            &hammer_count_inodes, 0, "");
138 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
139            &hammer_count_iqueued, 0, "");
140 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaiming, CTLFLAG_RD,
141            &hammer_count_reclaiming, 0, "");
142 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
143            &hammer_count_records, 0, "");
144 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
145            &hammer_count_record_datas, 0, "");
146 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
147            &hammer_count_volumes, 0, "");
148 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
149            &hammer_count_buffers, 0, "");
150 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
151            &hammer_count_nodes, 0, "");
152 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD,
153            &hammer_count_extra_space_used, 0, "");
154
155 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
156            &hammer_stats_btree_searches, 0, "");
157 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
158            &hammer_stats_btree_lookups, 0, "");
159 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
160            &hammer_stats_btree_inserts, 0, "");
161 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
162            &hammer_stats_btree_deletes, 0, "");
163 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
164            &hammer_stats_btree_elements, 0, "");
165 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
166            &hammer_stats_btree_splits, 0, "");
167 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
168            &hammer_stats_btree_iterations, 0, "");
169 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
170            &hammer_stats_record_iterations, 0, "");
171
172 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD,
173            &hammer_stats_file_read, 0, "");
174 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD,
175            &hammer_stats_file_write, 0, "");
176 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD,
177            &hammer_stats_file_iopsr, 0, "");
178 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD,
179            &hammer_stats_file_iopsw, 0, "");
180 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD,
181            &hammer_stats_disk_read, 0, "");
182 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD,
183            &hammer_stats_disk_write, 0, "");
184 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD,
185            &hammer_stats_inode_flushes, 0, "");
186 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD,
187            &hammer_stats_commits, 0, "");
188
189 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
190            &hammer_count_dirtybufspace, 0, "");
191 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
192            &hammer_count_refedbufs, 0, "");
193 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
194            &hammer_count_reservations, 0, "");
195 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
196            &hammer_count_io_running_read, 0, "");
197 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
198            &hammer_count_io_locked, 0, "");
199 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
200            &hammer_count_io_running_write, 0, "");
201 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
202            &hammer_zone_limit, 0, "");
203 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
204            &hammer_contention_count, 0, "");
205 SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW,
206            &hammer_autoflush, 0, "");
207 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
208            &hammer_verify_zone, 0, "");
209 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW,
210            &hammer_verify_data, 0, "");
211 SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW,
212            &hammer_write_mode, 0, "");
213
214 KTR_INFO_MASTER(hammer);
215
216 /*
217  * VFS ABI
218  */
219 static void     hammer_free_hmp(struct mount *mp);
220
221 static int      hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
222                                 struct ucred *cred);
223 static int      hammer_vfs_unmount(struct mount *mp, int mntflags);
224 static int      hammer_vfs_root(struct mount *mp, struct vnode **vpp);
225 static int      hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
226                                 struct ucred *cred);
227 static int      hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
228                                 struct ucred *cred);
229 static int      hammer_vfs_sync(struct mount *mp, int waitfor);
230 static int      hammer_vfs_vget(struct mount *mp, ino_t ino,
231                                 struct vnode **vpp);
232 static int      hammer_vfs_init(struct vfsconf *conf);
233 static int      hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
234                                 struct fid *fhp, struct vnode **vpp);
235 static int      hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
236 static int      hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
237                                 int *exflagsp, struct ucred **credanonp);
238
239
240 static struct vfsops hammer_vfsops = {
241         .vfs_mount      = hammer_vfs_mount,
242         .vfs_unmount    = hammer_vfs_unmount,
243         .vfs_root       = hammer_vfs_root,
244         .vfs_statfs     = hammer_vfs_statfs,
245         .vfs_statvfs    = hammer_vfs_statvfs,
246         .vfs_sync       = hammer_vfs_sync,
247         .vfs_vget       = hammer_vfs_vget,
248         .vfs_init       = hammer_vfs_init,
249         .vfs_vptofh     = hammer_vfs_vptofh,
250         .vfs_fhtovp     = hammer_vfs_fhtovp,
251         .vfs_checkexp   = hammer_vfs_checkexp
252 };
253
254 MALLOC_DEFINE(M_HAMMER, "hammer-general", "hammer general");
255 MALLOC_DEFINE(M_HAMMER_INO, "hammer-inodes", "hammer inodes");
256
257 VFS_SET(hammer_vfsops, hammer, 0);
258 MODULE_VERSION(hammer, 1);
259
260 static int
261 hammer_vfs_init(struct vfsconf *conf)
262 {
263         int n;
264
265         if (hammer_limit_recs == 0) {
266                 hammer_limit_recs = nbuf * 25;
267                 n = kmalloc_limit(M_HAMMER) / 512;
268                 if (hammer_limit_recs > n)
269                         hammer_limit_recs = n;
270         }
271         if (hammer_limit_dirtybufspace == 0) {
272                 hammer_limit_dirtybufspace = hidirtybufspace / 2;
273                 if (hammer_limit_dirtybufspace < 100)
274                         hammer_limit_dirtybufspace = 100;
275         }
276         if (hammer_limit_iqueued == 0)
277                 hammer_limit_iqueued = desiredvnodes / 5;
278         return(0);
279 }
280
281 static int
282 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
283                  struct ucred *cred)
284 {
285         struct hammer_mount_info info;
286         hammer_mount_t hmp;
287         hammer_volume_t rootvol;
288         struct vnode *rootvp;
289         struct vnode *devvp = NULL;
290         const char *upath;      /* volume name in userspace */
291         char *path;             /* volume name in system space */
292         int error;
293         int i;
294         int master_id;
295         if (mntpt == NULL) {
296                 /*
297                  * Root mount
298                  */
299                 if ((error = bdevvp(rootdev, &devvp))) {
300                         kprintf("hammer_mountroot: can't find devvp\n");
301                         return (error);
302                 }
303                 mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
304                 bzero(&info, sizeof(info));
305                 info.asof = 0;
306                 info.hflags = 0;
307                 info.nvolumes = 1;
308         } else {
309                 if ((error = copyin(data, &info, sizeof(info))) != 0)
310                         return (error);
311         }
312
313         /*
314          * updating or new mount
315          */
316         if (mp->mnt_flag & MNT_UPDATE) {
317                 hmp = (void *)mp->mnt_data;
318                 KKASSERT(hmp != NULL);
319         } else {
320                 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
321                         return (EINVAL);
322                 hmp = NULL;
323         }
324
325         /*
326          * master-id validation.  The master id may not be changed by a
327          * mount update.
328          */
329         if (info.hflags & HMNT_MASTERID) {
330                 if (hmp && hmp->master_id != info.master_id) {
331                         kprintf("hammer: cannot change master id "
332                                 "with mount update\n");
333                         return(EINVAL);
334                 }
335                 master_id = info.master_id;
336                 if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
337                         return (EINVAL);
338         } else {
339                 if (hmp)
340                         master_id = hmp->master_id;
341                 else
342                         master_id = 0;
343         }
344
345         /*
346          * Interal mount data structure
347          */
348         if (hmp == NULL) {
349                 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
350                 mp->mnt_data = (qaddr_t)hmp;
351                 hmp->mp = mp;
352                 hmp->namekey_iterator = mycpu->gd_time_seconds;
353                 /*TAILQ_INIT(&hmp->recycle_list);*/
354
355                 hmp->root_btree_beg.localization = 0x00000000U;
356                 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
357                 hmp->root_btree_beg.key = -0x8000000000000000LL;
358                 hmp->root_btree_beg.create_tid = 1;
359                 hmp->root_btree_beg.delete_tid = 1;
360                 hmp->root_btree_beg.rec_type = 0;
361                 hmp->root_btree_beg.obj_type = 0;
362
363                 hmp->root_btree_end.localization = 0xFFFFFFFFU;
364                 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
365                 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
366                 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
367                 hmp->root_btree_end.delete_tid = 0;   /* special case */
368                 hmp->root_btree_end.rec_type = 0xFFFFU;
369                 hmp->root_btree_end.obj_type = 0;
370
371                 hmp->krate.freq = 1;    /* maximum reporting rate (hz) */
372                 hmp->krate.count = -16; /* initial burst */
373
374                 hmp->sync_lock.refs = 1;
375                 hmp->free_lock.refs = 1;
376                 hmp->undo_lock.refs = 1;
377                 hmp->blkmap_lock.refs = 1;
378
379                 TAILQ_INIT(&hmp->delay_list);
380                 TAILQ_INIT(&hmp->flush_group_list);
381                 TAILQ_INIT(&hmp->objid_cache_list);
382                 TAILQ_INIT(&hmp->undo_lru_list);
383                 TAILQ_INIT(&hmp->reclaim_list);
384         }
385         hmp->hflags &= ~HMNT_USERFLAGS;
386         hmp->hflags |= info.hflags & HMNT_USERFLAGS;
387
388         hmp->master_id = master_id;
389
390         if (info.asof) {
391                 mp->mnt_flag |= MNT_RDONLY;
392                 hmp->asof = info.asof;
393         } else {
394                 hmp->asof = HAMMER_MAX_TID;
395         }
396
397         /*
398          * Re-open read-write if originally read-only, or vise-versa.
399          */
400         if (mp->mnt_flag & MNT_UPDATE) {
401                 error = 0;
402                 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
403                         kprintf("HAMMER read-only -> read-write\n");
404                         hmp->ronly = 0;
405                         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
406                                 hammer_adjust_volume_mode, NULL);
407                         rootvol = hammer_get_root_volume(hmp, &error);
408                         if (rootvol) {
409                                 hammer_recover_flush_buffers(hmp, rootvol, 1);
410                                 bcopy(rootvol->ondisk->vol0_blockmap,
411                                       hmp->blockmap,
412                                       sizeof(hmp->blockmap));
413                                 hammer_rel_volume(rootvol, 0);
414                         }
415                         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
416                                 hammer_reload_inode, NULL);
417                         /* kernel clears MNT_RDONLY */
418                 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
419                         kprintf("HAMMER read-write -> read-only\n");
420                         hmp->ronly = 1; /* messy */
421                         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
422                                 hammer_reload_inode, NULL);
423                         hmp->ronly = 0;
424                         hammer_flusher_sync(hmp);
425                         hammer_flusher_sync(hmp);
426                         hammer_flusher_sync(hmp);
427                         hmp->ronly = 1;
428                         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
429                                 hammer_adjust_volume_mode, NULL);
430                 }
431                 return(error);
432         }
433
434         RB_INIT(&hmp->rb_vols_root);
435         RB_INIT(&hmp->rb_inos_root);
436         RB_INIT(&hmp->rb_nods_root);
437         RB_INIT(&hmp->rb_undo_root);
438         RB_INIT(&hmp->rb_resv_root);
439         RB_INIT(&hmp->rb_bufs_root);
440         RB_INIT(&hmp->rb_pfsm_root);
441
442         hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
443
444         TAILQ_INIT(&hmp->volu_list);
445         TAILQ_INIT(&hmp->undo_list);
446         TAILQ_INIT(&hmp->data_list);
447         TAILQ_INIT(&hmp->meta_list);
448         TAILQ_INIT(&hmp->lose_list);
449
450         /*
451          * Load volumes
452          */
453         path = objcache_get(namei_oc, M_WAITOK);
454         hmp->nvolumes = -1;
455         for (i = 0; i < info.nvolumes; ++i) {
456                 if (mntpt == NULL) {
457                         /*
458                          * Root mount.
459                          * Only one volume; and no need for copyin.
460                          */
461                         KKASSERT(info.nvolumes == 1);
462                         ksnprintf(path, MAXPATHLEN, "/dev/%s",
463                                   mp->mnt_stat.f_mntfromname);  
464                         error = 0;
465                 } else {
466                         error = copyin(&info.volumes[i], &upath,
467                                        sizeof(char *));
468                         if (error == 0)
469                                 error = copyinstr(upath, path,
470                                                   MAXPATHLEN, NULL);
471                 }
472                 if (error == 0)
473                         error = hammer_install_volume(hmp, path, devvp);
474                 if (error)
475                         break;
476         }
477         objcache_put(namei_oc, path);
478
479         /*
480          * Make sure we found a root volume
481          */
482         if (error == 0 && hmp->rootvol == NULL) {
483                 kprintf("hammer_mount: No root volume found!\n");
484                 error = EINVAL;
485         }
486
487         /*
488          * Check that all required volumes are available
489          */
490         if (error == 0 && hammer_mountcheck_volumes(hmp)) {
491                 kprintf("hammer_mount: Missing volumes, cannot mount!\n");
492                 error = EINVAL;
493         }
494
495         if (error) {
496                 hammer_free_hmp(mp);
497                 return (error);
498         }
499
500         /*
501          * No errors, setup enough of the mount point so we can lookup the
502          * root vnode.
503          */
504         mp->mnt_iosize_max = MAXPHYS;
505         mp->mnt_kern_flag |= MNTK_FSMID;
506
507         /* 
508          * note: f_iosize is used by vnode_pager_haspage() when constructing
509          * its VOP_BMAP call.
510          */
511         mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
512         mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
513
514         mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
515         mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
516
517         mp->mnt_maxsymlinklen = 255;
518         mp->mnt_flag |= MNT_LOCAL;
519
520         vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
521         vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
522         vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
523
524         /*
525          * The root volume's ondisk pointer is only valid if we hold a
526          * reference to it.
527          */
528         rootvol = hammer_get_root_volume(hmp, &error);
529         if (error)
530                 goto failed;
531
532         /*
533          * Perform any necessary UNDO operations.  The recovery code does
534          * call hammer_undo_lookup() so we have to pre-cache the blockmap,
535          * and then re-copy it again after recovery is complete.
536          *
537          * If this is a read-only mount the UNDO information is retained
538          * in memory in the form of dirty buffer cache buffers, and not
539          * written back to the media.
540          */
541         bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
542               sizeof(hmp->blockmap));
543
544         /*
545          * The undo_rec_limit limits the size of flush groups to avoid
546          * blowing out the UNDO FIFO.  This calculation is typically in
547          * the tens of thousands and is designed primarily when small
548          * HAMMER filesystems are created.
549          */
550         hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
551         if (hammer_debug_general & 0x0001)
552                 kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit);
553
554         error = hammer_recover(hmp, rootvol);
555         if (error) {
556                 kprintf("Failed to recover HAMMER filesystem on mount\n");
557                 goto done;
558         }
559
560         /*
561          * Finish setup now that we have a good root volume.
562          *
563          * The top 16 bits of fsid.val[1] is a pfs id.
564          */
565         ksnprintf(mp->mnt_stat.f_mntfromname,
566                   sizeof(mp->mnt_stat.f_mntfromname), "%s",
567                   rootvol->ondisk->vol_name);
568         mp->mnt_stat.f_fsid.val[0] =
569                 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
570         mp->mnt_stat.f_fsid.val[1] =
571                 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
572         mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF;
573
574         mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
575         mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
576                                      sizeof(mp->mnt_vstat.f_fsid_uuid));
577
578         /*
579          * Certain often-modified fields in the root volume are cached in
580          * the hammer_mount structure so we do not have to generate lots
581          * of little UNDO structures for them.
582          *
583          * Recopy after recovery.  This also has the side effect of
584          * setting our cached undo FIFO's first_offset, which serves to
585          * placemark the FIFO start for the NEXT flush cycle while the
586          * on-disk first_offset represents the LAST flush cycle.
587          */
588         hmp->next_tid = rootvol->ondisk->vol0_next_tid;
589         hmp->flush_tid1 = hmp->next_tid;
590         hmp->flush_tid2 = hmp->next_tid;
591         bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
592               sizeof(hmp->blockmap));
593         hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
594
595         hammer_flusher_create(hmp);
596
597         /*
598          * Locate the root directory using the root cluster's B-Tree as a
599          * starting point.  The root directory uses an obj_id of 1.
600          *
601          * FUTURE: Leave the root directory cached referenced but unlocked
602          * in hmp->rootvp (need to flush it on unmount).
603          */
604         error = hammer_vfs_vget(mp, 1, &rootvp);
605         if (error)
606                 goto done;
607         vput(rootvp);
608         /*vn_unlock(hmp->rootvp);*/
609
610 done:
611         hammer_rel_volume(rootvol, 0);
612 failed:
613         /*
614          * Cleanup and return.
615          */
616         if (error)
617                 hammer_free_hmp(mp);
618         return (error);
619 }
620
621 static int
622 hammer_vfs_unmount(struct mount *mp, int mntflags)
623 {
624 #if 0
625         struct hammer_mount *hmp = (void *)mp->mnt_data;
626 #endif
627         int flags;
628         int error;
629
630         /*
631          * Clean out the vnodes
632          */
633         flags = 0;
634         if (mntflags & MNT_FORCE)
635                 flags |= FORCECLOSE;
636         if ((error = vflush(mp, 0, flags)) != 0)
637                 return (error);
638
639         /*
640          * Clean up the internal mount structure and related entities.  This
641          * may issue I/O.
642          */
643         hammer_free_hmp(mp);
644         return(0);
645 }
646
647 /*
648  * Clean up the internal mount structure and disassociate it from the mount.
649  * This may issue I/O.
650  */
651 static void
652 hammer_free_hmp(struct mount *mp)
653 {
654         struct hammer_mount *hmp = (void *)mp->mnt_data;
655         hammer_flush_group_t flg;
656         int count;
657
658         /*
659          * Flush anything dirty.  This won't even run if the
660          * filesystem errored-out.
661          */
662         count = 0;
663         while (hammer_flusher_haswork(hmp)) {
664                 hammer_flusher_sync(hmp);
665                 ++count;
666                 if (count >= 5) {
667                         if (count == 5)
668                                 kprintf("HAMMER: umount flushing.");
669                         else
670                                 kprintf(".");
671                         tsleep(hmp, 0, "hmrufl", hz);
672                 }
673                 if (count == 30) {
674                         kprintf("giving up\n");
675                         break;
676                 }
677         }
678         if (count >= 5 && count < 30)
679                 kprintf("\n");
680
681         /*
682          * If the mount had a critical error we have to destroy any
683          * remaining inodes before we can finish cleaning up the flusher.
684          */
685         if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
686                 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
687                         hammer_destroy_inode_callback, NULL);
688         }
689
690         /*
691          * There shouldn't be any inodes left now and any left over
692          * flush groups should now be empty.
693          */
694         KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
695         while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
696                 TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
697                 KKASSERT(TAILQ_EMPTY(&flg->flush_list));
698                 if (flg->refs) {
699                         kprintf("HAMMER: Warning, flush_group %p was "
700                                 "not empty on umount!\n", flg);
701                 }
702                 kfree(flg, M_HAMMER);
703         }
704
705         /*
706          * We can finally destroy the flusher
707          */
708         hammer_flusher_destroy(hmp);
709
710         /*
711          * We may have held recovered buffers due to a read-only mount.
712          * These must be discarded.
713          */
714         if (hmp->ronly)
715                 hammer_recover_flush_buffers(hmp, NULL, -1);
716
717         /*
718          * Unload buffers and then volumes
719          */
720         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
721                 hammer_unload_buffer, NULL);
722         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
723                 hammer_unload_volume, NULL);
724
725         mp->mnt_data = NULL;
726         mp->mnt_flag &= ~MNT_LOCAL;
727         hmp->mp = NULL;
728         hammer_destroy_objid_cache(hmp);
729         kfree(hmp, M_HAMMER);
730 }
731
732 /*
733  * Report critical errors.  ip may be NULL.
734  */
735 void
736 hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
737                       int error, const char *msg)
738 {
739         hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
740         krateprintf(&hmp->krate,
741                 "HAMMER(%s): Critical error inode=%lld %s\n",
742                 hmp->mp->mnt_stat.f_mntfromname,
743                 (ip ? ip->obj_id : -1), msg);
744         if (hmp->ronly == 0) {
745                 hmp->ronly = 2;         /* special errored read-only mode */
746                 hmp->mp->mnt_flag |= MNT_RDONLY;
747                 kprintf("HAMMER(%s): Forcing read-only mode\n",
748                         hmp->mp->mnt_stat.f_mntfromname);
749         }
750         hmp->error = error;
751 }
752
753
754 /*
755  * Obtain a vnode for the specified inode number.  An exclusively locked
756  * vnode is returned.
757  */
758 int
759 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
760 {
761         struct hammer_transaction trans;
762         struct hammer_mount *hmp = (void *)mp->mnt_data;
763         struct hammer_inode *ip;
764         int error;
765
766         hammer_simple_transaction(&trans, hmp);
767
768         /*
769          * Lookup the requested HAMMER inode.  The structure must be
770          * left unlocked while we manipulate the related vnode to avoid
771          * a deadlock.
772          */
773         ip = hammer_get_inode(&trans, NULL, ino,
774                               hmp->asof, HAMMER_DEF_LOCALIZATION, 
775                               0, &error);
776         if (ip == NULL) {
777                 *vpp = NULL;
778                 hammer_done_transaction(&trans);
779                 return(error);
780         }
781         error = hammer_get_vnode(ip, vpp);
782         hammer_rel_inode(ip, 0);
783         hammer_done_transaction(&trans);
784         return (error);
785 }
786
787 /*
788  * Return the root vnode for the filesystem.
789  *
790  * HAMMER stores the root vnode in the hammer_mount structure so
791  * getting it is easy.
792  */
793 static int
794 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
795 {
796 #if 0
797         struct hammer_mount *hmp = (void *)mp->mnt_data;
798 #endif
799         int error;
800
801         error = hammer_vfs_vget(mp, 1, vpp);
802         return (error);
803 }
804
805 static int
806 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
807 {
808         struct hammer_mount *hmp = (void *)mp->mnt_data;
809         hammer_volume_t volume;
810         hammer_volume_ondisk_t ondisk;
811         int error;
812         int64_t bfree;
813
814         volume = hammer_get_root_volume(hmp, &error);
815         if (error)
816                 return(error);
817         ondisk = volume->ondisk;
818
819         /*
820          * Basic stats
821          */
822         mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
823         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
824         hammer_rel_volume(volume, 0);
825
826         mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
827         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
828         if (mp->mnt_stat.f_files < 0)
829                 mp->mnt_stat.f_files = 0;
830
831         *sbp = mp->mnt_stat;
832         return(0);
833 }
834
835 static int
836 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
837 {
838         struct hammer_mount *hmp = (void *)mp->mnt_data;
839         hammer_volume_t volume;
840         hammer_volume_ondisk_t ondisk;
841         int error;
842         int64_t bfree;
843
844         volume = hammer_get_root_volume(hmp, &error);
845         if (error)
846                 return(error);
847         ondisk = volume->ondisk;
848
849         /*
850          * Basic stats
851          */
852         mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
853         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
854         hammer_rel_volume(volume, 0);
855
856         mp->mnt_vstat.f_bfree = bfree / HAMMER_BUFSIZE;
857         mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
858         if (mp->mnt_vstat.f_files < 0)
859                 mp->mnt_vstat.f_files = 0;
860         *sbp = mp->mnt_vstat;
861         return(0);
862 }
863
864 /*
865  * Sync the filesystem.  Currently we have to run it twice, the second
866  * one will advance the undo start index to the end index, so if a crash
867  * occurs no undos will be run on mount.
868  *
869  * We do not sync the filesystem if we are called from a panic.  If we did
870  * we might end up blowing up a sync that was already in progress.
871  */
872 static int
873 hammer_vfs_sync(struct mount *mp, int waitfor)
874 {
875         struct hammer_mount *hmp = (void *)mp->mnt_data;
876         int error;
877
878         if (panicstr == NULL) {
879                 error = hammer_sync_hmp(hmp, waitfor);
880         } else {
881                 error = EIO;
882         }
883         return (error);
884 }
885
886 /*
887  * Convert a vnode to a file handle.
888  */
889 static int
890 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
891 {
892         hammer_inode_t ip;
893
894         KKASSERT(MAXFIDSZ >= 16);
895         ip = VTOI(vp);
896         fhp->fid_len = offsetof(struct fid, fid_data[16]);
897         fhp->fid_ext = ip->obj_localization >> 16;
898         bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
899         bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
900         return(0);
901 }
902
903
904 /*
905  * Convert a file handle back to a vnode.
906  *
907  * Use rootvp to enforce PFS isolation when a PFS is exported via a
908  * null mount.
909  */
910 static int
911 hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
912                   struct fid *fhp, struct vnode **vpp)
913 {
914         struct hammer_transaction trans;
915         struct hammer_inode *ip;
916         struct hammer_inode_info info;
917         int error;
918         u_int32_t localization;
919
920         bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
921         bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
922         if (rootvp)
923                 localization = VTOI(rootvp)->obj_localization;
924         else
925                 localization = (u_int32_t)fhp->fid_ext << 16;
926
927         hammer_simple_transaction(&trans, (void *)mp->mnt_data);
928
929         /*
930          * Get/allocate the hammer_inode structure.  The structure must be
931          * unlocked while we manipulate the related vnode to avoid a
932          * deadlock.
933          */
934         ip = hammer_get_inode(&trans, NULL, info.obj_id,
935                               info.obj_asof, localization, 0, &error);
936         if (ip == NULL) {
937                 *vpp = NULL;
938                 return(error);
939         }
940         error = hammer_get_vnode(ip, vpp);
941         hammer_rel_inode(ip, 0);
942         hammer_done_transaction(&trans);
943         return (error);
944 }
945
946 static int
947 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
948                     int *exflagsp, struct ucred **credanonp)
949 {
950         hammer_mount_t hmp = (void *)mp->mnt_data;
951         struct netcred *np;
952         int error;
953
954         np = vfs_export_lookup(mp, &hmp->export, nam);
955         if (np) {
956                 *exflagsp = np->netc_exflags;
957                 *credanonp = &np->netc_anon;
958                 error = 0;
959         } else {
960                 error = EACCES;
961         }
962         return (error);
963
964 }
965
966 int
967 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
968 {
969         hammer_mount_t hmp = (void *)mp->mnt_data;
970         int error;
971
972         switch(op) {
973         case MOUNTCTL_SET_EXPORT:
974                 error = vfs_export(mp, &hmp->export, export);
975                 break;
976         default:
977                 error = EOPNOTSUPP;
978                 break;
979         }
980         return(error);
981 }
982