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