1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2014 Sebastien Ponce <sebastien.ponce@cern.ch>
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.
15 #ifndef CEPH_LIBRADOSSTRIPER_RADOSSTRIPERIMPL_H
16 #define CEPH_LIBRADOSSTRIPER_RADOSSTRIPERIMPL_H
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"
26 #include "librados/IoCtxImpl.h"
27 #include "librados/AioCompletionImpl.h"
28 #include "common/RefCountedObj.h"
30 namespace libradosstriper
{
32 using MultiAioCompletionImplPtr
=
33 boost::intrusive_ptr
<MultiAioCompletionImpl
>;
35 struct RadosStriperImpl
{
38 * exception wrapper around an error code
41 ErrorCode(int error
) : m_code(error
) {};
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
52 RadosStriperImpl(librados::IoCtx
& ioctx
, librados::IoCtxImpl
*ioctx_impl
);
54 ~RadosStriperImpl() {};
57 int setObjectLayoutStripeUnit(unsigned int stripe_unit
);
58 int setObjectLayoutStripeCount(unsigned int stripe_count
);
59 int setObjectLayoutObjectSize(unsigned int object_size
);
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
);
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
);
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
);
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
>
91 typedef int (librados::IoCtxImpl::*Type
) (const object_t
& oid
,
92 librados::AioCompletionImpl
*c
,
93 uint64_t *psize
, TimeType
*pmtime
);
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
);
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);
112 // reference counting
114 std::lock_guard l
{lock
};
118 bool deleteme
= false;
129 // objectid manipulation
130 std::string
getObjectId(const object_t
& soid
, long long unsigned objectno
);
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
);
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
,
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
,
153 int internal_aio_write(const std::string
& soid
,
154 MultiAioCompletionImplPtr c
,
155 const bufferlist
& bl
,
158 const ceph_file_layout
& layout
);
160 int extract_uint32_attr(std::map
<std::string
, bufferlist
> &attrs
,
161 const std::string
& key
,
164 int extract_sizet_attr(std::map
<std::string
, bufferlist
> &attrs
,
165 const std::string
& key
,
168 int internal_get_layout_and_size(const std::string
& oid
,
169 ceph_file_layout
*layout
,
172 int internal_aio_remove(const std::string
& soid
,
173 MultiAioCompletionImplPtr multi_completion
,
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
182 int openStripedObjectForRead(const std::string
& soid
,
183 ceph_file_layout
*layout
,
185 std::string
*lockCookie
);
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
200 int openStripedObjectForWrite(const std::string
& soid
,
201 ceph_file_layout
*layout
,
203 std::string
*lockCookie
,
204 bool isFileSizeAbsolute
);
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
215 int createAndOpenStripedObject(const std::string
& soid
,
216 ceph_file_layout
*layout
,
218 std::string
*lockCookie
,
219 bool isFileSizeAbsolute
);
222 * truncates an object synchronously. Should only be called with size < original_size
224 int truncate(const std::string
& soid
,
225 uint64_t original_size
,
227 ceph_file_layout
&layout
);
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
235 int aio_truncate(const std::string
& soid
,
236 MultiAioCompletionImplPtr c
,
237 uint64_t original_size
,
239 ceph_file_layout
&layout
);
242 * grows an object (adding 0s). Should only be called with size > original_size
244 int grow(const std::string
& soid
,
245 uint64_t original_size
,
247 ceph_file_layout
&layout
);
250 * creates a unique identifier
252 static std::string
getUUID();
255 return (CephContext
*)m_radosCluster
.cct();
258 // reference counting
259 std::condition_variable cond
;
265 librados::Rados m_radosCluster
;
266 librados::IoCtx m_ioCtx
;
267 librados::IoCtxImpl
*m_ioCtxImpl
;
270 ceph_file_layout m_layout
;