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