]> git.proxmox.com Git - ceph.git/blob - ceph/src/libradosstriper/RadosStriperImpl.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / libradosstriper / RadosStriperImpl.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2014 Sebastien Ponce <sebastien.ponce@cern.ch>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef CEPH_LIBRADOSSTRIPER_RADOSSTRIPERIMPL_H
16 #define CEPH_LIBRADOSSTRIPER_RADOSSTRIPERIMPL_H
17
18 #include <string>
19
20 #include "include/rados/librados.h"
21 #include "include/rados/librados.hpp"
22 #include "include/radosstriper/libradosstriper.h"
23 #include "include/radosstriper/libradosstriper.hpp"
24 #include "MultiAioCompletionImpl.h"
25
26 #include "librados/IoCtxImpl.h"
27 #include "librados/AioCompletionImpl.h"
28 #include "common/RefCountedObj.h"
29
30 namespace libradosstriper {
31
32 using MultiAioCompletionImplPtr =
33 boost::intrusive_ptr<MultiAioCompletionImpl>;
34
35 struct RadosStriperImpl {
36
37 /**
38 * exception wrapper around an error code
39 */
40 struct ErrorCode {
41 ErrorCode(int error) : m_code(error) {};
42 int m_code;
43 };
44
45 /*
46 * Constructor
47 * @param cluster_name name of the cluster, can be NULL
48 * @param client_name has 2 meanings depending on cluster_name
49 * - if cluster_name is null : this is the client id
50 * - else : this is the full client name in format type.id
51 */
52 RadosStriperImpl(librados::IoCtx& ioctx, librados::IoCtxImpl *ioctx_impl);
53 /// Destructor
54 ~RadosStriperImpl() {};
55
56 // configuration
57 int setObjectLayoutStripeUnit(unsigned int stripe_unit);
58 int setObjectLayoutStripeCount(unsigned int stripe_count);
59 int setObjectLayoutObjectSize(unsigned int object_size);
60
61 // xattrs
62 int getxattr(const object_t& soid, const char *name, bufferlist& bl);
63 int setxattr(const object_t& soid, const char *name, bufferlist& bl);
64 int getxattrs(const object_t& soid, map<string, bufferlist>& attrset);
65 int rmxattr(const object_t& soid, const char *name);
66
67 // io
68 int write(const std::string& soid, const bufferlist& bl, size_t len, uint64_t off);
69 int append(const std::string& soid, const bufferlist& bl, size_t len);
70 int write_full(const std::string& soid, const bufferlist& bl);
71 int read(const std::string& soid, bufferlist* pbl, size_t len, uint64_t off);
72
73 // asynchronous io
74 int aio_write(const std::string& soid, librados::AioCompletionImpl *c,
75 const bufferlist& bl, size_t len, uint64_t off);
76 int aio_append(const std::string& soid, librados::AioCompletionImpl *c,
77 const bufferlist& bl, size_t len);
78 int aio_write_full(const std::string& soid, librados::AioCompletionImpl *c,
79 const bufferlist& bl);
80 int aio_read(const std::string& soid, librados::AioCompletionImpl *c,
81 bufferlist* pbl, size_t len, uint64_t off);
82 int aio_read(const std::string& soid, librados::AioCompletionImpl *c,
83 char* buf, size_t len, uint64_t off);
84 int aio_flush();
85
86 // stat, deletion and truncation
87 int stat(const std::string& soid, uint64_t *psize, time_t *pmtime);
88 int stat2(const std::string& soid, uint64_t *psize, struct timespec *pts);
89 template<class TimeType>
90 struct StatFunction {
91 typedef int (librados::IoCtxImpl::*Type) (const object_t& oid,
92 librados::AioCompletionImpl *c,
93 uint64_t *psize, TimeType *pmtime);
94 };
95 template<class TimeType>
96 int aio_generic_stat(const std::string& soid, librados::AioCompletionImpl *c,
97 uint64_t *psize, TimeType *pmtime,
98 typename StatFunction<TimeType>::Type statFunction);
99 int aio_stat(const std::string& soid, librados::AioCompletionImpl *c,
100 uint64_t *psize, time_t *pmtime);
101 int aio_stat2(const std::string& soid, librados::AioCompletionImpl *c,
102 uint64_t *psize, struct timespec *pts);
103 int remove(const std::string& soid, int flags=0);
104 int trunc(const std::string& soid, uint64_t size);
105
106 // asynchronous remove. Note that the removal is not 100% parallelized :
107 // the removal of the first rados object of the striped object will be
108 // done via a syncrhonous call after the completion of all other removals.
109 // These are done asynchrounously and in parallel
110 int aio_remove(const std::string& soid, librados::AioCompletionImpl *c, int flags=0);
111
112 // reference counting
113 void get() {
114 std::lock_guard l{lock};
115 m_refCnt ++ ;
116 }
117 void put() {
118 bool deleteme = false;
119 lock.lock();
120 m_refCnt --;
121 if (m_refCnt == 0)
122 deleteme = true;
123 cond.notify_all();
124 lock.unlock();
125 if (deleteme)
126 delete this;
127 }
128
129 // objectid manipulation
130 std::string getObjectId(const object_t& soid, long long unsigned objectno);
131
132 // opening and closing of striped objects
133 void unlockObject(const std::string& soid,
134 const std::string& lockCookie);
135 void aio_unlockObject(const std::string& soid,
136 const std::string& lockCookie,
137 librados::AioCompletion *c);
138
139 // internal versions of IO method
140 int write_in_open_object(const std::string& soid,
141 const ceph_file_layout& layout,
142 const std::string& lockCookie,
143 const bufferlist& bl,
144 size_t len,
145 uint64_t off);
146 int aio_write_in_open_object(const std::string& soid,
147 librados::AioCompletionImpl *c,
148 const ceph_file_layout& layout,
149 const std::string& lockCookie,
150 const bufferlist& bl,
151 size_t len,
152 uint64_t off);
153 int internal_aio_write(const std::string& soid,
154 MultiAioCompletionImplPtr c,
155 const bufferlist& bl,
156 size_t len,
157 uint64_t off,
158 const ceph_file_layout& layout);
159
160 int extract_uint32_attr(std::map<std::string, bufferlist> &attrs,
161 const std::string& key,
162 ceph_le32 *value);
163
164 int extract_sizet_attr(std::map<std::string, bufferlist> &attrs,
165 const std::string& key,
166 size_t *value);
167
168 int internal_get_layout_and_size(const std::string& oid,
169 ceph_file_layout *layout,
170 uint64_t *size);
171
172 int internal_aio_remove(const std::string& soid,
173 MultiAioCompletionImplPtr multi_completion,
174 int flags=0);
175
176 /**
177 * opens an existing striped object and takes a shared lock on it
178 * @return 0 if everything is ok and the lock was taken. -errcode otherwise
179 * In particulae, if the striped object does not exists, -ENOENT is returned
180 * In case the return code in not 0, no lock is taken
181 */
182 int openStripedObjectForRead(const std::string& soid,
183 ceph_file_layout *layout,
184 uint64_t *size,
185 std::string *lockCookie);
186
187 /**
188 * opens an existing striped object, takes a shared lock on it
189 * and sets its size to the size it will have after the write.
190 * In case the striped object does not exists, it will create it by
191 * calling createOrOpenStripedObject.
192 * @param layout this is filled with the layout of the file
193 * @param size new size of the file (together with isFileSizeAbsolute)
194 * In case of success, this is filled with the size of the file before the opening
195 * @param isFileSizeAbsolute if false, this means that the given size should
196 * be added to the current file size (append mode)
197 * @return 0 if everything is ok and the lock was taken. -errcode otherwise
198 * In case the return code in not 0, no lock is taken
199 */
200 int openStripedObjectForWrite(const std::string& soid,
201 ceph_file_layout *layout,
202 uint64_t *size,
203 std::string *lockCookie,
204 bool isFileSizeAbsolute);
205 /**
206 * creates an empty striped object with the given size and opens it calling
207 * openStripedObjectForWrite, which implies taking a shared lock on it
208 * Also deals with the cases where the object was created in the mean time
209 * @param isFileSizeAbsolute if false, this means that the given size should
210 * be added to the current file size (append mode). This of course only makes
211 * sense in case the striped object already exists
212 * @return 0 if everything is ok and the lock was taken. -errcode otherwise
213 * In case the return code in not 0, no lock is taken
214 */
215 int createAndOpenStripedObject(const std::string& soid,
216 ceph_file_layout *layout,
217 uint64_t size,
218 std::string *lockCookie,
219 bool isFileSizeAbsolute);
220
221 /**
222 * truncates an object synchronously. Should only be called with size < original_size
223 */
224 int truncate(const std::string& soid,
225 uint64_t original_size,
226 uint64_t size,
227 ceph_file_layout &layout);
228
229 /**
230 * truncates an object asynchronously. Should only be called with size < original_size
231 * note that the method is not 100% asynchronous, only the removal of rados objects
232 * is, the (potential) truncation of the rados object residing just at the truncation
233 * point is synchronous for lack of asynchronous truncation in the rados layer
234 */
235 int aio_truncate(const std::string& soid,
236 MultiAioCompletionImplPtr c,
237 uint64_t original_size,
238 uint64_t size,
239 ceph_file_layout &layout);
240
241 /**
242 * grows an object (adding 0s). Should only be called with size > original_size
243 */
244 int grow(const std::string& soid,
245 uint64_t original_size,
246 uint64_t size,
247 ceph_file_layout &layout);
248
249 /**
250 * creates a unique identifier
251 */
252 static std::string getUUID();
253
254 CephContext *cct() {
255 return (CephContext*)m_radosCluster.cct();
256 }
257
258 // reference counting
259 std::condition_variable cond;
260 int m_refCnt;
261 std::mutex lock;
262
263
264 // Context
265 librados::Rados m_radosCluster;
266 librados::IoCtx m_ioCtx;
267 librados::IoCtxImpl *m_ioCtxImpl;
268
269 // Default layout
270 ceph_file_layout m_layout;
271 };
272 }
273 #endif