Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4 | #include <linux/sort.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 6 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 7 | #include "mds_client.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 8 | #include <linux/ceph/decode.h> |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 9 | |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 10 | /* unused map expires after 5 minutes */ |
| 11 | #define CEPH_SNAPID_MAP_TIMEOUT (5 * 60 * HZ) |
| 12 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 13 | /* |
| 14 | * Snapshots in ceph are driven in large part by cooperation from the |
| 15 | * client. In contrast to local file systems or file servers that |
| 16 | * implement snapshots at a single point in the system, ceph's |
| 17 | * distributed access to storage requires clients to help decide |
| 18 | * whether a write logically occurs before or after a recently created |
| 19 | * snapshot. |
| 20 | * |
| 21 | * This provides a perfect instantanous client-wide snapshot. Between |
| 22 | * clients, however, snapshots may appear to be applied at slightly |
| 23 | * different points in time, depending on delays in delivering the |
| 24 | * snapshot notification. |
| 25 | * |
| 26 | * Snapshots are _not_ file system-wide. Instead, each snapshot |
| 27 | * applies to the subdirectory nested beneath some directory. This |
| 28 | * effectively divides the hierarchy into multiple "realms," where all |
| 29 | * of the files contained by each realm share the same set of |
| 30 | * snapshots. An individual realm's snap set contains snapshots |
| 31 | * explicitly created on that realm, as well as any snaps in its |
| 32 | * parent's snap set _after_ the point at which the parent became it's |
| 33 | * parent (due to, say, a rename). Similarly, snaps from prior parents |
| 34 | * during the time intervals during which they were the parent are included. |
| 35 | * |
| 36 | * The client is spared most of this detail, fortunately... it must only |
| 37 | * maintains a hierarchy of realms reflecting the current parent/child |
| 38 | * realm relationship, and for each realm has an explicit list of snaps |
| 39 | * inherited from prior parents. |
| 40 | * |
| 41 | * A snap_realm struct is maintained for realms containing every inode |
| 42 | * with an open cap in the system. (The needed snap realm information is |
| 43 | * provided by the MDS whenever a cap is issued, i.e., on open.) A 'seq' |
| 44 | * version number is used to ensure that as realm parameters change (new |
| 45 | * snapshot, new parent, etc.) the client's realm hierarchy is updated. |
| 46 | * |
| 47 | * The realm hierarchy drives the generation of a 'snap context' for each |
| 48 | * realm, which simply lists the resulting set of snaps for the realm. This |
| 49 | * is attached to any writes sent to OSDs. |
| 50 | */ |
| 51 | /* |
| 52 | * Unfortunately error handling is a bit mixed here. If we get a snap |
| 53 | * update, but don't have enough memory to update our realm hierarchy, |
| 54 | * it's not clear what we can do about it (besides complaining to the |
| 55 | * console). |
| 56 | */ |
| 57 | |
| 58 | |
| 59 | /* |
| 60 | * increase ref count for the realm |
| 61 | * |
| 62 | * caller must hold snap_rwsem for write. |
| 63 | */ |
| 64 | void ceph_get_snap_realm(struct ceph_mds_client *mdsc, |
| 65 | struct ceph_snap_realm *realm) |
| 66 | { |
| 67 | dout("get_realm %p %d -> %d\n", realm, |
| 68 | atomic_read(&realm->nref), atomic_read(&realm->nref)+1); |
| 69 | /* |
| 70 | * since we _only_ increment realm refs or empty the empty |
| 71 | * list with snap_rwsem held, adjusting the empty list here is |
| 72 | * safe. we do need to protect against concurrent empty list |
| 73 | * additions, however. |
| 74 | */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 75 | if (atomic_inc_return(&realm->nref) == 1) { |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 76 | spin_lock(&mdsc->snap_empty_lock); |
| 77 | list_del_init(&realm->empty_item); |
| 78 | spin_unlock(&mdsc->snap_empty_lock); |
| 79 | } |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 82 | static void __insert_snap_realm(struct rb_root *root, |
| 83 | struct ceph_snap_realm *new) |
| 84 | { |
| 85 | struct rb_node **p = &root->rb_node; |
| 86 | struct rb_node *parent = NULL; |
| 87 | struct ceph_snap_realm *r = NULL; |
| 88 | |
| 89 | while (*p) { |
| 90 | parent = *p; |
| 91 | r = rb_entry(parent, struct ceph_snap_realm, node); |
| 92 | if (new->ino < r->ino) |
| 93 | p = &(*p)->rb_left; |
| 94 | else if (new->ino > r->ino) |
| 95 | p = &(*p)->rb_right; |
| 96 | else |
| 97 | BUG(); |
| 98 | } |
| 99 | |
| 100 | rb_link_node(&new->node, parent, p); |
| 101 | rb_insert_color(&new->node, root); |
| 102 | } |
| 103 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 104 | /* |
| 105 | * create and get the realm rooted at @ino and bump its ref count. |
| 106 | * |
| 107 | * caller must hold snap_rwsem for write. |
| 108 | */ |
| 109 | static struct ceph_snap_realm *ceph_create_snap_realm( |
| 110 | struct ceph_mds_client *mdsc, |
| 111 | u64 ino) |
| 112 | { |
| 113 | struct ceph_snap_realm *realm; |
| 114 | |
| 115 | realm = kzalloc(sizeof(*realm), GFP_NOFS); |
| 116 | if (!realm) |
| 117 | return ERR_PTR(-ENOMEM); |
| 118 | |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 119 | atomic_set(&realm->nref, 1); /* for caller */ |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 120 | realm->ino = ino; |
| 121 | INIT_LIST_HEAD(&realm->children); |
| 122 | INIT_LIST_HEAD(&realm->child_item); |
| 123 | INIT_LIST_HEAD(&realm->empty_item); |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 124 | INIT_LIST_HEAD(&realm->dirty_item); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 125 | INIT_LIST_HEAD(&realm->inodes_with_caps); |
| 126 | spin_lock_init(&realm->inodes_with_caps_lock); |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 127 | __insert_snap_realm(&mdsc->snap_realms, realm); |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 128 | mdsc->num_snap_realms++; |
| 129 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 130 | dout("create_snap_realm %llx %p\n", realm->ino, realm); |
| 131 | return realm; |
| 132 | } |
| 133 | |
| 134 | /* |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 135 | * lookup the realm rooted at @ino. |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 136 | * |
| 137 | * caller must hold snap_rwsem for write. |
| 138 | */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 139 | static struct ceph_snap_realm *__lookup_snap_realm(struct ceph_mds_client *mdsc, |
| 140 | u64 ino) |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 141 | { |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 142 | struct rb_node *n = mdsc->snap_realms.rb_node; |
| 143 | struct ceph_snap_realm *r; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 144 | |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 145 | while (n) { |
| 146 | r = rb_entry(n, struct ceph_snap_realm, node); |
| 147 | if (ino < r->ino) |
| 148 | n = n->rb_left; |
| 149 | else if (ino > r->ino) |
| 150 | n = n->rb_right; |
| 151 | else { |
| 152 | dout("lookup_snap_realm %llx %p\n", r->ino, r); |
| 153 | return r; |
| 154 | } |
| 155 | } |
| 156 | return NULL; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 159 | struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc, |
| 160 | u64 ino) |
| 161 | { |
| 162 | struct ceph_snap_realm *r; |
| 163 | r = __lookup_snap_realm(mdsc, ino); |
| 164 | if (r) |
| 165 | ceph_get_snap_realm(mdsc, r); |
| 166 | return r; |
| 167 | } |
| 168 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 169 | static void __put_snap_realm(struct ceph_mds_client *mdsc, |
| 170 | struct ceph_snap_realm *realm); |
| 171 | |
| 172 | /* |
| 173 | * called with snap_rwsem (write) |
| 174 | */ |
| 175 | static void __destroy_snap_realm(struct ceph_mds_client *mdsc, |
| 176 | struct ceph_snap_realm *realm) |
| 177 | { |
| 178 | dout("__destroy_snap_realm %p %llx\n", realm, realm->ino); |
| 179 | |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 180 | rb_erase(&realm->node, &mdsc->snap_realms); |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 181 | mdsc->num_snap_realms--; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 182 | |
| 183 | if (realm->parent) { |
| 184 | list_del_init(&realm->child_item); |
| 185 | __put_snap_realm(mdsc, realm->parent); |
| 186 | } |
| 187 | |
| 188 | kfree(realm->prior_parent_snaps); |
| 189 | kfree(realm->snaps); |
| 190 | ceph_put_snap_context(realm->cached_context); |
| 191 | kfree(realm); |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * caller holds snap_rwsem (write) |
| 196 | */ |
| 197 | static void __put_snap_realm(struct ceph_mds_client *mdsc, |
| 198 | struct ceph_snap_realm *realm) |
| 199 | { |
| 200 | dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm, |
| 201 | atomic_read(&realm->nref), atomic_read(&realm->nref)-1); |
| 202 | if (atomic_dec_and_test(&realm->nref)) |
| 203 | __destroy_snap_realm(mdsc, realm); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * caller needn't hold any locks |
| 208 | */ |
| 209 | void ceph_put_snap_realm(struct ceph_mds_client *mdsc, |
| 210 | struct ceph_snap_realm *realm) |
| 211 | { |
| 212 | dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm, |
| 213 | atomic_read(&realm->nref), atomic_read(&realm->nref)-1); |
| 214 | if (!atomic_dec_and_test(&realm->nref)) |
| 215 | return; |
| 216 | |
| 217 | if (down_write_trylock(&mdsc->snap_rwsem)) { |
| 218 | __destroy_snap_realm(mdsc, realm); |
| 219 | up_write(&mdsc->snap_rwsem); |
| 220 | } else { |
| 221 | spin_lock(&mdsc->snap_empty_lock); |
Henry C Chang | a26a185 | 2011-05-11 10:29:53 +0000 | [diff] [blame] | 222 | list_add(&realm->empty_item, &mdsc->snap_empty); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 223 | spin_unlock(&mdsc->snap_empty_lock); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Clean up any realms whose ref counts have dropped to zero. Note |
| 229 | * that this does not include realms who were created but not yet |
| 230 | * used. |
| 231 | * |
| 232 | * Called under snap_rwsem (write) |
| 233 | */ |
| 234 | static void __cleanup_empty_realms(struct ceph_mds_client *mdsc) |
| 235 | { |
| 236 | struct ceph_snap_realm *realm; |
| 237 | |
| 238 | spin_lock(&mdsc->snap_empty_lock); |
| 239 | while (!list_empty(&mdsc->snap_empty)) { |
| 240 | realm = list_first_entry(&mdsc->snap_empty, |
| 241 | struct ceph_snap_realm, empty_item); |
| 242 | list_del(&realm->empty_item); |
| 243 | spin_unlock(&mdsc->snap_empty_lock); |
| 244 | __destroy_snap_realm(mdsc, realm); |
| 245 | spin_lock(&mdsc->snap_empty_lock); |
| 246 | } |
| 247 | spin_unlock(&mdsc->snap_empty_lock); |
| 248 | } |
| 249 | |
| 250 | void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc) |
| 251 | { |
| 252 | down_write(&mdsc->snap_rwsem); |
| 253 | __cleanup_empty_realms(mdsc); |
| 254 | up_write(&mdsc->snap_rwsem); |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * adjust the parent realm of a given @realm. adjust child list, and parent |
| 259 | * pointers, and ref counts appropriately. |
| 260 | * |
| 261 | * return true if parent was changed, 0 if unchanged, <0 on error. |
| 262 | * |
| 263 | * caller must hold snap_rwsem for write. |
| 264 | */ |
| 265 | static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc, |
| 266 | struct ceph_snap_realm *realm, |
| 267 | u64 parentino) |
| 268 | { |
| 269 | struct ceph_snap_realm *parent; |
| 270 | |
| 271 | if (realm->parent_ino == parentino) |
| 272 | return 0; |
| 273 | |
| 274 | parent = ceph_lookup_snap_realm(mdsc, parentino); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 275 | if (!parent) { |
| 276 | parent = ceph_create_snap_realm(mdsc, parentino); |
| 277 | if (IS_ERR(parent)) |
| 278 | return PTR_ERR(parent); |
| 279 | } |
| 280 | dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n", |
| 281 | realm->ino, realm, realm->parent_ino, realm->parent, |
| 282 | parentino, parent); |
| 283 | if (realm->parent) { |
| 284 | list_del_init(&realm->child_item); |
| 285 | ceph_put_snap_realm(mdsc, realm->parent); |
| 286 | } |
| 287 | realm->parent_ino = parentino; |
| 288 | realm->parent = parent; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 289 | list_add(&realm->child_item, &parent->children); |
| 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | static int cmpu64_rev(const void *a, const void *b) |
| 295 | { |
| 296 | if (*(u64 *)a < *(u64 *)b) |
| 297 | return 1; |
| 298 | if (*(u64 *)a > *(u64 *)b) |
| 299 | return -1; |
| 300 | return 0; |
| 301 | } |
| 302 | |
Yan, Zheng | 97c85a8 | 2014-11-06 15:09:41 +0800 | [diff] [blame] | 303 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 304 | /* |
| 305 | * build the snap context for a given realm. |
| 306 | */ |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 307 | static int build_snap_context(struct ceph_snap_realm *realm, |
| 308 | struct list_head* dirty_realms) |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 309 | { |
| 310 | struct ceph_snap_realm *parent = realm->parent; |
| 311 | struct ceph_snap_context *snapc; |
| 312 | int err = 0; |
Alex Elder | aa711ee3 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 313 | u32 num = realm->num_prior_parent_snaps + realm->num_snaps; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 314 | |
| 315 | /* |
| 316 | * build parent context, if it hasn't been built. |
| 317 | * conservatively estimate that all parent snaps might be |
| 318 | * included by us. |
| 319 | */ |
| 320 | if (parent) { |
| 321 | if (!parent->cached_context) { |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 322 | err = build_snap_context(parent, dirty_realms); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 323 | if (err) |
| 324 | goto fail; |
| 325 | } |
| 326 | num += parent->cached_context->num_snaps; |
| 327 | } |
| 328 | |
| 329 | /* do i actually need to update? not if my context seq |
| 330 | matches realm seq, and my parents' does to. (this works |
| 331 | because we rebuild_snap_realms() works _downward_ in |
| 332 | hierarchy after each update.) */ |
| 333 | if (realm->cached_context && |
Sage Weil | ec4318bc | 2010-03-19 13:24:39 -0700 | [diff] [blame] | 334 | realm->cached_context->seq == realm->seq && |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 335 | (!parent || |
Sage Weil | ec4318bc | 2010-03-19 13:24:39 -0700 | [diff] [blame] | 336 | realm->cached_context->seq >= parent->cached_context->seq)) { |
Alex Elder | aa711ee3 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 337 | dout("build_snap_context %llx %p: %p seq %lld (%u snaps)" |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 338 | " (unchanged)\n", |
| 339 | realm->ino, realm, realm->cached_context, |
| 340 | realm->cached_context->seq, |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 341 | (unsigned int)realm->cached_context->num_snaps); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* alloc new snap context */ |
| 346 | err = -ENOMEM; |
Xi Wang | a3860c1 | 2012-05-31 16:26:04 -0700 | [diff] [blame] | 347 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64)) |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 348 | goto fail; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 349 | snapc = ceph_create_snap_context(num, GFP_NOFS); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 350 | if (!snapc) |
| 351 | goto fail; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 352 | |
| 353 | /* build (reverse sorted) snap vector */ |
| 354 | num = 0; |
| 355 | snapc->seq = realm->seq; |
| 356 | if (parent) { |
Alex Elder | aa711ee3 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 357 | u32 i; |
| 358 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 359 | /* include any of parent's snaps occurring _after_ my |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 360 | parent became my parent */ |
| 361 | for (i = 0; i < parent->cached_context->num_snaps; i++) |
| 362 | if (parent->cached_context->snaps[i] >= |
| 363 | realm->parent_since) |
| 364 | snapc->snaps[num++] = |
| 365 | parent->cached_context->snaps[i]; |
| 366 | if (parent->cached_context->seq > snapc->seq) |
| 367 | snapc->seq = parent->cached_context->seq; |
| 368 | } |
| 369 | memcpy(snapc->snaps + num, realm->snaps, |
| 370 | sizeof(u64)*realm->num_snaps); |
| 371 | num += realm->num_snaps; |
| 372 | memcpy(snapc->snaps + num, realm->prior_parent_snaps, |
| 373 | sizeof(u64)*realm->num_prior_parent_snaps); |
| 374 | num += realm->num_prior_parent_snaps; |
| 375 | |
| 376 | sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL); |
| 377 | snapc->num_snaps = num; |
Alex Elder | aa711ee3 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 378 | dout("build_snap_context %llx %p: %p seq %lld (%u snaps)\n", |
| 379 | realm->ino, realm, snapc, snapc->seq, |
| 380 | (unsigned int) snapc->num_snaps); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 381 | |
Yan, Zheng | 9f4057f | 2017-09-22 09:26:57 +0800 | [diff] [blame] | 382 | ceph_put_snap_context(realm->cached_context); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 383 | realm->cached_context = snapc; |
Yan, Zheng | 9f4057f | 2017-09-22 09:26:57 +0800 | [diff] [blame] | 384 | /* queue realm for cap_snap creation */ |
| 385 | list_add_tail(&realm->dirty_item, dirty_realms); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | |
| 388 | fail: |
| 389 | /* |
| 390 | * if we fail, clear old (incorrect) cached_context... hopefully |
| 391 | * we'll have better luck building it later |
| 392 | */ |
| 393 | if (realm->cached_context) { |
| 394 | ceph_put_snap_context(realm->cached_context); |
| 395 | realm->cached_context = NULL; |
| 396 | } |
| 397 | pr_err("build_snap_context %llx %p fail %d\n", realm->ino, |
| 398 | realm, err); |
| 399 | return err; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * rebuild snap context for the given realm and all of its children. |
| 404 | */ |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 405 | static void rebuild_snap_realms(struct ceph_snap_realm *realm, |
| 406 | struct list_head *dirty_realms) |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 407 | { |
| 408 | struct ceph_snap_realm *child; |
| 409 | |
| 410 | dout("rebuild_snap_realms %llx %p\n", realm->ino, realm); |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 411 | build_snap_context(realm, dirty_realms); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 412 | |
| 413 | list_for_each_entry(child, &realm->children, child_item) |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 414 | rebuild_snap_realms(child, dirty_realms); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
| 418 | /* |
| 419 | * helper to allocate and decode an array of snapids. free prior |
| 420 | * instance, if any. |
| 421 | */ |
Alex Elder | aa711ee3 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 422 | static int dup_array(u64 **dst, __le64 *src, u32 num) |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 423 | { |
Alex Elder | aa711ee3 | 2012-07-13 20:35:11 -0500 | [diff] [blame] | 424 | u32 i; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 425 | |
| 426 | kfree(*dst); |
| 427 | if (num) { |
| 428 | *dst = kcalloc(num, sizeof(u64), GFP_NOFS); |
| 429 | if (!*dst) |
| 430 | return -ENOMEM; |
| 431 | for (i = 0; i < num; i++) |
| 432 | (*dst)[i] = get_unaligned_le64(src + i); |
| 433 | } else { |
| 434 | *dst = NULL; |
| 435 | } |
| 436 | return 0; |
| 437 | } |
| 438 | |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 439 | static bool has_new_snaps(struct ceph_snap_context *o, |
| 440 | struct ceph_snap_context *n) |
| 441 | { |
| 442 | if (n->num_snaps == 0) |
| 443 | return false; |
| 444 | /* snaps are in descending order */ |
| 445 | return n->snaps[0] > o->seq; |
| 446 | } |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 447 | |
| 448 | /* |
| 449 | * When a snapshot is applied, the size/mtime inode metadata is queued |
| 450 | * in a ceph_cap_snap (one for each snapshot) until writeback |
| 451 | * completes and the metadata can be flushed back to the MDS. |
| 452 | * |
| 453 | * However, if a (sync) write is currently in-progress when we apply |
| 454 | * the snapshot, we have to wait until the write succeeds or fails |
| 455 | * (and a final size/mtime is known). In this case the |
| 456 | * cap_snap->writing = 1, and is said to be "pending." When the write |
| 457 | * finishes, we __ceph_finish_cap_snap(). |
| 458 | * |
| 459 | * Caller must hold snap_rwsem for read (i.e., the realm topology won't |
| 460 | * change). |
| 461 | */ |
Sage Weil | fc837c8f | 2010-04-13 11:41:22 -0700 | [diff] [blame] | 462 | void ceph_queue_cap_snap(struct ceph_inode_info *ci) |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 463 | { |
| 464 | struct inode *inode = &ci->vfs_inode; |
| 465 | struct ceph_cap_snap *capsnap; |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 466 | struct ceph_snap_context *old_snapc, *new_snapc; |
Sage Weil | 4a625be | 2010-08-22 15:03:56 -0700 | [diff] [blame] | 467 | int used, dirty; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 468 | |
| 469 | capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS); |
| 470 | if (!capsnap) { |
| 471 | pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode); |
| 472 | return; |
| 473 | } |
| 474 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 475 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 476 | used = __ceph_caps_used(ci); |
Sage Weil | 4a625be | 2010-08-22 15:03:56 -0700 | [diff] [blame] | 477 | dirty = __ceph_caps_dirty(ci); |
Sage Weil | af0ed56 | 2011-07-26 11:26:31 -0700 | [diff] [blame] | 478 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 479 | old_snapc = ci->i_head_snapc; |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 480 | new_snapc = ci->i_snap_realm->cached_context; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 481 | |
Sage Weil | af0ed56 | 2011-07-26 11:26:31 -0700 | [diff] [blame] | 482 | /* |
| 483 | * If there is a write in progress, treat that as a dirty Fw, |
| 484 | * even though it hasn't completed yet; by the time we finish |
| 485 | * up this capsnap it will be. |
| 486 | */ |
| 487 | if (used & CEPH_CAP_FILE_WR) |
| 488 | dirty |= CEPH_CAP_FILE_WR; |
| 489 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 490 | if (__ceph_have_pending_cap_snap(ci)) { |
| 491 | /* there is no point in queuing multiple "pending" cap_snaps, |
| 492 | as no new writes are allowed to start when pending, so any |
| 493 | writes in progress now were started before the previous |
| 494 | cap_snap. lucky us. */ |
Sage Weil | fc837c8f | 2010-04-13 11:41:22 -0700 | [diff] [blame] | 495 | dout("queue_cap_snap %p already pending\n", inode); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 496 | goto update_snapc; |
| 497 | } |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 498 | if (ci->i_wrbuffer_ref_head == 0 && |
| 499 | !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) { |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 500 | dout("queue_cap_snap %p nothing dirty|writing\n", inode); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 501 | goto update_snapc; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 504 | BUG_ON(!old_snapc); |
| 505 | |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 506 | /* |
| 507 | * There is no need to send FLUSHSNAP message to MDS if there is |
| 508 | * no new snapshot. But when there is dirty pages or on-going |
| 509 | * writes, we still need to create cap_snap. cap_snap is needed |
| 510 | * by the write path and page writeback path. |
| 511 | * |
| 512 | * also see ceph_try_drop_cap_snap() |
| 513 | */ |
| 514 | if (has_new_snaps(old_snapc, new_snapc)) { |
| 515 | if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR)) |
| 516 | capsnap->need_flush = true; |
| 517 | } else { |
| 518 | if (!(used & CEPH_CAP_FILE_WR) && |
| 519 | ci->i_wrbuffer_ref_head == 0) { |
| 520 | dout("queue_cap_snap %p " |
| 521 | "no new_snap|dirty_page|writing\n", inode); |
| 522 | goto update_snapc; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | dout("queue_cap_snap %p cap_snap %p queuing under %p %s %s\n", |
| 527 | inode, capsnap, old_snapc, ceph_cap_string(dirty), |
| 528 | capsnap->need_flush ? "" : "no_flush"); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 529 | ihold(inode); |
| 530 | |
Elena Reshetova | 805692d | 2017-03-03 11:15:07 +0200 | [diff] [blame] | 531 | refcount_set(&capsnap->nref, 1); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 532 | INIT_LIST_HEAD(&capsnap->ci_item); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 533 | |
| 534 | capsnap->follows = old_snapc->seq; |
| 535 | capsnap->issued = __ceph_caps_issued(ci, NULL); |
| 536 | capsnap->dirty = dirty; |
| 537 | |
| 538 | capsnap->mode = inode->i_mode; |
| 539 | capsnap->uid = inode->i_uid; |
| 540 | capsnap->gid = inode->i_gid; |
| 541 | |
| 542 | if (dirty & CEPH_CAP_XATTR_EXCL) { |
| 543 | __ceph_build_xattrs_blob(ci); |
| 544 | capsnap->xattr_blob = |
| 545 | ceph_buffer_get(ci->i_xattrs.blob); |
| 546 | capsnap->xattr_version = ci->i_xattrs.version; |
| 547 | } else { |
| 548 | capsnap->xattr_blob = NULL; |
| 549 | capsnap->xattr_version = 0; |
| 550 | } |
| 551 | |
| 552 | capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE; |
| 553 | |
| 554 | /* dirty page count moved from _head to this cap_snap; |
| 555 | all subsequent writes page dirties occur _after_ this |
| 556 | snapshot. */ |
| 557 | capsnap->dirty_pages = ci->i_wrbuffer_ref_head; |
| 558 | ci->i_wrbuffer_ref_head = 0; |
| 559 | capsnap->context = old_snapc; |
| 560 | list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 561 | |
| 562 | if (used & CEPH_CAP_FILE_WR) { |
| 563 | dout("queue_cap_snap %p cap_snap %p snapc %p" |
| 564 | " seq %llu used WR, now pending\n", inode, |
| 565 | capsnap, old_snapc, old_snapc->seq); |
| 566 | capsnap->writing = 1; |
| 567 | } else { |
| 568 | /* note mtime, size NOW. */ |
| 569 | __ceph_finish_cap_snap(ci, capsnap); |
| 570 | } |
| 571 | capsnap = NULL; |
Yan, Zheng | fce8515 | 2016-06-15 20:51:22 +0800 | [diff] [blame] | 572 | old_snapc = NULL; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 573 | |
| 574 | update_snapc: |
Yan, Zheng | 3765918 | 2019-04-18 11:24:57 +0800 | [diff] [blame] | 575 | if (ci->i_wrbuffer_ref_head == 0 && |
| 576 | ci->i_wr_ref == 0 && |
| 577 | ci->i_dirty_caps == 0 && |
| 578 | ci->i_flushing_caps == 0) { |
| 579 | ci->i_head_snapc = NULL; |
| 580 | } else { |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 581 | ci->i_head_snapc = ceph_get_snap_context(new_snapc); |
| 582 | dout(" new snapc is %p\n", new_snapc); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 583 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 584 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 585 | |
| 586 | kfree(capsnap); |
| 587 | ceph_put_snap_context(old_snapc); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Finalize the size, mtime for a cap_snap.. that is, settle on final values |
| 592 | * to be used for the snapshot, to be flushed back to the mds. |
| 593 | * |
| 594 | * If capsnap can now be flushed, add to snap_flush list, and return 1. |
| 595 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 596 | * Caller must hold i_ceph_lock. |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 597 | */ |
| 598 | int __ceph_finish_cap_snap(struct ceph_inode_info *ci, |
| 599 | struct ceph_cap_snap *capsnap) |
| 600 | { |
| 601 | struct inode *inode = &ci->vfs_inode; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 602 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 603 | |
| 604 | BUG_ON(capsnap->writing); |
| 605 | capsnap->size = inode->i_size; |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 606 | capsnap->mtime = inode->i_mtime; |
| 607 | capsnap->atime = inode->i_atime; |
| 608 | capsnap->ctime = inode->i_ctime; |
Jeff Layton | ec62b89 | 2019-05-29 12:23:14 -0400 | [diff] [blame^] | 609 | capsnap->btime = ci->i_btime; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 610 | capsnap->time_warp_seq = ci->i_time_warp_seq; |
Yan, Zheng | 5f743e4 | 2016-11-15 16:04:37 +0800 | [diff] [blame] | 611 | capsnap->truncate_size = ci->i_truncate_size; |
| 612 | capsnap->truncate_seq = ci->i_truncate_seq; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 613 | if (capsnap->dirty_pages) { |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 614 | dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu " |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 615 | "still has %d dirty pages\n", inode, capsnap, |
| 616 | capsnap->context, capsnap->context->seq, |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 617 | ceph_cap_string(capsnap->dirty), capsnap->size, |
| 618 | capsnap->dirty_pages); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 619 | return 0; |
| 620 | } |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 621 | |
| 622 | ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS; |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 623 | dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n", |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 624 | inode, capsnap, capsnap->context, |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 625 | capsnap->context->seq, ceph_cap_string(capsnap->dirty), |
| 626 | capsnap->size); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 627 | |
| 628 | spin_lock(&mdsc->snap_flush_lock); |
Yan, Zheng | 04242ff | 2019-02-11 15:18:52 +0800 | [diff] [blame] | 629 | if (list_empty(&ci->i_snap_flush_item)) |
| 630 | list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 631 | spin_unlock(&mdsc->snap_flush_lock); |
| 632 | return 1; /* caller may want to ceph_flush_snaps */ |
| 633 | } |
| 634 | |
Sage Weil | ed32604 | 2010-08-16 13:37:31 -0700 | [diff] [blame] | 635 | /* |
| 636 | * Queue cap_snaps for snap writeback for this realm and its children. |
| 637 | * Called under snap_rwsem, so realm topology won't change. |
| 638 | */ |
| 639 | static void queue_realm_cap_snaps(struct ceph_snap_realm *realm) |
| 640 | { |
| 641 | struct ceph_inode_info *ci; |
| 642 | struct inode *lastinode = NULL; |
Sage Weil | ed32604 | 2010-08-16 13:37:31 -0700 | [diff] [blame] | 643 | |
| 644 | dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino); |
| 645 | |
| 646 | spin_lock(&realm->inodes_with_caps_lock); |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 647 | list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) { |
Sage Weil | ed32604 | 2010-08-16 13:37:31 -0700 | [diff] [blame] | 648 | struct inode *inode = igrab(&ci->vfs_inode); |
| 649 | if (!inode) |
| 650 | continue; |
| 651 | spin_unlock(&realm->inodes_with_caps_lock); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 652 | /* avoid calling iput_final() while holding |
| 653 | * mdsc->snap_rwsem or in mds dispatch threads */ |
| 654 | ceph_async_iput(lastinode); |
Sage Weil | ed32604 | 2010-08-16 13:37:31 -0700 | [diff] [blame] | 655 | lastinode = inode; |
| 656 | ceph_queue_cap_snap(ci); |
| 657 | spin_lock(&realm->inodes_with_caps_lock); |
| 658 | } |
| 659 | spin_unlock(&realm->inodes_with_caps_lock); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 660 | ceph_async_iput(lastinode); |
Sage Weil | ed32604 | 2010-08-16 13:37:31 -0700 | [diff] [blame] | 661 | |
Sage Weil | ed32604 | 2010-08-16 13:37:31 -0700 | [diff] [blame] | 662 | dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino); |
| 663 | } |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 664 | |
| 665 | /* |
| 666 | * Parse and apply a snapblob "snap trace" from the MDS. This specifies |
| 667 | * the snap realm parameters from a given realm and all of its ancestors, |
| 668 | * up to the root. |
| 669 | * |
| 670 | * Caller must hold snap_rwsem for write. |
| 671 | */ |
| 672 | int ceph_update_snap_trace(struct ceph_mds_client *mdsc, |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 673 | void *p, void *e, bool deletion, |
| 674 | struct ceph_snap_realm **realm_ret) |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 675 | { |
| 676 | struct ceph_mds_snap_realm *ri; /* encoded */ |
| 677 | __le64 *snaps; /* encoded */ |
| 678 | __le64 *prior_parent_snaps; /* encoded */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 679 | struct ceph_snap_realm *realm = NULL; |
| 680 | struct ceph_snap_realm *first_realm = NULL; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 681 | int invalidate = 0; |
| 682 | int err = -ENOMEM; |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 683 | LIST_HEAD(dirty_realms); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 684 | |
| 685 | dout("update_snap_trace deletion=%d\n", deletion); |
| 686 | more: |
| 687 | ceph_decode_need(&p, e, sizeof(*ri), bad); |
| 688 | ri = p; |
| 689 | p += sizeof(*ri); |
| 690 | ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) + |
| 691 | le32_to_cpu(ri->num_prior_parent_snaps)), bad); |
| 692 | snaps = p; |
| 693 | p += sizeof(u64) * le32_to_cpu(ri->num_snaps); |
| 694 | prior_parent_snaps = p; |
| 695 | p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps); |
| 696 | |
| 697 | realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino)); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 698 | if (!realm) { |
| 699 | realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino)); |
| 700 | if (IS_ERR(realm)) { |
| 701 | err = PTR_ERR(realm); |
| 702 | goto fail; |
| 703 | } |
| 704 | } |
| 705 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 706 | /* ensure the parent is correct */ |
| 707 | err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent)); |
| 708 | if (err < 0) |
| 709 | goto fail; |
| 710 | invalidate += err; |
| 711 | |
| 712 | if (le64_to_cpu(ri->seq) > realm->seq) { |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 713 | dout("update_snap_trace updating %llx %p %lld -> %lld\n", |
| 714 | realm->ino, realm, realm->seq, le64_to_cpu(ri->seq)); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 715 | /* update realm parameters, snap lists */ |
| 716 | realm->seq = le64_to_cpu(ri->seq); |
| 717 | realm->created = le64_to_cpu(ri->created); |
| 718 | realm->parent_since = le64_to_cpu(ri->parent_since); |
| 719 | |
| 720 | realm->num_snaps = le32_to_cpu(ri->num_snaps); |
| 721 | err = dup_array(&realm->snaps, snaps, realm->num_snaps); |
| 722 | if (err < 0) |
| 723 | goto fail; |
| 724 | |
| 725 | realm->num_prior_parent_snaps = |
| 726 | le32_to_cpu(ri->num_prior_parent_snaps); |
| 727 | err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps, |
| 728 | realm->num_prior_parent_snaps); |
| 729 | if (err < 0) |
| 730 | goto fail; |
| 731 | |
Yan, Zheng | affbc19 | 2015-05-05 21:22:13 +0800 | [diff] [blame] | 732 | if (realm->seq > mdsc->last_snap_seq) |
| 733 | mdsc->last_snap_seq = realm->seq; |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 734 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 735 | invalidate = 1; |
| 736 | } else if (!realm->cached_context) { |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 737 | dout("update_snap_trace %llx %p seq %lld new\n", |
| 738 | realm->ino, realm, realm->seq); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 739 | invalidate = 1; |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 740 | } else { |
| 741 | dout("update_snap_trace %llx %p seq %lld unchanged\n", |
| 742 | realm->ino, realm, realm->seq); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino, |
| 746 | realm, invalidate, p, e); |
| 747 | |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 748 | /* invalidate when we reach the _end_ (root) of the trace */ |
| 749 | if (invalidate && p >= e) |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 750 | rebuild_snap_realms(realm, &dirty_realms); |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 751 | |
| 752 | if (!first_realm) |
| 753 | first_realm = realm; |
| 754 | else |
| 755 | ceph_put_snap_realm(mdsc, realm); |
| 756 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 757 | if (p < e) |
| 758 | goto more; |
| 759 | |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 760 | /* |
| 761 | * queue cap snaps _after_ we've built the new snap contexts, |
| 762 | * so that i_head_snapc can be set appropriately. |
| 763 | */ |
Sage Weil | e8e1ba96 | 2011-02-04 20:45:58 -0800 | [diff] [blame] | 764 | while (!list_empty(&dirty_realms)) { |
| 765 | realm = list_first_entry(&dirty_realms, struct ceph_snap_realm, |
| 766 | dirty_item); |
Yan, Zheng | 3ae0beb | 2017-08-28 16:36:53 +0800 | [diff] [blame] | 767 | list_del_init(&realm->dirty_item); |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 768 | queue_realm_cap_snaps(realm); |
| 769 | } |
| 770 | |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 771 | if (realm_ret) |
| 772 | *realm_ret = first_realm; |
| 773 | else |
| 774 | ceph_put_snap_realm(mdsc, first_realm); |
| 775 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 776 | __cleanup_empty_realms(mdsc); |
| 777 | return 0; |
| 778 | |
| 779 | bad: |
| 780 | err = -EINVAL; |
| 781 | fail: |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 782 | if (realm && !IS_ERR(realm)) |
| 783 | ceph_put_snap_realm(mdsc, realm); |
| 784 | if (first_realm) |
| 785 | ceph_put_snap_realm(mdsc, first_realm); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 786 | pr_err("update_snap_trace error %d\n", err); |
| 787 | return err; |
| 788 | } |
| 789 | |
| 790 | |
| 791 | /* |
| 792 | * Send any cap_snaps that are queued for flush. Try to carry |
| 793 | * s_mutex across multiple snap flushes to avoid locking overhead. |
| 794 | * |
| 795 | * Caller holds no locks. |
| 796 | */ |
| 797 | static void flush_snaps(struct ceph_mds_client *mdsc) |
| 798 | { |
| 799 | struct ceph_inode_info *ci; |
| 800 | struct inode *inode; |
| 801 | struct ceph_mds_session *session = NULL; |
| 802 | |
| 803 | dout("flush_snaps\n"); |
| 804 | spin_lock(&mdsc->snap_flush_lock); |
| 805 | while (!list_empty(&mdsc->snap_flush_list)) { |
| 806 | ci = list_first_entry(&mdsc->snap_flush_list, |
| 807 | struct ceph_inode_info, i_snap_flush_item); |
| 808 | inode = &ci->vfs_inode; |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 809 | ihold(inode); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 810 | spin_unlock(&mdsc->snap_flush_lock); |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 811 | ceph_flush_snaps(ci, &session); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 812 | /* avoid calling iput_final() while holding |
| 813 | * session->s_mutex or in mds dispatch threads */ |
| 814 | ceph_async_iput(inode); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 815 | spin_lock(&mdsc->snap_flush_lock); |
| 816 | } |
| 817 | spin_unlock(&mdsc->snap_flush_lock); |
| 818 | |
| 819 | if (session) { |
| 820 | mutex_unlock(&session->s_mutex); |
| 821 | ceph_put_mds_session(session); |
| 822 | } |
| 823 | dout("flush_snaps done\n"); |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /* |
| 828 | * Handle a snap notification from the MDS. |
| 829 | * |
| 830 | * This can take two basic forms: the simplest is just a snap creation |
| 831 | * or deletion notification on an existing realm. This should update the |
| 832 | * realm and its children. |
| 833 | * |
| 834 | * The more difficult case is realm creation, due to snap creation at a |
| 835 | * new point in the file hierarchy, or due to a rename that moves a file or |
| 836 | * directory into another realm. |
| 837 | */ |
| 838 | void ceph_handle_snap(struct ceph_mds_client *mdsc, |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 839 | struct ceph_mds_session *session, |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 840 | struct ceph_msg *msg) |
| 841 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 842 | struct super_block *sb = mdsc->fsc->sb; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 843 | int mds = session->s_mds; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 844 | u64 split; |
| 845 | int op; |
| 846 | int trace_len; |
| 847 | struct ceph_snap_realm *realm = NULL; |
| 848 | void *p = msg->front.iov_base; |
| 849 | void *e = p + msg->front.iov_len; |
| 850 | struct ceph_mds_snap_head *h; |
| 851 | int num_split_inos, num_split_realms; |
| 852 | __le64 *split_inos = NULL, *split_realms = NULL; |
| 853 | int i; |
| 854 | int locked_rwsem = 0; |
| 855 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 856 | /* decode */ |
| 857 | if (msg->front.iov_len < sizeof(*h)) |
| 858 | goto bad; |
| 859 | h = p; |
| 860 | op = le32_to_cpu(h->op); |
| 861 | split = le64_to_cpu(h->split); /* non-zero if we are splitting an |
| 862 | * existing realm */ |
| 863 | num_split_inos = le32_to_cpu(h->num_split_inos); |
| 864 | num_split_realms = le32_to_cpu(h->num_split_realms); |
| 865 | trace_len = le32_to_cpu(h->trace_len); |
| 866 | p += sizeof(*h); |
| 867 | |
| 868 | dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds, |
| 869 | ceph_snap_op_name(op), split, trace_len); |
| 870 | |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 871 | mutex_lock(&session->s_mutex); |
| 872 | session->s_seq++; |
| 873 | mutex_unlock(&session->s_mutex); |
| 874 | |
| 875 | down_write(&mdsc->snap_rwsem); |
| 876 | locked_rwsem = 1; |
| 877 | |
| 878 | if (op == CEPH_SNAP_OP_SPLIT) { |
| 879 | struct ceph_mds_snap_realm *ri; |
| 880 | |
| 881 | /* |
| 882 | * A "split" breaks part of an existing realm off into |
| 883 | * a new realm. The MDS provides a list of inodes |
| 884 | * (with caps) and child realms that belong to the new |
| 885 | * child. |
| 886 | */ |
| 887 | split_inos = p; |
| 888 | p += sizeof(u64) * num_split_inos; |
| 889 | split_realms = p; |
| 890 | p += sizeof(u64) * num_split_realms; |
| 891 | ceph_decode_need(&p, e, sizeof(*ri), bad); |
| 892 | /* we will peek at realm info here, but will _not_ |
| 893 | * advance p, as the realm update will occur below in |
| 894 | * ceph_update_snap_trace. */ |
| 895 | ri = p; |
| 896 | |
| 897 | realm = ceph_lookup_snap_realm(mdsc, split); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 898 | if (!realm) { |
| 899 | realm = ceph_create_snap_realm(mdsc, split); |
| 900 | if (IS_ERR(realm)) |
| 901 | goto out; |
| 902 | } |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 903 | |
| 904 | dout("splitting snap_realm %llx %p\n", realm->ino, realm); |
| 905 | for (i = 0; i < num_split_inos; i++) { |
| 906 | struct ceph_vino vino = { |
| 907 | .ino = le64_to_cpu(split_inos[i]), |
| 908 | .snap = CEPH_NOSNAP, |
| 909 | }; |
| 910 | struct inode *inode = ceph_find_inode(sb, vino); |
| 911 | struct ceph_inode_info *ci; |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 912 | struct ceph_snap_realm *oldrealm; |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 913 | |
| 914 | if (!inode) |
| 915 | continue; |
| 916 | ci = ceph_inode(inode); |
| 917 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 918 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 919 | if (!ci->i_snap_realm) |
| 920 | goto skip_inode; |
| 921 | /* |
| 922 | * If this inode belongs to a realm that was |
| 923 | * created after our new realm, we experienced |
| 924 | * a race (due to another split notifications |
| 925 | * arriving from a different MDS). So skip |
| 926 | * this inode. |
| 927 | */ |
| 928 | if (ci->i_snap_realm->created > |
| 929 | le64_to_cpu(ri->created)) { |
| 930 | dout(" leaving %p in newer realm %llx %p\n", |
| 931 | inode, ci->i_snap_realm->ino, |
| 932 | ci->i_snap_realm); |
| 933 | goto skip_inode; |
| 934 | } |
| 935 | dout(" will move %p to split realm %llx %p\n", |
| 936 | inode, realm->ino, realm); |
| 937 | /* |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 938 | * Move the inode to the new realm |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 939 | */ |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 940 | oldrealm = ci->i_snap_realm; |
| 941 | spin_lock(&oldrealm->inodes_with_caps_lock); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 942 | list_del_init(&ci->i_snap_realm_item); |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 943 | spin_unlock(&oldrealm->inodes_with_caps_lock); |
| 944 | |
| 945 | spin_lock(&realm->inodes_with_caps_lock); |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 946 | list_add(&ci->i_snap_realm_item, |
| 947 | &realm->inodes_with_caps); |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 948 | ci->i_snap_realm = realm; |
Luis Henriques | e3161f1 | 2018-01-12 17:19:28 +0000 | [diff] [blame] | 949 | if (realm->ino == ci->i_vino.ino) |
| 950 | realm->inode = inode; |
Sage Weil | 052bb34 | 2010-03-09 12:52:26 -0800 | [diff] [blame] | 951 | spin_unlock(&realm->inodes_with_caps_lock); |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 952 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 953 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 954 | |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 955 | ceph_get_snap_realm(mdsc, realm); |
| 956 | ceph_put_snap_realm(mdsc, oldrealm); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 957 | |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 958 | /* avoid calling iput_final() while holding |
| 959 | * mdsc->snap_rwsem or mds in dispatch threads */ |
| 960 | ceph_async_iput(inode); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 961 | continue; |
| 962 | |
| 963 | skip_inode: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 964 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 3e1d045 | 2019-05-18 20:39:55 +0800 | [diff] [blame] | 965 | ceph_async_iput(inode); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* we may have taken some of the old realm's children. */ |
| 969 | for (i = 0; i < num_split_realms; i++) { |
| 970 | struct ceph_snap_realm *child = |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 971 | __lookup_snap_realm(mdsc, |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 972 | le64_to_cpu(split_realms[i])); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 973 | if (!child) |
| 974 | continue; |
| 975 | adjust_snap_realm_parent(mdsc, child, realm->ino); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * update using the provided snap trace. if we are deleting a |
| 981 | * snap, we can avoid queueing cap_snaps. |
| 982 | */ |
| 983 | ceph_update_snap_trace(mdsc, p, e, |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 984 | op == CEPH_SNAP_OP_DESTROY, NULL); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 985 | |
Sage Weil | ae00d4f | 2010-09-16 16:26:51 -0700 | [diff] [blame] | 986 | if (op == CEPH_SNAP_OP_SPLIT) |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 987 | /* we took a reference when we created the realm, above */ |
| 988 | ceph_put_snap_realm(mdsc, realm); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 989 | |
| 990 | __cleanup_empty_realms(mdsc); |
| 991 | |
| 992 | up_write(&mdsc->snap_rwsem); |
| 993 | |
| 994 | flush_snaps(mdsc); |
| 995 | return; |
| 996 | |
| 997 | bad: |
| 998 | pr_err("corrupt snap message from mds%d\n", mds); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 999 | ceph_msg_dump(msg); |
Sage Weil | 963b61e | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1000 | out: |
| 1001 | if (locked_rwsem) |
| 1002 | up_write(&mdsc->snap_rwsem); |
| 1003 | return; |
| 1004 | } |
Yan, Zheng | 75c9627 | 2017-12-14 15:11:09 +0800 | [diff] [blame] | 1005 | |
| 1006 | struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc, |
| 1007 | u64 snap) |
| 1008 | { |
| 1009 | struct ceph_snapid_map *sm, *exist; |
| 1010 | struct rb_node **p, *parent; |
| 1011 | int ret; |
| 1012 | |
| 1013 | exist = NULL; |
| 1014 | spin_lock(&mdsc->snapid_map_lock); |
| 1015 | p = &mdsc->snapid_map_tree.rb_node; |
| 1016 | while (*p) { |
| 1017 | exist = rb_entry(*p, struct ceph_snapid_map, node); |
| 1018 | if (snap > exist->snap) { |
| 1019 | p = &(*p)->rb_left; |
| 1020 | } else if (snap < exist->snap) { |
| 1021 | p = &(*p)->rb_right; |
| 1022 | } else { |
| 1023 | if (atomic_inc_return(&exist->ref) == 1) |
| 1024 | list_del_init(&exist->lru); |
| 1025 | break; |
| 1026 | } |
| 1027 | exist = NULL; |
| 1028 | } |
| 1029 | spin_unlock(&mdsc->snapid_map_lock); |
| 1030 | if (exist) { |
| 1031 | dout("found snapid map %llx -> %x\n", exist->snap, exist->dev); |
| 1032 | return exist; |
| 1033 | } |
| 1034 | |
| 1035 | sm = kmalloc(sizeof(*sm), GFP_NOFS); |
| 1036 | if (!sm) |
| 1037 | return NULL; |
| 1038 | |
| 1039 | ret = get_anon_bdev(&sm->dev); |
| 1040 | if (ret < 0) { |
| 1041 | kfree(sm); |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | |
| 1045 | INIT_LIST_HEAD(&sm->lru); |
| 1046 | atomic_set(&sm->ref, 1); |
| 1047 | sm->snap = snap; |
| 1048 | |
| 1049 | exist = NULL; |
| 1050 | parent = NULL; |
| 1051 | p = &mdsc->snapid_map_tree.rb_node; |
| 1052 | spin_lock(&mdsc->snapid_map_lock); |
| 1053 | while (*p) { |
| 1054 | parent = *p; |
| 1055 | exist = rb_entry(*p, struct ceph_snapid_map, node); |
| 1056 | if (snap > exist->snap) |
| 1057 | p = &(*p)->rb_left; |
| 1058 | else if (snap < exist->snap) |
| 1059 | p = &(*p)->rb_right; |
| 1060 | else |
| 1061 | break; |
| 1062 | exist = NULL; |
| 1063 | } |
| 1064 | if (exist) { |
| 1065 | if (atomic_inc_return(&exist->ref) == 1) |
| 1066 | list_del_init(&exist->lru); |
| 1067 | } else { |
| 1068 | rb_link_node(&sm->node, parent, p); |
| 1069 | rb_insert_color(&sm->node, &mdsc->snapid_map_tree); |
| 1070 | } |
| 1071 | spin_unlock(&mdsc->snapid_map_lock); |
| 1072 | if (exist) { |
| 1073 | free_anon_bdev(sm->dev); |
| 1074 | kfree(sm); |
| 1075 | dout("found snapid map %llx -> %x\n", exist->snap, exist->dev); |
| 1076 | return exist; |
| 1077 | } |
| 1078 | |
| 1079 | dout("create snapid map %llx -> %x\n", sm->snap, sm->dev); |
| 1080 | return sm; |
| 1081 | } |
| 1082 | |
| 1083 | void ceph_put_snapid_map(struct ceph_mds_client* mdsc, |
| 1084 | struct ceph_snapid_map *sm) |
| 1085 | { |
| 1086 | if (!sm) |
| 1087 | return; |
| 1088 | if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) { |
| 1089 | if (!RB_EMPTY_NODE(&sm->node)) { |
| 1090 | sm->last_used = jiffies; |
| 1091 | list_add_tail(&sm->lru, &mdsc->snapid_map_lru); |
| 1092 | spin_unlock(&mdsc->snapid_map_lock); |
| 1093 | } else { |
| 1094 | /* already cleaned up by |
| 1095 | * ceph_cleanup_snapid_map() */ |
| 1096 | spin_unlock(&mdsc->snapid_map_lock); |
| 1097 | kfree(sm); |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | void ceph_trim_snapid_map(struct ceph_mds_client *mdsc) |
| 1103 | { |
| 1104 | struct ceph_snapid_map *sm; |
| 1105 | unsigned long now; |
| 1106 | LIST_HEAD(to_free); |
| 1107 | |
| 1108 | spin_lock(&mdsc->snapid_map_lock); |
| 1109 | now = jiffies; |
| 1110 | |
| 1111 | while (!list_empty(&mdsc->snapid_map_lru)) { |
| 1112 | sm = list_first_entry(&mdsc->snapid_map_lru, |
| 1113 | struct ceph_snapid_map, lru); |
| 1114 | if (time_after(sm->last_used + CEPH_SNAPID_MAP_TIMEOUT, now)) |
| 1115 | break; |
| 1116 | |
| 1117 | rb_erase(&sm->node, &mdsc->snapid_map_tree); |
| 1118 | list_move(&sm->lru, &to_free); |
| 1119 | } |
| 1120 | spin_unlock(&mdsc->snapid_map_lock); |
| 1121 | |
| 1122 | while (!list_empty(&to_free)) { |
| 1123 | sm = list_first_entry(&to_free, struct ceph_snapid_map, lru); |
| 1124 | list_del(&sm->lru); |
| 1125 | dout("trim snapid map %llx -> %x\n", sm->snap, sm->dev); |
| 1126 | free_anon_bdev(sm->dev); |
| 1127 | kfree(sm); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc) |
| 1132 | { |
| 1133 | struct ceph_snapid_map *sm; |
| 1134 | struct rb_node *p; |
| 1135 | LIST_HEAD(to_free); |
| 1136 | |
| 1137 | spin_lock(&mdsc->snapid_map_lock); |
| 1138 | while ((p = rb_first(&mdsc->snapid_map_tree))) { |
| 1139 | sm = rb_entry(p, struct ceph_snapid_map, node); |
| 1140 | rb_erase(p, &mdsc->snapid_map_tree); |
| 1141 | RB_CLEAR_NODE(p); |
| 1142 | list_move(&sm->lru, &to_free); |
| 1143 | } |
| 1144 | spin_unlock(&mdsc->snapid_map_lock); |
| 1145 | |
| 1146 | while (!list_empty(&to_free)) { |
| 1147 | sm = list_first_entry(&to_free, struct ceph_snapid_map, lru); |
| 1148 | list_del(&sm->lru); |
| 1149 | free_anon_bdev(sm->dev); |
| 1150 | if (WARN_ON_ONCE(atomic_read(&sm->ref))) { |
| 1151 | pr_err("snapid map %llx -> %x still in use\n", |
| 1152 | sm->snap, sm->dev); |
| 1153 | } |
| 1154 | } |
| 1155 | } |