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