xref: /freebsd/stand/libsa/mount.c (revision b44cc1b479fefc8570611309c3f5a6966fb26e3b)
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 		DEBUG_PRINTF(1,("%s: fs=%s path=%s\n",
111 			__func__, fs->fs_name, path));
112 		if (is_tftp())
113 			break;
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
unmount(const char * dev,int flags __unused)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