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