Merge from vendor branch LIBARCHIVE:
[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.47 2008/06/13 00:25:33 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;
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_debug_cluster_enable = 1;    /* enable read clustering by default */
60 int hammer_count_inodes;
61 int hammer_count_iqueued;
62 int hammer_count_reclaiming;
63 int hammer_count_records;
64 int hammer_count_record_datas;
65 int hammer_count_volumes;
66 int hammer_count_buffers;
67 int hammer_count_nodes;
68 int hammer_count_dirtybufs;             /* global */
69 int hammer_count_refedbufs;             /* global */
70 int hammer_count_reservations;
71 int hammer_count_io_running_read;
72 int hammer_count_io_running_write;
73 int hammer_count_io_locked;
74 int hammer_stats_btree_iterations;
75 int hammer_stats_record_iterations;
76 int hammer_limit_dirtybufs;             /* per-mount */
77 int hammer_limit_irecs;                 /* per-inode */
78 int hammer_limit_recs;                  /* as a whole XXX */
79 int hammer_limit_iqueued;               /* per-mount */
80 int hammer_bio_count;
81 int64_t hammer_contention_count;
82 int64_t hammer_zone_limit;
83
84 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
85 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
86            &hammer_debug_general, 0, "");
87 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
88            &hammer_debug_io, 0, "");
89 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
90            &hammer_debug_debug, 0, "");
91 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
92            &hammer_debug_inode, 0, "");
93 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
94            &hammer_debug_locks, 0, "");
95 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
96            &hammer_debug_btree, 0, "");
97 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
98            &hammer_debug_tid, 0, "");
99 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
100            &hammer_debug_recover, 0, "");
101 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
102            &hammer_debug_recover_faults, 0, "");
103 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_cluster_enable, CTLFLAG_RW,
104            &hammer_debug_cluster_enable, 0, "");
105
106 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufs, CTLFLAG_RW,
107            &hammer_limit_dirtybufs, 0, "");
108 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_irecs, CTLFLAG_RW,
109            &hammer_limit_irecs, 0, "");
110 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
111            &hammer_limit_recs, 0, "");
112 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_iqueued, CTLFLAG_RW,
113            &hammer_limit_iqueued, 0, "");
114
115 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
116            &hammer_count_inodes, 0, "");
117 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
118            &hammer_count_iqueued, 0, "");
119 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaiming, CTLFLAG_RD,
120            &hammer_count_reclaiming, 0, "");
121 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
122            &hammer_count_records, 0, "");
123 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
124            &hammer_count_record_datas, 0, "");
125 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
126            &hammer_count_volumes, 0, "");
127 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
128            &hammer_count_buffers, 0, "");
129 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
130            &hammer_count_nodes, 0, "");
131 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufs, CTLFLAG_RD,
132            &hammer_count_dirtybufs, 0, "");
133 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
134            &hammer_count_refedbufs, 0, "");
135 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
136            &hammer_count_reservations, 0, "");
137 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
138            &hammer_count_io_running_read, 0, "");
139 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
140            &hammer_count_io_locked, 0, "");
141 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
142            &hammer_count_io_running_write, 0, "");
143 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
144            &hammer_zone_limit, 0, "");
145 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
146            &hammer_contention_count, 0, "");
147
148 /*
149  * VFS ABI
150  */
151 static void     hammer_free_hmp(struct mount *mp);
152
153 static int      hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
154                                 struct ucred *cred);
155 static int      hammer_vfs_unmount(struct mount *mp, int mntflags);
156 static int      hammer_vfs_root(struct mount *mp, struct vnode **vpp);
157 static int      hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
158                                 struct ucred *cred);
159 static int      hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
160                                 struct ucred *cred);
161 static int      hammer_vfs_sync(struct mount *mp, int waitfor);
162 static int      hammer_vfs_vget(struct mount *mp, ino_t ino,
163                                 struct vnode **vpp);
164 static int      hammer_vfs_init(struct vfsconf *conf);
165 static int      hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
166                                 struct vnode **vpp);
167 static int      hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
168 static int      hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
169                                 int *exflagsp, struct ucred **credanonp);
170
171
172 static struct vfsops hammer_vfsops = {
173         .vfs_mount      = hammer_vfs_mount,
174         .vfs_unmount    = hammer_vfs_unmount,
175         .vfs_root       = hammer_vfs_root,
176         .vfs_statfs     = hammer_vfs_statfs,
177         .vfs_statvfs    = hammer_vfs_statvfs,
178         .vfs_sync       = hammer_vfs_sync,
179         .vfs_vget       = hammer_vfs_vget,
180         .vfs_init       = hammer_vfs_init,
181         .vfs_vptofh     = hammer_vfs_vptofh,
182         .vfs_fhtovp     = hammer_vfs_fhtovp,
183         .vfs_checkexp   = hammer_vfs_checkexp
184 };
185
186 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
187
188 VFS_SET(hammer_vfsops, hammer, 0);
189 MODULE_VERSION(hammer, 1);
190
191 static int
192 hammer_vfs_init(struct vfsconf *conf)
193 {
194         if (hammer_limit_irecs == 0)
195                 hammer_limit_irecs = nbuf * 8;
196         if (hammer_limit_recs == 0)             /* XXX TODO */
197                 hammer_limit_recs = nbuf * 25;
198         if (hammer_limit_dirtybufs == 0) {
199                 hammer_limit_dirtybufs = hidirtybuffers / 2;
200                 if (hammer_limit_dirtybufs < 100)
201                         hammer_limit_dirtybufs = 100;
202         }
203         if (hammer_limit_iqueued == 0)
204                 hammer_limit_iqueued = desiredvnodes / 5;
205         return(0);
206 }
207
208 static int
209 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
210                  struct ucred *cred)
211 {
212         struct hammer_mount_info info;
213         hammer_mount_t hmp;
214         hammer_volume_t rootvol;
215         struct vnode *rootvp;
216         const char *upath;      /* volume name in userspace */
217         char *path;             /* volume name in system space */
218         int error;
219         int i;
220
221         if ((error = copyin(data, &info, sizeof(info))) != 0)
222                 return (error);
223         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
224                 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
225                         return (EINVAL);
226         }
227
228         /*
229          * Interal mount data structure
230          */
231         if (mp->mnt_flag & MNT_UPDATE) {
232                 hmp = (void *)mp->mnt_data;
233                 KKASSERT(hmp != NULL);
234         } else {
235                 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
236                 mp->mnt_data = (qaddr_t)hmp;
237                 hmp->mp = mp;
238                 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
239                 hmp->namekey_iterator = mycpu->gd_time_seconds;
240                 /*TAILQ_INIT(&hmp->recycle_list);*/
241
242                 hmp->root_btree_beg.localization = HAMMER_MIN_LOCALIZATION;
243                 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
244                 hmp->root_btree_beg.key = -0x8000000000000000LL;
245                 hmp->root_btree_beg.create_tid = 1;
246                 hmp->root_btree_beg.delete_tid = 1;
247                 hmp->root_btree_beg.rec_type = 0;
248                 hmp->root_btree_beg.obj_type = 0;
249
250                 hmp->root_btree_end.localization = HAMMER_MAX_LOCALIZATION;
251                 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
252                 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
253                 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
254                 hmp->root_btree_end.delete_tid = 0;   /* special case */
255                 hmp->root_btree_end.rec_type = 0xFFFFU;
256                 hmp->root_btree_end.obj_type = 0;
257
258                 hmp->sync_lock.refs = 1;
259                 hmp->free_lock.refs = 1;
260                 hmp->undo_lock.refs = 1;
261                 hmp->blkmap_lock.refs = 1;
262
263                 TAILQ_INIT(&hmp->flush_list);
264                 TAILQ_INIT(&hmp->delay_list);
265                 TAILQ_INIT(&hmp->objid_cache_list);
266                 TAILQ_INIT(&hmp->undo_lru_list);
267                 TAILQ_INIT(&hmp->reclaim_list);
268
269                 /*
270                  * Set default zone limits.  This value can be reduced
271                  * further by the zone limit specified in the root volume.
272                  *
273                  * The sysctl can force a small zone limit for debugging
274                  * purposes.
275                  */
276                 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
277                         hmp->zone_limits[i] =
278                                 HAMMER_ZONE_ENCODE(i, HAMMER_ZONE_LIMIT);
279
280                         if (hammer_zone_limit) {
281                                 hmp->zone_limits[i] =
282                                     HAMMER_ZONE_ENCODE(i, hammer_zone_limit);
283                         }
284                         hammer_init_holes(hmp, &hmp->holes[i]);
285                 }
286         }
287         hmp->hflags &= ~HMNT_USERFLAGS;
288         hmp->hflags |= info.hflags & HMNT_USERFLAGS;
289         if (info.asof) {
290                 kprintf("ASOF\n");
291                 mp->mnt_flag |= MNT_RDONLY;
292                 hmp->asof = info.asof;
293         } else {
294                 hmp->asof = HAMMER_MAX_TID;
295         }
296
297         /*
298          * Re-open read-write if originally read-only, or vise-versa.
299          */
300         if (mp->mnt_flag & MNT_UPDATE) {
301                 error = 0;
302                 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
303                         kprintf("HAMMER read-only -> read-write\n");
304                         hmp->ronly = 0;
305                         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
306                                 hammer_adjust_volume_mode, NULL);
307                         rootvol = hammer_get_root_volume(hmp, &error);
308                         if (rootvol) {
309                                 hammer_recover_flush_buffers(hmp, rootvol);
310                                 bcopy(rootvol->ondisk->vol0_blockmap,
311                                       hmp->blockmap,
312                                       sizeof(hmp->blockmap));
313                                 hammer_rel_volume(rootvol, 0);
314                         }
315                         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
316                                 hammer_reload_inode, NULL);
317                         /* kernel clears MNT_RDONLY */
318                 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
319                         kprintf("HAMMER read-write -> read-only\n");
320                         hmp->ronly = 1; /* messy */
321                         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
322                                 hammer_reload_inode, NULL);
323                         hmp->ronly = 0;
324                         hammer_flusher_sync(hmp);
325                         hammer_flusher_sync(hmp);
326                         hammer_flusher_sync(hmp);
327                         hmp->ronly = 1;
328                         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
329                                 hammer_adjust_volume_mode, NULL);
330                 }
331                 return(error);
332         }
333
334         RB_INIT(&hmp->rb_vols_root);
335         RB_INIT(&hmp->rb_inos_root);
336         RB_INIT(&hmp->rb_nods_root);
337         RB_INIT(&hmp->rb_undo_root);
338         RB_INIT(&hmp->rb_resv_root);
339         RB_INIT(&hmp->rb_bufs_root);
340
341         hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
342
343         TAILQ_INIT(&hmp->volu_list);
344         TAILQ_INIT(&hmp->undo_list);
345         TAILQ_INIT(&hmp->data_list);
346         TAILQ_INIT(&hmp->meta_list);
347         TAILQ_INIT(&hmp->lose_list);
348
349         /*
350          * Load volumes
351          */
352         path = objcache_get(namei_oc, M_WAITOK);
353         hmp->nvolumes = info.nvolumes;
354         for (i = 0; i < info.nvolumes; ++i) {
355                 error = copyin(&info.volumes[i], &upath, sizeof(char *));
356                 if (error == 0)
357                         error = copyinstr(upath, path, MAXPATHLEN, NULL);
358                 if (error == 0)
359                         error = hammer_install_volume(hmp, path);
360                 if (error)
361                         break;
362         }
363         objcache_put(namei_oc, path);
364
365         /*
366          * Make sure we found a root volume
367          */
368         if (error == 0 && hmp->rootvol == NULL) {
369                 kprintf("hammer_mount: No root volume found!\n");
370                 error = EINVAL;
371         }
372         if (error) {
373                 hammer_free_hmp(mp);
374                 return (error);
375         }
376
377         /*
378          * No errors, setup enough of the mount point so we can lookup the
379          * root vnode.
380          */
381         mp->mnt_iosize_max = MAXPHYS;
382         mp->mnt_kern_flag |= MNTK_FSMID;
383
384         /* 
385          * note: f_iosize is used by vnode_pager_haspage() when constructing
386          * its VOP_BMAP call.
387          */
388         mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
389         mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
390
391         mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
392         mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
393
394         mp->mnt_maxsymlinklen = 255;
395         mp->mnt_flag |= MNT_LOCAL;
396
397         vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
398         vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
399         vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
400
401         /*
402          * The root volume's ondisk pointer is only valid if we hold a
403          * reference to it.
404          */
405         rootvol = hammer_get_root_volume(hmp, &error);
406         if (error)
407                 goto failed;
408
409         /*
410          * Perform any necessary UNDO operations.  The recovery code does
411          * call hammer_undo_lookup() so we have to pre-cache the blockmap,
412          * and then re-copy it again after recovery is complete.
413          *
414          * If this is a read-only mount the UNDO information is retained
415          * in memory in the form of dirty buffer cache buffers, and not
416          * written back to the media.
417          */
418         bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
419               sizeof(hmp->blockmap));
420
421         error = hammer_recover(hmp, rootvol);
422         if (error) {
423                 kprintf("Failed to recover HAMMER filesystem on mount\n");
424                 goto done;
425         }
426
427         /*
428          * Finish setup now that we have a good root volume
429          */
430         ksnprintf(mp->mnt_stat.f_mntfromname,
431                   sizeof(mp->mnt_stat.f_mntfromname), "%s",
432                   rootvol->ondisk->vol_name);
433         mp->mnt_stat.f_fsid.val[0] =
434                 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
435         mp->mnt_stat.f_fsid.val[1] =
436                 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
437
438         mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
439         mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
440                                      sizeof(mp->mnt_vstat.f_fsid_uuid));
441
442         /*
443          * Certain often-modified fields in the root volume are cached in
444          * the hammer_mount structure so we do not have to generate lots
445          * of little UNDO structures for them.
446          *
447          * Recopy after recovery.  This also has the side effect of
448          * setting our cached undo FIFO's first_offset, which serves to
449          * placemark the FIFO start for the NEXT flush cycle while the
450          * on-disk first_offset represents the LAST flush cycle.
451          */
452         hmp->next_tid = rootvol->ondisk->vol0_next_tid;
453         bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
454               sizeof(hmp->blockmap));
455         hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
456
457         /*
458          * Use the zone limit set by newfs_hammer, or the zone limit set by
459          * sysctl (for debugging), whichever is smaller.
460          */
461         if (rootvol->ondisk->vol0_zone_limit) {
462                 hammer_off_t vol0_zone_limit;
463
464                 vol0_zone_limit = rootvol->ondisk->vol0_zone_limit;
465                 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
466                         if (hmp->zone_limits[i] > vol0_zone_limit)
467                                 hmp->zone_limits[i] = vol0_zone_limit;
468                 }
469         }
470
471         hammer_flusher_create(hmp);
472
473         /*
474          * Locate the root directory using the root cluster's B-Tree as a
475          * starting point.  The root directory uses an obj_id of 1.
476          *
477          * FUTURE: Leave the root directory cached referenced but unlocked
478          * in hmp->rootvp (need to flush it on unmount).
479          */
480         error = hammer_vfs_vget(mp, 1, &rootvp);
481         if (error)
482                 goto done;
483         vput(rootvp);
484         /*vn_unlock(hmp->rootvp);*/
485
486 done:
487         hammer_rel_volume(rootvol, 0);
488 failed:
489         /*
490          * Cleanup and return.
491          */
492         if (error)
493                 hammer_free_hmp(mp);
494         return (error);
495 }
496
497 static int
498 hammer_vfs_unmount(struct mount *mp, int mntflags)
499 {
500 #if 0
501         struct hammer_mount *hmp = (void *)mp->mnt_data;
502 #endif
503         int flags;
504         int error;
505
506         /*
507          * Clean out the vnodes
508          */
509         flags = 0;
510         if (mntflags & MNT_FORCE)
511                 flags |= FORCECLOSE;
512         if ((error = vflush(mp, 0, flags)) != 0)
513                 return (error);
514
515         /*
516          * Clean up the internal mount structure and related entities.  This
517          * may issue I/O.
518          */
519         hammer_free_hmp(mp);
520         return(0);
521 }
522
523 /*
524  * Clean up the internal mount structure and disassociate it from the mount.
525  * This may issue I/O.
526  */
527 static void
528 hammer_free_hmp(struct mount *mp)
529 {
530         struct hammer_mount *hmp = (void *)mp->mnt_data;
531         int i;
532
533 #if 0
534         /*
535          * Clean up the root vnode
536          */
537         if (hmp->rootvp) {
538                 vrele(hmp->rootvp);
539                 hmp->rootvp = NULL;
540         }
541 #endif
542         kprintf("X1");
543         hammer_flusher_sync(hmp);
544         kprintf("X2");
545         hammer_flusher_sync(hmp);
546         kprintf("X3");
547         hammer_flusher_destroy(hmp);
548         kprintf("X4");
549
550         KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
551
552 #if 0
553         /*
554          * Unload & flush inodes
555          *
556          * XXX illegal to call this from here, it can only be done from
557          * the flusher.
558          */
559         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
560                 hammer_unload_inode, (void *)MNT_WAIT);
561
562         /*
563          * Unload & flush volumes
564          */
565 #endif
566         /*
567          * Unload buffers and then volumes
568          */
569         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
570                 hammer_unload_buffer, NULL);
571         kprintf("X5");
572         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
573                 hammer_unload_volume, NULL);
574         kprintf("X6");
575
576         mp->mnt_data = NULL;
577         mp->mnt_flag &= ~MNT_LOCAL;
578         hmp->mp = NULL;
579         kprintf("X7");
580         hammer_destroy_objid_cache(hmp);
581         kprintf("X8");
582         kfree(hmp->zbuf, M_HAMMER);
583         kprintf("X9");
584         kprintf("X10");
585
586         for (i = 0; i < HAMMER_MAX_ZONES; ++i)
587                 hammer_free_holes(hmp, &hmp->holes[i]);
588         kprintf("X11");
589
590         kfree(hmp, M_HAMMER);
591         kprintf("X12");
592 }
593
594 /*
595  * Obtain a vnode for the specified inode number.  An exclusively locked
596  * vnode is returned.
597  */
598 int
599 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
600 {
601         struct hammer_transaction trans;
602         struct hammer_mount *hmp = (void *)mp->mnt_data;
603         struct hammer_inode *ip;
604         int error;
605
606         hammer_simple_transaction(&trans, hmp);
607
608         /*
609          * Lookup the requested HAMMER inode.  The structure must be
610          * left unlocked while we manipulate the related vnode to avoid
611          * a deadlock.
612          */
613         ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error);
614         if (ip == NULL) {
615                 *vpp = NULL;
616                 return(error);
617         }
618         error = hammer_get_vnode(ip, vpp);
619         hammer_rel_inode(ip, 0);
620         hammer_done_transaction(&trans);
621         return (error);
622 }
623
624 /*
625  * Return the root vnode for the filesystem.
626  *
627  * HAMMER stores the root vnode in the hammer_mount structure so
628  * getting it is easy.
629  */
630 static int
631 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
632 {
633 #if 0
634         struct hammer_mount *hmp = (void *)mp->mnt_data;
635 #endif
636         int error;
637
638         error = hammer_vfs_vget(mp, 1, vpp);
639         return (error);
640 }
641
642 static int
643 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
644 {
645         struct hammer_mount *hmp = (void *)mp->mnt_data;
646         hammer_volume_t volume;
647         hammer_volume_ondisk_t ondisk;
648         int error;
649         int64_t bfree;
650
651         volume = hammer_get_root_volume(hmp, &error);
652         if (error)
653                 return(error);
654         ondisk = volume->ondisk;
655
656         /*
657          * Basic stats
658          */
659         mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
660         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
661         hammer_rel_volume(volume, 0);
662
663         mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
664         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
665         if (mp->mnt_stat.f_files < 0)
666                 mp->mnt_stat.f_files = 0;
667
668         *sbp = mp->mnt_stat;
669         return(0);
670 }
671
672 static int
673 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
674 {
675         struct hammer_mount *hmp = (void *)mp->mnt_data;
676         hammer_volume_t volume;
677         hammer_volume_ondisk_t ondisk;
678         int error;
679         int64_t bfree;
680
681         volume = hammer_get_root_volume(hmp, &error);
682         if (error)
683                 return(error);
684         ondisk = volume->ondisk;
685
686         /*
687          * Basic stats
688          */
689         mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
690         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
691         hammer_rel_volume(volume, 0);
692
693         mp->mnt_vstat.f_bfree = bfree / HAMMER_BUFSIZE;
694         mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
695         if (mp->mnt_vstat.f_files < 0)
696                 mp->mnt_vstat.f_files = 0;
697         *sbp = mp->mnt_vstat;
698         return(0);
699 }
700
701 /*
702  * Sync the filesystem.  Currently we have to run it twice, the second
703  * one will advance the undo start index to the end index, so if a crash
704  * occurs no undos will be run on mount.
705  *
706  * We do not sync the filesystem if we are called from a panic.  If we did
707  * we might end up blowing up a sync that was already in progress.
708  */
709 static int
710 hammer_vfs_sync(struct mount *mp, int waitfor)
711 {
712         struct hammer_mount *hmp = (void *)mp->mnt_data;
713         int error;
714
715         if (panicstr == NULL) {
716                 error = hammer_sync_hmp(hmp, waitfor);
717                 if (error == 0)
718                         error = hammer_sync_hmp(hmp, waitfor);
719         } else {
720                 error = EIO;
721         }
722         return (error);
723 }
724
725 /*
726  * Convert a vnode to a file handle.
727  */
728 static int
729 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
730 {
731         hammer_inode_t ip;
732
733         KKASSERT(MAXFIDSZ >= 16);
734         ip = VTOI(vp);
735         fhp->fid_len = offsetof(struct fid, fid_data[16]);
736         fhp->fid_reserved = 0;
737         bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
738         bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
739         return(0);
740 }
741
742
743 /*
744  * Convert a file handle back to a vnode.
745  */
746 static int
747 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
748 {
749         struct hammer_transaction trans;
750         struct hammer_inode *ip;
751         struct hammer_inode_info info;
752         int error;
753
754         bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
755         bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
756
757         hammer_simple_transaction(&trans, (void *)mp->mnt_data);
758
759         /*
760          * Get/allocate the hammer_inode structure.  The structure must be
761          * unlocked while we manipulate the related vnode to avoid a
762          * deadlock.
763          */
764         ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof,
765                               0, &error);
766         if (ip == NULL) {
767                 *vpp = NULL;
768                 return(error);
769         }
770         error = hammer_get_vnode(ip, vpp);
771         hammer_rel_inode(ip, 0);
772         hammer_done_transaction(&trans);
773         return (error);
774 }
775
776 static int
777 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
778                     int *exflagsp, struct ucred **credanonp)
779 {
780         hammer_mount_t hmp = (void *)mp->mnt_data;
781         struct netcred *np;
782         int error;
783
784         np = vfs_export_lookup(mp, &hmp->export, nam);
785         if (np) {
786                 *exflagsp = np->netc_exflags;
787                 *credanonp = &np->netc_anon;
788                 error = 0;
789         } else {
790                 error = EACCES;
791         }
792         return (error);
793
794 }
795
796 int
797 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
798 {
799         hammer_mount_t hmp = (void *)mp->mnt_data;
800         int error;
801
802         switch(op) {
803         case MOUNTCTL_SET_EXPORT:
804                 error = vfs_export(mp, &hmp->export, export);
805                 break;
806         default:
807                 error = EOPNOTSUPP;
808                 break;
809         }
810         return(error);
811 }
812