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