xref: /freebsd/stand/libsa/mount.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*-
2  * Copyright 2021 Toomas Soome <tsoome@me.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <stand.h>
30 #include <sys/queue.h>
31 
32 /*
33  * While setting "currdev" environment variable, alse "mount" the
34  * new root file system. This is done to hold disk device open
35  * in between file accesses, and thus preserve block cache for
36  * this device. Additionally, this allows us to optimize filesystem
37  * access by sharing filesystem metadata (like superblock).
38  */
39 
40 typedef STAILQ_HEAD(mnt_info_list, mnt_info) mnt_info_list_t;
41 
42 typedef struct mnt_info {
43 	STAILQ_ENTRY(mnt_info)	mnt_link;	/* link in mount list */
44 	const struct fs_ops	*mnt_fs;
45 	char			*mnt_dev;
46 	char			*mnt_path;
47 	unsigned		mnt_refcount;
48 	void			*mnt_data;	/* Private state */
49 } mnt_info_t;
50 
51 /* list of mounted filesystems. */
52 static mnt_info_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
53 
54 static void
55 free_mnt(mnt_info_t *mnt)
56 {
57 	free(mnt->mnt_dev);
58 	free(mnt->mnt_path);
59 	free(mnt);
60 }
61 
62 static int
63 add_mnt_info(struct fs_ops *fs, const char *dev, const char *path, void *data)
64 {
65 	mnt_info_t *mnt;
66 
67 	mnt = malloc(sizeof(*mnt));
68 	if (mnt == NULL)
69 		return (ENOMEM);
70 
71 	mnt->mnt_fs = fs;
72 	mnt->mnt_dev = strdup(dev);
73 	mnt->mnt_path = strdup(path);
74 	mnt->mnt_data = data;
75 	mnt->mnt_refcount = 1;
76 
77 	if (mnt->mnt_dev == NULL || mnt->mnt_path == NULL) {
78 		free_mnt(mnt);
79 		return (ENOMEM);
80 	}
81 	STAILQ_INSERT_TAIL(&mnt_list, mnt, mnt_link);
82 	return (0);
83 }
84 
85 static void
86 delete_mnt_info(mnt_info_t *mnt)
87 {
88 	STAILQ_REMOVE(&mnt_list, mnt, mnt_info, mnt_link);
89 	free_mnt(mnt);
90 }
91 
92 int
93 mount(const char *dev, const char *path, int flags __unused, void *data)
94 {
95 	mnt_info_t *mnt;
96 	int rc = -1;
97 
98 	/* Is it already mounted? */
99 	STAILQ_FOREACH(mnt, &mnt_list, mnt_link) {
100 		if (strcmp(dev, mnt->mnt_dev) == 0 &&
101 		    strcmp(path, mnt->mnt_path) == 0) {
102 			mnt->mnt_refcount++;
103 			return (0);
104 		}
105 	}
106 
107 	for (int i = 0; file_system[i] != NULL; i++) {
108 		struct fs_ops *fs;
109 
110 		fs = file_system[i];
111 		if (fs->fo_mount == NULL)
112 			continue;
113 
114 		if (fs->fo_mount(dev, path, &data) != 0)
115 			continue;
116 
117 		rc = add_mnt_info(fs, dev, path, data);
118 		if (rc != 0 && mnt->mnt_fs->fo_unmount != NULL) {
119 			printf("failed to mount %s: %s\n", dev,
120 			    strerror(rc));
121 			(void)mnt->mnt_fs->fo_unmount(dev, data);
122 		}
123 		break;
124 	}
125 
126 
127 	/*
128 	 * if rc is -1, it means we have no file system with fo_mount()
129 	 * callback, or all fo_mount() calls failed. As long as we
130 	 * have missing fo_mount() callbacks, we allow mount() to return 0.
131 	 */
132 	if (rc == -1)
133 		rc = 0;
134 
135 	return (rc);
136 }
137 
138 int
139 unmount(const char *dev, int flags __unused)
140 {
141 	mnt_info_t *mnt;
142 	int rv;
143 
144 	rv = 0;
145 	STAILQ_FOREACH(mnt, &mnt_list, mnt_link) {
146 		if (strcmp(dev, mnt->mnt_dev) == 0) {
147 			if (mnt->mnt_refcount > 1) {
148 				mnt->mnt_refcount--;
149 				break;
150 			}
151 
152 			if (mnt->mnt_fs->fo_unmount != NULL)
153 				rv = mnt->mnt_fs->fo_unmount(dev,
154 				    mnt->mnt_data);
155 			delete_mnt_info(mnt);
156 			break;
157 		}
158 	}
159 
160 	if (rv != 0)
161 		printf("failed to unmount %s: %d\n", dev, rv);
162 	return (0);
163 }
164