xref: /freebsd/stand/libsa/mount.c (revision 3e15b01d6914c927e37d1699645783acf286655c)
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 <stand.h>
27 #include <sys/queue.h>
28 
29 /*
30  * While setting "currdev" environment variable, alse "mount" the
31  * new root file system. This is done to hold disk device open
32  * in between file accesses, and thus preserve block cache for
33  * this device. Additionally, this allows us to optimize filesystem
34  * access by sharing filesystem metadata (like superblock).
35  */
36 
37 typedef STAILQ_HEAD(mnt_info_list, mnt_info) mnt_info_list_t;
38 
39 typedef struct mnt_info {
40 	STAILQ_ENTRY(mnt_info)	mnt_link;	/* link in mount list */
41 	const struct fs_ops	*mnt_fs;
42 	char			*mnt_dev;
43 	char			*mnt_path;
44 	unsigned		mnt_refcount;
45 	void			*mnt_data;	/* Private state */
46 } mnt_info_t;
47 
48 /* list of mounted filesystems. */
49 static mnt_info_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
50 
51 static void
free_mnt(mnt_info_t * mnt)52 free_mnt(mnt_info_t *mnt)
53 {
54 	free(mnt->mnt_dev);
55 	free(mnt->mnt_path);
56 	free(mnt);
57 }
58 
59 static int
add_mnt_info(struct fs_ops * fs,const char * dev,const char * path,void * data)60 add_mnt_info(struct fs_ops *fs, const char *dev, const char *path, void *data)
61 {
62 	mnt_info_t *mnt;
63 
64 	mnt = malloc(sizeof(*mnt));
65 	if (mnt == NULL)
66 		return (ENOMEM);
67 
68 	mnt->mnt_fs = fs;
69 	mnt->mnt_dev = strdup(dev);
70 	mnt->mnt_path = strdup(path);
71 	mnt->mnt_data = data;
72 	mnt->mnt_refcount = 1;
73 
74 	if (mnt->mnt_dev == NULL || mnt->mnt_path == NULL) {
75 		free_mnt(mnt);
76 		return (ENOMEM);
77 	}
78 	STAILQ_INSERT_TAIL(&mnt_list, mnt, mnt_link);
79 	return (0);
80 }
81 
82 static void
delete_mnt_info(mnt_info_t * mnt)83 delete_mnt_info(mnt_info_t *mnt)
84 {
85 	STAILQ_REMOVE(&mnt_list, mnt, mnt_info, mnt_link);
86 	free_mnt(mnt);
87 }
88 
89 int
mount(const char * dev,const char * path,int flags __unused,void * data)90 mount(const char *dev, const char *path, int flags __unused, void *data)
91 {
92 	mnt_info_t *mnt;
93 	int rc = -1;
94 
95 	/* Is it already mounted? */
96 	STAILQ_FOREACH(mnt, &mnt_list, mnt_link) {
97 		if (strcmp(dev, mnt->mnt_dev) == 0 &&
98 		    strcmp(path, mnt->mnt_path) == 0) {
99 			mnt->mnt_refcount++;
100 			return (0);
101 		}
102 	}
103 
104 	for (int i = 0; file_system[i] != NULL; i++) {
105 		struct fs_ops *fs;
106 
107 		fs = file_system[i];
108 		if (fs->fo_mount == NULL)
109 			continue;
110 
111 		if (fs->fo_mount(dev, path, &data) != 0)
112 			continue;
113 
114 		rc = add_mnt_info(fs, dev, path, data);
115 		if (rc != 0 && mnt->mnt_fs->fo_unmount != NULL) {
116 			printf("failed to mount %s: %s\n", dev,
117 			    strerror(rc));
118 			(void)mnt->mnt_fs->fo_unmount(dev, data);
119 		}
120 		break;
121 	}
122 
123 
124 	/*
125 	 * if rc is -1, it means we have no file system with fo_mount()
126 	 * callback, or all fo_mount() calls failed. As long as we
127 	 * have missing fo_mount() callbacks, we allow mount() to return 0.
128 	 */
129 	if (rc == -1)
130 		rc = 0;
131 
132 	return (rc);
133 }
134 
135 int
unmount(const char * dev,int flags __unused)136 unmount(const char *dev, int flags __unused)
137 {
138 	mnt_info_t *mnt;
139 	int rv;
140 
141 	rv = 0;
142 	STAILQ_FOREACH(mnt, &mnt_list, mnt_link) {
143 		if (strcmp(dev, mnt->mnt_dev) == 0) {
144 			if (mnt->mnt_refcount > 1) {
145 				mnt->mnt_refcount--;
146 				break;
147 			}
148 
149 			if (mnt->mnt_fs->fo_unmount != NULL)
150 				rv = mnt->mnt_fs->fo_unmount(dev,
151 				    mnt->mnt_data);
152 			delete_mnt_info(mnt);
153 			break;
154 		}
155 	}
156 
157 	if (rv != 0)
158 		printf("failed to unmount %s: %d\n", dev, rv);
159 	return (0);
160 }
161