1 /*****************************************************************************
2 
3 Copyright (c) 2013, 2021, Oracle and/or its affiliates.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/fsp0space.h
29 General shared tablespace implementation.
30 
31 Created 2013-7-26 by Kevin Lewis
32 *******************************************************/
33 
34 #ifndef fsp0space_h
35 #define fsp0space_h
36 
37 #include "univ.i"
38 #include "fsp0file.h"
39 #include "fsp0fsp.h"
40 #include "fsp0types.h"
41 #include "ut0new.h"
42 
43 #include <vector>
44 
45 /** Data structure that contains the information about shared tablespaces.
46 Currently this can be the system tablespace or a temporary table tablespace */
47 class Tablespace {
48 
49 public:
50 	typedef std::vector<Datafile, ut_allocator<Datafile> >	files_t;
51 
52 	/** Data file information - each Datafile can be accessed globally */
53 	files_t		m_files;
54 
Tablespace()55 	Tablespace()
56 		:
57 		m_files(),
58 		m_name(),
59 		m_space_id(ULINT_UNDEFINED),
60 		m_path(),
61 		m_flags(),
62 		m_ignore_read_only(false)
63 	{
64 		/* No op */
65 	}
66 
~Tablespace()67 	virtual ~Tablespace()
68 	{
69 		shutdown();
70 		ut_ad(m_files.empty());
71 		ut_ad(m_space_id == ULINT_UNDEFINED);
72 		if (m_name != NULL) {
73 			ut_free(m_name);
74 			m_name = NULL;
75 		}
76 		if (m_path != NULL) {
77 			ut_free(m_path);
78 			m_path = NULL;
79 		}
80 	}
81 
82 	// Disable copying
83 	Tablespace(const Tablespace&);
84 	Tablespace& operator=(const Tablespace&);
85 
86 	/** Set tablespace name
87 	@param[in]	name	tablespace name */
set_name(const char * name)88 	void set_name(const char* name)
89 	{
90 		ut_ad(m_name == NULL);
91 		m_name = mem_strdup(name);
92 		ut_ad(m_name != NULL);
93 	}
94 
95 	/** Get tablespace name
96 	@return tablespace name */
name()97 	const char* name()	const
98 	{
99 		return(m_name);
100 	}
101 
102 	/** Set tablespace path and filename members.
103 	@param[in]	path	where tablespace file(s) resides
104 	@param[in]	len	length of the file path */
set_path(const char * path,size_t len)105 	void set_path(const char* path, size_t len)
106 	{
107 		ut_ad(m_path == NULL);
108 		m_path = mem_strdupl(path, len);
109 		ut_ad(m_path != NULL);
110 
111 		os_normalize_path(m_path);
112 	}
113 
114 	/** Set tablespace path and filename members.
115 	@param[in]	path	where tablespace file(s) resides */
set_path(const char * path)116 	void set_path(const char* path)
117 	{
118 		set_path(path, strlen(path));
119 	}
120 
121 	/** Get tablespace path
122 	@return tablespace path */
path()123 	const char* path()	const
124 	{
125 		return(m_path);
126 	}
127 
128 	/** Set the space id of the tablespace
129 	@param[in]	space_id	 tablespace ID to set */
set_space_id(ulint space_id)130 	void set_space_id(ulint space_id)
131 	{
132 		ut_ad(m_space_id == ULINT_UNDEFINED);
133 		m_space_id = space_id;
134 	}
135 
136 	/** Get the space id of the tablespace
137 	@return m_space_id space id of the tablespace */
space_id()138 	ulint space_id()	const
139 	{
140 		return(m_space_id);
141 	}
142 
143 	/** Set the tablespace flags
144 	@param[in]	fsp_flags	tablespace flags */
set_flags(ulint fsp_flags)145 	void set_flags(ulint fsp_flags)
146 	{
147 		ut_ad(fsp_flags_is_valid(fsp_flags));
148 		m_flags = fsp_flags;
149 	}
150 
151 	/** Get the tablespace flags
152 	@return m_flags tablespace flags */
flags()153 	ulint flags()	const
154 	{
155 		return(m_flags);
156 	}
157 
158 	/** Set Ignore Read Only Status for tablespace.
159 	@param[in]	read_only_status	read only status indicator */
set_ignore_read_only(bool read_only_status)160 	void set_ignore_read_only(bool read_only_status)
161 	{
162 		m_ignore_read_only = read_only_status;
163 	}
164 
165 	/** Free the memory allocated by the Tablespace object */
166 	void shutdown();
167 
168 	/** @return the sum of the file sizes of each Datafile */
get_sum_of_sizes()169 	ulint get_sum_of_sizes() const
170 	{
171 		ulint	sum = 0;
172 
173 		for (files_t::const_iterator it = m_files.begin();
174 		     it != m_files.end(); ++it) {
175 			sum += it->m_size;
176 		}
177 
178 		return(sum);
179 	}
180 
181 	/** Open or Create the data files if they do not exist.
182 	@param[in]	is_temp	whether this is a temporary tablespace
183 	@return DB_SUCCESS or error code */
184 	dberr_t open_or_create(bool is_temp)
185 		MY_ATTRIBUTE((warn_unused_result));
186 
187 	/** Delete all the data files. */
188 	void delete_files();
189 
190 	/** Check if two tablespaces have common data file names.
191 	@param[in]	other_space	Tablespace to check against this.
192 	@return true if they have the same data filenames and paths */
193 	bool intersection(const Tablespace* other_space);
194 
195 	/** Use the ADD DATAFILE path to create a Datafile object and add
196 	it to the front of m_files. Parse the datafile path into a path
197 	and a basename with extension 'ibd'. This datafile_path provided
198 	may be an absolute or relative path, but it must end with the
199 	extension .ibd and have a basename of at least 1 byte.
200 
201 	Set tablespace m_path member and add a Datafile with the filename.
202 	@param[in]	datafile_path	full path of the tablespace file. */
203 	dberr_t add_datafile(
204 		const char*	datafile_path);
205 
206 	/* Return a pointer to the first Datafile for this Tablespace
207 	@return pointer to the first Datafile for this Tablespace*/
first_datafile()208 	Datafile* first_datafile()
209 	{
210 		ut_a(!m_files.empty());
211 		return(&m_files.front());
212 	}
213 
214 	/** Check if undo tablespace.
215 	@return true if undo tablespace */
216 	static bool is_undo_tablespace(ulint id);
217 private:
218 	/**
219 	@param[in]	filename	Name to lookup in the data files.
220 	@return true if the filename exists in the data files */
221 	bool find(const char* filename);
222 
223 	/** Note that the data file was found.
224 	@param[in]	file	data file object */
225 	void file_found(Datafile& file);
226 
227 	/* DATA MEMBERS */
228 
229 	/** Name of the tablespace. */
230 	char*		m_name;
231 
232 	/** Tablespace ID */
233 	ulint		m_space_id;
234 
235 	/** Path where tablespace files will reside, not including a filename.*/
236 	char*		m_path;
237 
238 	/** Tablespace flags */
239 	ulint		m_flags;
240 
241 protected:
242 	/** Ignore server read only configuration for this tablespace. */
243 	bool		m_ignore_read_only;
244 };
245 
246 #endif /* fsp0space_h */
247