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