hammer2 - serialized flush work part 1
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vfsops.c
1 /*-
2  * Copyright (c) 2011, 2012 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 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/nlookup.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/fcntl.h>
41 #include <sys/buf.h>
42 #include <sys/uuid.h>
43 #include <sys/vfsops.h>
44 #include <sys/sysctl.h>
45 #include <sys/socket.h>
46
47 #include "hammer2.h"
48 #include "hammer2_disk.h"
49 #include "hammer2_mount.h"
50
51 struct hammer2_sync_info {
52         int error;
53         int waitfor;
54 };
55
56 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
57 static struct hammer2_mntlist hammer2_mntlist;
58 static struct lock hammer2_mntlk;
59
60 int hammer2_debug;
61 int hammer2_cluster_enable = 1;
62 int hammer2_hardlink_enable = 1;
63 long hammer2_iod_file_read;
64 long hammer2_iod_meta_read;
65 long hammer2_iod_indr_read;
66 long hammer2_iod_file_write;
67 long hammer2_iod_meta_write;
68 long hammer2_iod_indr_write;
69 long hammer2_iod_fmap_write;
70 long hammer2_iod_volu_write;
71 long hammer2_ioa_file_read;
72 long hammer2_ioa_meta_read;
73 long hammer2_ioa_indr_read;
74 long hammer2_ioa_fmap_write;
75 long hammer2_ioa_file_write;
76 long hammer2_ioa_meta_write;
77 long hammer2_ioa_indr_write;
78 long hammer2_ioa_volu_write;
79
80 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
81
82 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
83            &hammer2_debug, 0, "");
84 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
85            &hammer2_cluster_enable, 0, "");
86 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
87            &hammer2_hardlink_enable, 0, "");
88 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
89            &hammer2_iod_file_read, 0, "");
90 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
91            &hammer2_iod_meta_read, 0, "");
92 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
93            &hammer2_iod_indr_read, 0, "");
94 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
95            &hammer2_iod_file_write, 0, "");
96 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
97            &hammer2_iod_meta_write, 0, "");
98 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
99            &hammer2_iod_indr_write, 0, "");
100 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
101            &hammer2_iod_volu_write, 0, "");
102 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
103            &hammer2_ioa_file_read, 0, "");
104 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
105            &hammer2_ioa_meta_read, 0, "");
106 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
107            &hammer2_ioa_indr_read, 0, "");
108 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
109            &hammer2_ioa_file_write, 0, "");
110 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
111            &hammer2_ioa_meta_write, 0, "");
112 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
113            &hammer2_ioa_indr_write, 0, "");
114 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
115            &hammer2_ioa_volu_write, 0, "");
116
117 static int hammer2_vfs_init(struct vfsconf *conf);
118 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
119                                 struct ucred *cred);
120 static int hammer2_remount(struct mount *, char *, struct vnode *,
121                                 struct ucred *);
122 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
123 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
124 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
125                                 struct ucred *cred);
126 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
127                                 struct ucred *cred);
128 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
129 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
130                                 ino_t ino, struct vnode **vpp);
131 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
132                                 struct fid *fhp, struct vnode **vpp);
133 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
134 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
135                                 int *exflagsp, struct ucred **credanonp);
136
137 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
138 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
139 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
140
141 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
142 static void hammer2_autodmsg(kdmsg_msg_t *msg);
143
144 /*
145  * HAMMER2 vfs operations.
146  */
147 static struct vfsops hammer2_vfsops = {
148         .vfs_init       = hammer2_vfs_init,
149         .vfs_sync       = hammer2_vfs_sync,
150         .vfs_mount      = hammer2_vfs_mount,
151         .vfs_unmount    = hammer2_vfs_unmount,
152         .vfs_root       = hammer2_vfs_root,
153         .vfs_statfs     = hammer2_vfs_statfs,
154         .vfs_statvfs    = hammer2_vfs_statvfs,
155         .vfs_vget       = hammer2_vfs_vget,
156         .vfs_vptofh     = hammer2_vfs_vptofh,
157         .vfs_fhtovp     = hammer2_vfs_fhtovp,
158         .vfs_checkexp   = hammer2_vfs_checkexp
159 };
160
161 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
162
163 VFS_SET(hammer2_vfsops, hammer2, 0);
164 MODULE_VERSION(hammer2, 1);
165
166 static
167 int
168 hammer2_vfs_init(struct vfsconf *conf)
169 {
170         int error;
171
172         error = 0;
173
174         if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
175                 error = EINVAL;
176         if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
177                 error = EINVAL;
178         if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
179                 error = EINVAL;
180
181         if (error)
182                 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
183
184         lockinit(&hammer2_mntlk, "mntlk", 0, 0);
185         TAILQ_INIT(&hammer2_mntlist);
186
187         return (error);
188 }
189
190 /*
191  * Mount or remount HAMMER2 fileystem from physical media
192  *
193  *      mountroot
194  *              mp              mount point structure
195  *              path            NULL
196  *              data            <unused>
197  *              cred            <unused>
198  *
199  *      mount
200  *              mp              mount point structure
201  *              path            path to mount point
202  *              data            pointer to argument structure in user space
203  *                      volume  volume path (device@LABEL form)
204  *                      hflags  user mount flags
205  *              cred            user credentials
206  *
207  * RETURNS:     0       Success
208  *              !0      error number
209  */
210 static
211 int
212 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
213                   struct ucred *cred)
214 {
215         struct hammer2_mount_info info;
216         hammer2_pfsmount_t *pmp;
217         hammer2_mount_t *hmp;
218         hammer2_key_t lhc;
219         struct vnode *devvp;
220         struct nlookupdata nd;
221         hammer2_chain_t *parent;
222         hammer2_chain_t *schain;
223         hammer2_chain_t *rchain;
224         struct file *fp;
225         char devstr[MNAMELEN];
226         size_t size;
227         size_t done;
228         char *dev;
229         char *label;
230         int ronly = 1;
231         int create_hmp;
232         int error;
233
234         hmp = NULL;
235         pmp = NULL;
236         dev = NULL;
237         label = NULL;
238         devvp = NULL;
239
240         kprintf("hammer2_mount\n");
241
242         if (path == NULL) {
243                 /*
244                  * Root mount
245                  */
246                 bzero(&info, sizeof(info));
247                 info.cluster_fd = -1;
248                 return (EOPNOTSUPP);
249         } else {
250                 /*
251                  * Non-root mount or updating a mount
252                  */
253                 error = copyin(data, &info, sizeof(info));
254                 if (error)
255                         return (error);
256
257                 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
258                 if (error)
259                         return (error);
260
261                 /* Extract device and label */
262                 dev = devstr;
263                 label = strchr(devstr, '@');
264                 if (label == NULL ||
265                     ((label + 1) - dev) > done) {
266                         return (EINVAL);
267                 }
268                 *label = '\0';
269                 label++;
270                 if (*label == '\0')
271                         return (EINVAL);
272
273                 if (mp->mnt_flag & MNT_UPDATE) {
274                         /* Update mount */
275                         /* HAMMER2 implements NFS export via mountctl */
276                         hmp = MPTOHMP(mp);
277                         devvp = hmp->devvp;
278                         error = hammer2_remount(mp, path, devvp, cred);
279                         return error;
280                 }
281         }
282
283         /*
284          * PFS mount
285          *
286          * Lookup name and verify it refers to a block device.
287          */
288         error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
289         if (error == 0)
290                 error = nlookup(&nd);
291         if (error == 0)
292                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
293         nlookup_done(&nd);
294
295         if (error == 0) {
296                 if (vn_isdisk(devvp, &error))
297                         error = vfs_mountedon(devvp);
298         }
299
300         /*
301          * Determine if the device has already been mounted.  After this
302          * check hmp will be non-NULL if we are doing the second or more
303          * hammer2 mounts from the same device.
304          */
305         lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
306         TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
307                 if (hmp->devvp == devvp)
308                         break;
309         }
310
311         /*
312          * Open the device if this isn't a secondary mount
313          */
314         if (hmp) {
315                 create_hmp = 0;
316         } else {
317                 create_hmp = 1;
318                 if (error == 0 && vcount(devvp) > 0)
319                         error = EBUSY;
320
321                 /*
322                  * Now open the device
323                  */
324                 if (error == 0) {
325                         ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
326                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
327                         error = vinvalbuf(devvp, V_SAVE, 0, 0);
328                         if (error == 0) {
329                                 error = VOP_OPEN(devvp,
330                                                  ronly ? FREAD : FREAD | FWRITE,
331                                                  FSCRED, NULL);
332                         }
333                         vn_unlock(devvp);
334                 }
335                 if (error && devvp) {
336                         vrele(devvp);
337                         devvp = NULL;
338                 }
339                 if (error) {
340                         lockmgr(&hammer2_mntlk, LK_RELEASE);
341                         return error;
342                 }
343         }
344
345         /*
346          * Block device opened successfully, finish initializing the
347          * mount structure.
348          *
349          * From this point on we have to call hammer2_unmount() on failure.
350          */
351         pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
352         mp->mnt_data = (qaddr_t)pmp;
353         pmp->mp = mp;
354
355         kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
356         kdmsg_iocom_init(&pmp->iocom, pmp,
357                          KDMSG_IOCOMF_AUTOCONN |
358                          KDMSG_IOCOMF_AUTOSPAN |
359                          KDMSG_IOCOMF_AUTOCIRC,
360                          pmp->mmsg, hammer2_rcvdmsg);
361
362         if (create_hmp) {
363                 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
364                 hmp->ronly = ronly;
365                 hmp->devvp = devvp;
366                 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
367                 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
368                 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
369         }
370         ccms_domain_init(&pmp->ccms_dom);
371         pmp->hmp = hmp;
372         ++hmp->pmp_count;
373         lockmgr(&hammer2_mntlk, LK_RELEASE);
374         kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
375         
376         mp->mnt_flag = MNT_LOCAL;
377         mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;   /* all entry pts are SMP */
378
379         if (create_hmp) {
380                 /*
381                  * vchain setup. vchain.data is special cased to NULL.
382                  * vchain.refs is initialized and will never drop to 0.
383                  */
384                 hmp->vchain.refs = 1;
385                 hmp->vchain.data = (void *)&hmp->voldata;
386                 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
387                 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
388                 hmp->vchain.bref_flush = hmp->vchain.bref;
389                 ccms_cst_init(&hmp->vchain.cst, NULL);
390                 /* hmp->vchain.u.xxx is left NULL */
391                 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
392                 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
393
394                 /*
395                  * Install the volume header
396                  */
397                 error = hammer2_install_volume_header(hmp);
398                 if (error) {
399                         hammer2_vfs_unmount(mp, MNT_FORCE);
400                         return error;
401                 }
402         }
403
404         /*
405          * required mount structure initializations
406          */
407         mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
408         mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
409
410         mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
411         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
412
413         /*
414          * Optional fields
415          */
416         mp->mnt_iosize_max = MAXPHYS;
417
418         /*
419          * First locate the super-root inode, which is key 0 relative to the
420          * volume header's blockset.
421          *
422          * Then locate the root inode by scanning the directory keyspace
423          * represented by the label.
424          */
425         if (create_hmp) {
426                 parent = &hmp->vchain;
427                 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
428                 schain = hammer2_chain_lookup(hmp, &parent,
429                                       HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
430                 hammer2_chain_unlock(hmp, parent);
431                 if (schain == NULL) {
432                         kprintf("hammer2_mount: invalid super-root\n");
433                         hammer2_vfs_unmount(mp, MNT_FORCE);
434                         return EINVAL;
435                 }
436                 hammer2_chain_ref(hmp, schain); /* for hmp->schain */
437                 hmp->schain = schain;           /* left locked */
438         } else {
439                 schain = hmp->schain;
440                 hammer2_chain_lock(hmp, schain, HAMMER2_RESOLVE_ALWAYS);
441         }
442
443         parent = schain;
444         lhc = hammer2_dirhash(label, strlen(label));
445         rchain = hammer2_chain_lookup(hmp, &parent,
446                                       lhc, lhc + HAMMER2_DIRHASH_LOMASK,
447                                       0);
448         while (rchain) {
449                 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
450                     strcmp(label, rchain->data->ipdata.filename) == 0) {
451                         break;
452                 }
453                 rchain = hammer2_chain_next(hmp, &parent, rchain,
454                                             lhc, lhc + HAMMER2_DIRHASH_LOMASK,
455                                             0);
456         }
457         hammer2_chain_unlock(hmp, parent);
458         if (rchain == NULL) {
459                 kprintf("hammer2_mount: PFS label not found\n");
460                 hammer2_vfs_unmount(mp, MNT_FORCE);
461                 return EINVAL;
462         }
463         if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
464                 hammer2_chain_unlock(hmp, rchain);
465                 kprintf("hammer2_mount: PFS label already mounted!\n");
466                 hammer2_vfs_unmount(mp, MNT_FORCE);
467                 return EBUSY;
468         }
469         atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
470
471         /*
472          * NOTE: *_get() integrates chain's lock into the inode lock.
473          */
474         hammer2_chain_ref(hmp, rchain);         /* for pmp->rchain */
475         pmp->rchain = rchain;                   /* left held & unlocked */
476         pmp->iroot = hammer2_inode_get(pmp, NULL, rchain);
477         hammer2_inode_ref(pmp->iroot);          /* ref for pmp->iroot */
478         hammer2_inode_unlock_ex(pmp->iroot);    /* iroot & its chain */
479
480         kprintf("iroot %p\n", pmp->iroot);
481
482         /*
483          * Ref the cluster management messaging descriptor.  The mount
484          * program deals with the other end of the communications pipe.
485          */
486         fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
487         if (fp == NULL) {
488                 kprintf("hammer2_mount: bad cluster_fd!\n");
489                 hammer2_vfs_unmount(mp, MNT_FORCE);
490                 return EBADF;
491         }
492         hammer2_cluster_reconnect(pmp, fp);
493
494         /*
495          * Finish setup
496          */
497         vfs_getnewfsid(mp);
498         vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
499         vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
500         vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
501
502         copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
503         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
504         bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
505         copyinstr(path, mp->mnt_stat.f_mntonname,
506                   sizeof(mp->mnt_stat.f_mntonname) - 1,
507                   &size);
508
509         /*
510          * Initial statfs to prime mnt_stat.
511          */
512         hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
513
514         return 0;
515 }
516
517 static
518 int
519 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
520                 struct ucred *cred)
521 {
522         return (0);
523 }
524
525 static
526 int
527 hammer2_vfs_unmount(struct mount *mp, int mntflags)
528 {
529         hammer2_pfsmount_t *pmp;
530         hammer2_mount_t *hmp;
531         int flags;
532         int error = 0;
533         int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
534         struct vnode *devvp;
535
536         pmp = MPTOPMP(mp);
537         hmp = pmp->hmp;
538         flags = 0;
539
540         if (mntflags & MNT_FORCE)
541                 flags |= FORCECLOSE;
542
543         hammer2_mount_exlock(hmp);
544
545         /*
546          * If mount initialization proceeded far enough we must flush
547          * its vnodes.
548          */
549         if (pmp->iroot)
550                 error = vflush(mp, 0, flags);
551
552         if (error)
553                 return error;
554
555         lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
556         --hmp->pmp_count;
557         kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
558
559         /*
560          * Flush any left over chains.  The voldata lock is only used
561          * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
562          */
563         hammer2_voldata_lock(hmp);
564         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
565                                  HAMMER2_CHAIN_MODIFIED_AUX |
566                                  HAMMER2_CHAIN_SUBMODIFIED)) {
567                 hammer2_voldata_unlock(hmp);
568                 hammer2_vfs_sync(mp, MNT_WAIT);
569         } else {
570                 hammer2_voldata_unlock(hmp);
571         }
572         if (hmp->pmp_count == 0) {
573                 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
574                                          HAMMER2_CHAIN_MODIFIED_AUX |
575                                          HAMMER2_CHAIN_SUBMODIFIED)) {
576                         kprintf("hammer2_unmount: chains left over after "
577                                 "final sync\n");
578                         if (hammer2_debug & 0x0010)
579                                 Debugger("entered debugger");
580                 }
581         }
582
583         /*
584          * Cleanup the root and super-root chain elements (which should be
585          * clean).
586          */
587         if (pmp->iroot) {
588                 hammer2_inode_lock_ex(pmp->iroot);
589                 hammer2_inode_put(pmp->iroot);
590                 /* lock destroyed by the put */
591                 KKASSERT(pmp->iroot->refs == 1);
592                 hammer2_inode_drop(pmp->iroot);
593                 pmp->iroot = NULL;
594         }
595         if (pmp->rchain) {
596                 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
597                 KKASSERT(pmp->rchain->refs == 1);
598                 hammer2_chain_drop(hmp, pmp->rchain);
599                 pmp->rchain = NULL;
600         }
601         ccms_domain_uninit(&pmp->ccms_dom);
602
603         /*
604          * Kill cluster controller
605          */
606         kdmsg_iocom_uninit(&pmp->iocom);
607
608         /*
609          * If no PFS's left drop the master hammer2_mount for the device.
610          */
611         if (hmp->pmp_count == 0) {
612                 if (hmp->schain) {
613                         KKASSERT(hmp->schain->refs == 1);
614                         hammer2_chain_drop(hmp, hmp->schain);
615                         hmp->schain = NULL;
616                 }
617
618                 /*
619                  * Finish up with the device vnode
620                  */
621                 if ((devvp = hmp->devvp) != NULL) {
622                         vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
623                         hmp->devvp = NULL;
624                         VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
625                         vrele(devvp);
626                         devvp = NULL;
627                 }
628         }
629         hammer2_mount_unlock(hmp);
630
631         pmp->mp = NULL;
632         pmp->hmp = NULL;
633         mp->mnt_data = NULL;
634
635         kmalloc_destroy(&pmp->mmsg);
636
637         kfree(pmp, M_HAMMER2);
638         if (hmp->pmp_count == 0) {
639                 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
640                 kmalloc_destroy(&hmp->minode);
641                 kmalloc_destroy(&hmp->mchain);
642                 kfree(hmp, M_HAMMER2);
643         }
644         lockmgr(&hammer2_mntlk, LK_RELEASE);
645         return (error);
646 }
647
648 static
649 int
650 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
651              ino_t ino, struct vnode **vpp)
652 {
653         kprintf("hammer2_vget\n");
654         return (EOPNOTSUPP);
655 }
656
657 static
658 int
659 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
660 {
661         hammer2_pfsmount_t *pmp;
662         hammer2_mount_t *hmp;
663         int error;
664         struct vnode *vp;
665
666         pmp = MPTOPMP(mp);
667         hmp = pmp->hmp;
668         hammer2_mount_exlock(hmp);
669         if (pmp->iroot == NULL) {
670                 *vpp = NULL;
671                 error = EINVAL;
672         } else {
673                 hammer2_inode_lock_sh(pmp->iroot);
674                 vp = hammer2_igetv(pmp->iroot, &error);
675                 hammer2_inode_unlock_sh(pmp->iroot);
676                 *vpp = vp;
677                 if (vp == NULL)
678                         kprintf("vnodefail\n");
679         }
680         hammer2_mount_unlock(hmp);
681
682         return (error);
683 }
684
685 /*
686  * Filesystem status
687  *
688  * XXX incorporate ipdata->inode_quota and data_quota
689  */
690 static
691 int
692 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
693 {
694         hammer2_pfsmount_t *pmp;
695         hammer2_mount_t *hmp;
696
697         pmp = MPTOPMP(mp);
698         hmp = MPTOHMP(mp);
699
700         mp->mnt_stat.f_files = pmp->inode_count;
701         mp->mnt_stat.f_ffree = 0;
702         mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
703         mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
704                                 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
705         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
706
707         *sbp = mp->mnt_stat;
708         return (0);
709 }
710
711 static
712 int
713 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
714 {
715         hammer2_pfsmount_t *pmp;
716         hammer2_mount_t *hmp;
717
718         pmp = MPTOPMP(mp);
719         hmp = MPTOHMP(mp);
720
721         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
722         mp->mnt_vstat.f_files = pmp->inode_count;
723         mp->mnt_vstat.f_ffree = 0;
724         mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
725         mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
726                                  hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
727         mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
728
729         *sbp = mp->mnt_vstat;
730         return (0);
731 }
732
733 /*
734  * Sync the entire filesystem; this is called from the filesystem syncer
735  * process periodically and whenever a user calls sync(1) on the hammer
736  * mountpoint.
737  *
738  * Currently is actually called from the syncer! \o/
739  *
740  * This task will have to snapshot the state of the dirty inode chain.
741  * From that, it will have to make sure all of the inodes on the dirty
742  * chain have IO initiated. We make sure that io is initiated for the root
743  * block.
744  *
745  * If waitfor is set, we wait for media to acknowledge the new rootblock.
746  *
747  * THINKS: side A vs side B, to have sync not stall all I/O?
748  */
749 static
750 int
751 hammer2_vfs_sync(struct mount *mp, int waitfor)
752 {
753         struct hammer2_sync_info info;
754         hammer2_mount_t *hmp;
755         int flags;
756         int error;
757         int haswork;
758         int i;
759
760         hmp = MPTOHMP(mp);
761
762         flags = VMSC_GETVP;
763         if (waitfor & MNT_LAZY)
764                 flags |= VMSC_ONEPASS;
765
766         info.error = 0;
767         info.waitfor = MNT_NOWAIT;
768         vmntvnodescan(mp, flags | VMSC_NOWAIT,
769                       hammer2_sync_scan1,
770                       hammer2_sync_scan2, &info);
771         if (info.error == 0 && (waitfor & MNT_WAIT)) {
772                 info.waitfor = waitfor;
773                     vmntvnodescan(mp, flags,
774                                   hammer2_sync_scan1,
775                                   hammer2_sync_scan2, &info);
776
777         }
778 #if 0
779         if (waitfor == MNT_WAIT) {
780                 /* XXX */
781         } else {
782                 /* XXX */
783         }
784 #endif
785         hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
786         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
787                                  HAMMER2_CHAIN_MODIFIED_AUX |
788                                  HAMMER2_CHAIN_SUBMODIFIED)) {
789                 hammer2_chain_flush(hmp, &hmp->vchain, 0);
790                 haswork = 1;
791         } else {
792                 haswork = 0;
793         }
794         hammer2_chain_unlock(hmp, &hmp->vchain);
795
796         error = 0;
797
798         /*
799          * We can't safely flush the volume header until we have
800          * flushed any device buffers which have built up.
801          */
802 #if 0
803         if ((waitfor & MNT_LAZY) == 0) {
804                 waitfor = MNT_NOWAIT;
805                 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
806                 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
807                 vn_unlock(hmp->devvp);
808         }
809 #endif
810         vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
811         error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
812         vn_unlock(hmp->devvp);
813
814         if (error == 0 && haswork) {
815                 struct buf *bp;
816
817                 /*
818                  * Synchronize the disk before flushing the volume
819                  * header.
820                  */
821                 bp = getpbuf(NULL);
822                 bp->b_bio1.bio_offset = 0;
823                 bp->b_bufsize = 0;
824                 bp->b_bcount = 0;
825                 bp->b_cmd = BUF_CMD_FLUSH;
826                 bp->b_bio1.bio_done = biodone_sync;
827                 bp->b_bio1.bio_flags |= BIO_SYNC;
828                 vn_strategy(hmp->devvp, &bp->b_bio1);
829                 biowait(&bp->b_bio1, "h2vol");
830                 relpbuf(bp, NULL);
831
832                 /*
833                  * Then we can safely flush the version of the volume header
834                  * synchronized by the flush code.
835                  */
836                 i = hmp->volhdrno + 1;
837                 if (i >= HAMMER2_NUM_VOLHDRS)
838                         i = 0;
839                 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
840                     hmp->volsync.volu_size) {
841                         i = 0;
842                 }
843                 kprintf("sync volhdr %d %jd\n",
844                         i, (intmax_t)hmp->volsync.volu_size);
845                 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
846                             HAMMER2_PBUFSIZE, 0, 0);
847                 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
848                 bawrite(bp);
849                 hmp->volhdrno = i;
850         }
851         return (error);
852 }
853
854 /*
855  * Sync passes.
856  *
857  * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
858  *       won't flush on those flags.  The syncer code above will do a
859  *       general meta-data flush globally that will catch these flags.
860  */
861 static int
862 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
863 {
864         hammer2_inode_t *ip;
865
866         ip = VTOI(vp);
867         if (vp->v_type == VNON || ip == NULL ||
868             ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
869              RB_EMPTY(&vp->v_rbdirty_tree))) {
870                 return(-1);
871         }
872         return(0);
873 }
874
875 static int
876 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
877 {
878         struct hammer2_sync_info *info = data;
879         hammer2_inode_t *ip;
880         int error;
881
882         ip = VTOI(vp);
883         if (vp->v_type == VNON || vp->v_type == VBAD ||
884             ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
885              RB_EMPTY(&vp->v_rbdirty_tree))) {
886                 return(0);
887         }
888         error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
889         if (error)
890                 info->error = error;
891         return(0);
892 }
893
894 static
895 int
896 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
897 {
898         return (0);
899 }
900
901 static
902 int
903 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
904                struct fid *fhp, struct vnode **vpp)
905 {
906         return (0);
907 }
908
909 static
910 int
911 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
912                  int *exflagsp, struct ucred **credanonp)
913 {
914         return (0);
915 }
916
917 /*
918  * Support code for hammer2_mount().  Read, verify, and install the volume
919  * header into the HMP
920  *
921  * XXX read four volhdrs and use the one with the highest TID whos CRC
922  *     matches.
923  *
924  * XXX check iCRCs.
925  *
926  * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
927  *     nonexistant locations.
928  *
929  * XXX Record selected volhdr and ring updates to each of 4 volhdrs
930  */
931 static
932 int
933 hammer2_install_volume_header(hammer2_mount_t *hmp)
934 {
935         hammer2_volume_data_t *vd;
936         struct buf *bp;
937         hammer2_crc32_t crc0, crc, bcrc0, bcrc;
938         int error_reported;
939         int error;
940         int valid;
941         int i;
942
943         error_reported = 0;
944         error = 0;
945         valid = 0;
946         bp = NULL;
947
948         /*
949          * There are up to 4 copies of the volume header (syncs iterate
950          * between them so there is no single master).  We don't trust the
951          * volu_size field so we don't know precisely how large the filesystem
952          * is, so depend on the OS to return an error if we go beyond the
953          * block device's EOF.
954          */
955         for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
956                 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
957                               HAMMER2_VOLUME_BYTES, &bp);
958                 if (error) {
959                         brelse(bp);
960                         bp = NULL;
961                         continue;
962                 }
963
964                 vd = (struct hammer2_volume_data *) bp->b_data;
965                 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
966                     (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
967                         brelse(bp);
968                         bp = NULL;
969                         continue;
970                 }
971
972                 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
973                         /* XXX: Reversed-endianness filesystem */
974                         kprintf("hammer2: reverse-endian filesystem detected");
975                         brelse(bp);
976                         bp = NULL;
977                         continue;
978                 }
979
980                 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
981                 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
982                                       HAMMER2_VOLUME_ICRC0_SIZE);
983                 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
984                 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
985                                        HAMMER2_VOLUME_ICRC1_SIZE);
986                 if ((crc0 != crc) || (bcrc0 != bcrc)) {
987                         kprintf("hammer2 volume header crc "
988                                 "mismatch copy #%d %08x/%08x\n",
989                                 i, crc0, crc);
990                         error_reported = 1;
991                         brelse(bp);
992                         bp = NULL;
993                         continue;
994                 }
995                 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
996                         valid = 1;
997                         hmp->voldata = *vd;
998                         hmp->volhdrno = i;
999                 }
1000                 brelse(bp);
1001                 bp = NULL;
1002         }
1003         if (valid) {
1004                 hmp->volsync = hmp->voldata;
1005                 error = 0;
1006                 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1007                         kprintf("hammer2: using volume header #%d\n",
1008                                 hmp->volhdrno);
1009                 }
1010         } else {
1011                 error = EINVAL;
1012                 kprintf("hammer2: no valid volume headers found!\n");
1013         }
1014         return (error);
1015 }
1016
1017 /*
1018  * Reconnect using the passed file pointer.  The caller must ref the
1019  * fp for us.
1020  */
1021 void
1022 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1023 {
1024         hammer2_inode_data_t *ipdata;
1025         size_t name_len;
1026
1027         /*
1028          * Closes old comm descriptor, kills threads, cleans up
1029          * states, then installs the new descriptor and creates
1030          * new threads.
1031          */
1032         kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
1033
1034         /*
1035          * Setup LNK_CONN fields for autoinitiated state machine
1036          */
1037         hammer2_inode_lock_ex(pmp->iroot);
1038         ipdata = &pmp->iroot->chain->data->ipdata;
1039         pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
1040         pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
1041         pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
1042         pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
1043         pmp->iocom.auto_lnk_conn.peer_type = pmp->hmp->voldata.peer_type;
1044         hammer2_inode_unlock_ex(pmp->iroot);
1045
1046         /*
1047          * Filter adjustment.  Clients do not need visibility into other
1048          * clients (otherwise millions of clients would present a serious
1049          * problem).  The fs_label also serves to restrict the namespace.
1050          */
1051         pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
1052         pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
1053         switch (ipdata->pfs_type) {
1054         case DMSG_PFSTYPE_CLIENT:
1055                 pmp->iocom.auto_lnk_conn.peer_mask &=
1056                                 ~(1LLU << DMSG_PFSTYPE_CLIENT);
1057                 break;
1058         default:
1059                 break;
1060         }
1061
1062         name_len = ipdata->name_len;
1063         if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
1064                 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
1065         bcopy(ipdata->filename,
1066               pmp->iocom.auto_lnk_conn.fs_label,
1067               name_len);
1068         pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
1069
1070         /*
1071          * Setup LNK_SPAN fields for autoinitiated state machine
1072          */
1073         pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
1074         pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
1075         pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
1076         pmp->iocom.auto_lnk_span.peer_type = pmp->hmp->voldata.peer_type;
1077         pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
1078         name_len = ipdata->name_len;
1079         if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
1080                 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
1081         bcopy(ipdata->filename,
1082               pmp->iocom.auto_lnk_span.fs_label,
1083               name_len);
1084         pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
1085
1086         kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
1087 }
1088
1089 static int
1090 hammer2_rcvdmsg(kdmsg_msg_t *msg)
1091 {
1092         switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
1093         case DMSG_DBG_SHELL:
1094                 /*
1095                  * (non-transaction)
1096                  * Execute shell command (not supported atm)
1097                  */
1098                 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1099                 break;
1100         case DMSG_DBG_SHELL | DMSGF_REPLY:
1101                 /*
1102                  * (non-transaction)
1103                  */
1104                 if (msg->aux_data) {
1105                         msg->aux_data[msg->aux_size - 1] = 0;
1106                         kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
1107                 }
1108                 break;
1109         default:
1110                 /*
1111                  * Unsupported message received.  We only need to
1112                  * reply if it's a transaction in order to close our end.
1113                  * Ignore any one-way messages are any further messages
1114                  * associated with the transaction.
1115                  *
1116                  * NOTE: This case also includes DMSG_LNK_ERROR messages
1117                  *       which might be one-way, replying to those would
1118                  *       cause an infinite ping-pong.
1119                  */
1120                 if (msg->any.head.cmd & DMSGF_CREATE)
1121                         kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1122                 break;
1123         }
1124         return(0);
1125 }
1126
1127 /*
1128  * This function is called after KDMSG has automatically handled processing
1129  * of a LNK layer message (typically CONN, SPAN, or CIRC).
1130  *
1131  * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
1132  * advertises all available hammer2 super-root volumes.
1133  */
1134 static void
1135 hammer2_autodmsg(kdmsg_msg_t *msg)
1136 {
1137         hammer2_pfsmount_t *pmp = msg->iocom->handle;
1138         hammer2_mount_t *hmp = pmp->hmp;
1139         int copyid;
1140
1141         /*
1142          * We only care about replies to our LNK_CONN auto-request.  kdmsg
1143          * has already processed the reply, we use this calback as a shim
1144          * to know when we can advertise available super-root volumes.
1145          */
1146         if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
1147             (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
1148             msg->state == NULL) {
1149                 return;
1150         }
1151
1152         kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
1153
1154         if (msg->any.head.cmd & DMSGF_CREATE) {
1155                 kprintf("HAMMER2: VOLDATA DUMP\n");
1156
1157                 /*
1158                  * Dump the configuration stored in the volume header
1159                  */
1160                 hammer2_voldata_lock(hmp);
1161                 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
1162                         if (hmp->voldata.copyinfo[copyid].copyid == 0)
1163                                 continue;
1164                         hammer2_volconf_update(pmp, copyid);
1165                 }
1166                 hammer2_voldata_unlock(hmp);
1167         }
1168         if ((msg->any.head.cmd & DMSGF_DELETE) &&
1169             msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
1170                 kprintf("HAMMER2: CONN WAS TERMINATED\n");
1171         }
1172 }
1173
1174 /*
1175  * Volume configuration updates are passed onto the userland service
1176  * daemon via the open LNK_CONN transaction.
1177  */
1178 void
1179 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
1180 {
1181         hammer2_mount_t *hmp = pmp->hmp;
1182         kdmsg_msg_t *msg;
1183
1184         /* XXX interlock against connection state termination */
1185         kprintf("volconf update %p\n", pmp->iocom.conn_state);
1186         if (pmp->iocom.conn_state) {
1187                 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
1188                 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
1189                                             DMSG_LNK_VOLCONF, NULL, NULL);
1190                 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
1191                 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
1192                 msg->any.lnk_volconf.index = index;
1193                 kdmsg_msg_write(msg);
1194         }
1195 }