1 /* -*- mode: c; c-basic-offset: 8; -*- 2 * vim: noexpandtab sw=8 ts=8 sts=0: 3 * 4 * dlmglue.c 5 * 6 * Code which implements an OCFS2 specific interface to our DLM. 7 * 8 * Copyright (C) 2003, 2004 Oracle. All rights reserved. 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public 12 * License as published by the Free Software Foundation; either 13 * version 2 of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public 21 * License along with this program; if not, write to the 22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 * Boston, MA 021110-1307, USA. 24 */ 25 26 #include <linux/types.h> 27 #include <linux/slab.h> 28 #include <linux/highmem.h> 29 #include <linux/mm.h> 30 #include <linux/kthread.h> 31 #include <linux/pagemap.h> 32 #include <linux/debugfs.h> 33 #include <linux/seq_file.h> 34 #include <linux/time.h> 35 #include <linux/quotaops.h> 36 37 #define MLOG_MASK_PREFIX ML_DLM_GLUE 38 #include <cluster/masklog.h> 39 40 #include "ocfs2.h" 41 #include "ocfs2_lockingver.h" 42 43 #include "alloc.h" 44 #include "dcache.h" 45 #include "dlmglue.h" 46 #include "extent_map.h" 47 #include "file.h" 48 #include "heartbeat.h" 49 #include "inode.h" 50 #include "journal.h" 51 #include "stackglue.h" 52 #include "slot_map.h" 53 #include "super.h" 54 #include "uptodate.h" 55 #include "quota.h" 56 #include "refcounttree.h" 57 #include "acl.h" 58 59 #include "buffer_head_io.h" 60 61 struct ocfs2_mask_waiter { 62 struct list_head mw_item; 63 int mw_status; 64 struct completion mw_complete; 65 unsigned long mw_mask; 66 unsigned long mw_goal; 67 #ifdef CONFIG_OCFS2_FS_STATS 68 ktime_t mw_lock_start; 69 #endif 70 }; 71 72 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres); 73 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres); 74 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres); 75 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres); 76 77 /* 78 * Return value from ->downconvert_worker functions. 79 * 80 * These control the precise actions of ocfs2_unblock_lock() 81 * and ocfs2_process_blocked_lock() 82 * 83 */ 84 enum ocfs2_unblock_action { 85 UNBLOCK_CONTINUE = 0, /* Continue downconvert */ 86 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire 87 * ->post_unlock callback */ 88 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire 89 * ->post_unlock() callback. */ 90 }; 91 92 struct ocfs2_unblock_ctl { 93 int requeue; 94 enum ocfs2_unblock_action unblock_action; 95 }; 96 97 /* Lockdep class keys */ 98 struct lock_class_key lockdep_keys[OCFS2_NUM_LOCK_TYPES]; 99 100 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres, 101 int new_level); 102 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres); 103 104 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres, 105 int blocking); 106 107 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres, 108 int blocking); 109 110 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb, 111 struct ocfs2_lock_res *lockres); 112 113 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres); 114 115 static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres, 116 int new_level); 117 static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres, 118 int blocking); 119 120 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres) 121 122 /* This aids in debugging situations where a bad LVB might be involved. */ 123 static void ocfs2_dump_meta_lvb_info(u64 level, 124 const char *function, 125 unsigned int line, 126 struct ocfs2_lock_res *lockres) 127 { 128 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 129 130 mlog(level, "LVB information for %s (called from %s:%u):\n", 131 lockres->l_name, function, line); 132 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n", 133 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters), 134 be32_to_cpu(lvb->lvb_igeneration)); 135 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n", 136 (unsigned long long)be64_to_cpu(lvb->lvb_isize), 137 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid), 138 be16_to_cpu(lvb->lvb_imode)); 139 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, " 140 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink), 141 (long long)be64_to_cpu(lvb->lvb_iatime_packed), 142 (long long)be64_to_cpu(lvb->lvb_ictime_packed), 143 (long long)be64_to_cpu(lvb->lvb_imtime_packed), 144 be32_to_cpu(lvb->lvb_iattr)); 145 } 146 147 148 /* 149 * OCFS2 Lock Resource Operations 150 * 151 * These fine tune the behavior of the generic dlmglue locking infrastructure. 152 * 153 * The most basic of lock types can point ->l_priv to their respective 154 * struct ocfs2_super and allow the default actions to manage things. 155 * 156 * Right now, each lock type also needs to implement an init function, 157 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres() 158 * should be called when the lock is no longer needed (i.e., object 159 * destruction time). 160 */ 161 struct ocfs2_lock_res_ops { 162 /* 163 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define 164 * this callback if ->l_priv is not an ocfs2_super pointer 165 */ 166 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *); 167 168 /* 169 * Optionally called in the downconvert thread after a 170 * successful downconvert. The lockres will not be referenced 171 * after this callback is called, so it is safe to free 172 * memory, etc. 173 * 174 * The exact semantics of when this is called are controlled 175 * by ->downconvert_worker() 176 */ 177 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *); 178 179 /* 180 * Allow a lock type to add checks to determine whether it is 181 * safe to downconvert a lock. Return 0 to re-queue the 182 * downconvert at a later time, nonzero to continue. 183 * 184 * For most locks, the default checks that there are no 185 * incompatible holders are sufficient. 186 * 187 * Called with the lockres spinlock held. 188 */ 189 int (*check_downconvert)(struct ocfs2_lock_res *, int); 190 191 /* 192 * Allows a lock type to populate the lock value block. This 193 * is called on downconvert, and when we drop a lock. 194 * 195 * Locks that want to use this should set LOCK_TYPE_USES_LVB 196 * in the flags field. 197 * 198 * Called with the lockres spinlock held. 199 */ 200 void (*set_lvb)(struct ocfs2_lock_res *); 201 202 /* 203 * Called from the downconvert thread when it is determined 204 * that a lock will be downconverted. This is called without 205 * any locks held so the function can do work that might 206 * schedule (syncing out data, etc). 207 * 208 * This should return any one of the ocfs2_unblock_action 209 * values, depending on what it wants the thread to do. 210 */ 211 int (*downconvert_worker)(struct ocfs2_lock_res *, int); 212 213 /* 214 * LOCK_TYPE_* flags which describe the specific requirements 215 * of a lock type. Descriptions of each individual flag follow. 216 */ 217 int flags; 218 }; 219 220 /* 221 * Some locks want to "refresh" potentially stale data when a 222 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this 223 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the 224 * individual lockres l_flags member from the ast function. It is 225 * expected that the locking wrapper will clear the 226 * OCFS2_LOCK_NEEDS_REFRESH flag when done. 227 */ 228 #define LOCK_TYPE_REQUIRES_REFRESH 0x1 229 230 /* 231 * Indicate that a lock type makes use of the lock value block. The 232 * ->set_lvb lock type callback must be defined. 233 */ 234 #define LOCK_TYPE_USES_LVB 0x2 235 236 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = { 237 .get_osb = ocfs2_get_inode_osb, 238 .flags = 0, 239 }; 240 241 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = { 242 .get_osb = ocfs2_get_inode_osb, 243 .check_downconvert = ocfs2_check_meta_downconvert, 244 .set_lvb = ocfs2_set_meta_lvb, 245 .downconvert_worker = ocfs2_data_convert_worker, 246 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB, 247 }; 248 249 static struct ocfs2_lock_res_ops ocfs2_super_lops = { 250 .flags = LOCK_TYPE_REQUIRES_REFRESH, 251 }; 252 253 static struct ocfs2_lock_res_ops ocfs2_rename_lops = { 254 .flags = 0, 255 }; 256 257 static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = { 258 .flags = 0, 259 }; 260 261 static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = { 262 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB, 263 }; 264 265 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = { 266 .get_osb = ocfs2_get_dentry_osb, 267 .post_unlock = ocfs2_dentry_post_unlock, 268 .downconvert_worker = ocfs2_dentry_convert_worker, 269 .flags = 0, 270 }; 271 272 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = { 273 .get_osb = ocfs2_get_inode_osb, 274 .flags = 0, 275 }; 276 277 static struct ocfs2_lock_res_ops ocfs2_flock_lops = { 278 .get_osb = ocfs2_get_file_osb, 279 .flags = 0, 280 }; 281 282 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = { 283 .set_lvb = ocfs2_set_qinfo_lvb, 284 .get_osb = ocfs2_get_qinfo_osb, 285 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB, 286 }; 287 288 static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops = { 289 .check_downconvert = ocfs2_check_refcount_downconvert, 290 .downconvert_worker = ocfs2_refcount_convert_worker, 291 .flags = 0, 292 }; 293 294 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres) 295 { 296 return lockres->l_type == OCFS2_LOCK_TYPE_META || 297 lockres->l_type == OCFS2_LOCK_TYPE_RW || 298 lockres->l_type == OCFS2_LOCK_TYPE_OPEN; 299 } 300 301 static inline struct ocfs2_lock_res *ocfs2_lksb_to_lock_res(struct ocfs2_dlm_lksb *lksb) 302 { 303 return container_of(lksb, struct ocfs2_lock_res, l_lksb); 304 } 305 306 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres) 307 { 308 BUG_ON(!ocfs2_is_inode_lock(lockres)); 309 310 return (struct inode *) lockres->l_priv; 311 } 312 313 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres) 314 { 315 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY); 316 317 return (struct ocfs2_dentry_lock *)lockres->l_priv; 318 } 319 320 static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres) 321 { 322 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO); 323 324 return (struct ocfs2_mem_dqinfo *)lockres->l_priv; 325 } 326 327 static inline struct ocfs2_refcount_tree * 328 ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res) 329 { 330 return container_of(res, struct ocfs2_refcount_tree, rf_lockres); 331 } 332 333 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres) 334 { 335 if (lockres->l_ops->get_osb) 336 return lockres->l_ops->get_osb(lockres); 337 338 return (struct ocfs2_super *)lockres->l_priv; 339 } 340 341 static int ocfs2_lock_create(struct ocfs2_super *osb, 342 struct ocfs2_lock_res *lockres, 343 int level, 344 u32 dlm_flags); 345 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres, 346 int wanted); 347 static void __ocfs2_cluster_unlock(struct ocfs2_super *osb, 348 struct ocfs2_lock_res *lockres, 349 int level, unsigned long caller_ip); 350 static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb, 351 struct ocfs2_lock_res *lockres, 352 int level) 353 { 354 __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_); 355 } 356 357 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres); 358 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres); 359 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres); 360 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level); 361 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb, 362 struct ocfs2_lock_res *lockres); 363 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres, 364 int convert); 365 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \ 366 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \ 367 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \ 368 _err, _func, _lockres->l_name); \ 369 else \ 370 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \ 371 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \ 372 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \ 373 } while (0) 374 static int ocfs2_downconvert_thread(void *arg); 375 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb, 376 struct ocfs2_lock_res *lockres); 377 static int ocfs2_inode_lock_update(struct inode *inode, 378 struct buffer_head **bh); 379 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb); 380 static inline int ocfs2_highest_compat_lock_level(int level); 381 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres, 382 int new_level); 383 static int ocfs2_downconvert_lock(struct ocfs2_super *osb, 384 struct ocfs2_lock_res *lockres, 385 int new_level, 386 int lvb, 387 unsigned int generation); 388 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb, 389 struct ocfs2_lock_res *lockres); 390 static int ocfs2_cancel_convert(struct ocfs2_super *osb, 391 struct ocfs2_lock_res *lockres); 392 393 394 static void ocfs2_build_lock_name(enum ocfs2_lock_type type, 395 u64 blkno, 396 u32 generation, 397 char *name) 398 { 399 int len; 400 401 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES); 402 403 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x", 404 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD, 405 (long long)blkno, generation); 406 407 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1)); 408 409 mlog(0, "built lock resource with name: %s\n", name); 410 } 411 412 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock); 413 414 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res, 415 struct ocfs2_dlm_debug *dlm_debug) 416 { 417 mlog(0, "Add tracking for lockres %s\n", res->l_name); 418 419 spin_lock(&ocfs2_dlm_tracking_lock); 420 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking); 421 spin_unlock(&ocfs2_dlm_tracking_lock); 422 } 423 424 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res) 425 { 426 spin_lock(&ocfs2_dlm_tracking_lock); 427 if (!list_empty(&res->l_debug_list)) 428 list_del_init(&res->l_debug_list); 429 spin_unlock(&ocfs2_dlm_tracking_lock); 430 } 431 432 #ifdef CONFIG_OCFS2_FS_STATS 433 static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res) 434 { 435 res->l_lock_refresh = 0; 436 memset(&res->l_lock_prmode, 0, sizeof(struct ocfs2_lock_stats)); 437 memset(&res->l_lock_exmode, 0, sizeof(struct ocfs2_lock_stats)); 438 } 439 440 static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level, 441 struct ocfs2_mask_waiter *mw, int ret) 442 { 443 u32 usec; 444 ktime_t kt; 445 struct ocfs2_lock_stats *stats; 446 447 if (level == LKM_PRMODE) 448 stats = &res->l_lock_prmode; 449 else if (level == LKM_EXMODE) 450 stats = &res->l_lock_exmode; 451 else 452 return; 453 454 kt = ktime_sub(ktime_get(), mw->mw_lock_start); 455 usec = ktime_to_us(kt); 456 457 stats->ls_gets++; 458 stats->ls_total += ktime_to_ns(kt); 459 /* overflow */ 460 if (unlikely(stats->ls_gets == 0)) { 461 stats->ls_gets++; 462 stats->ls_total = ktime_to_ns(kt); 463 } 464 465 if (stats->ls_max < usec) 466 stats->ls_max = usec; 467 468 if (ret) 469 stats->ls_fail++; 470 } 471 472 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres) 473 { 474 lockres->l_lock_refresh++; 475 } 476 477 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw) 478 { 479 mw->mw_lock_start = ktime_get(); 480 } 481 #else 482 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res) 483 { 484 } 485 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, 486 int level, struct ocfs2_mask_waiter *mw, int ret) 487 { 488 } 489 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres) 490 { 491 } 492 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw) 493 { 494 } 495 #endif 496 497 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb, 498 struct ocfs2_lock_res *res, 499 enum ocfs2_lock_type type, 500 struct ocfs2_lock_res_ops *ops, 501 void *priv) 502 { 503 res->l_type = type; 504 res->l_ops = ops; 505 res->l_priv = priv; 506 507 res->l_level = DLM_LOCK_IV; 508 res->l_requested = DLM_LOCK_IV; 509 res->l_blocking = DLM_LOCK_IV; 510 res->l_action = OCFS2_AST_INVALID; 511 res->l_unlock_action = OCFS2_UNLOCK_INVALID; 512 513 res->l_flags = OCFS2_LOCK_INITIALIZED; 514 515 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug); 516 517 ocfs2_init_lock_stats(res); 518 #ifdef CONFIG_DEBUG_LOCK_ALLOC 519 if (type != OCFS2_LOCK_TYPE_OPEN) 520 lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type], 521 &lockdep_keys[type], 0); 522 else 523 res->l_lockdep_map.key = NULL; 524 #endif 525 } 526 527 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res) 528 { 529 /* This also clears out the lock status block */ 530 memset(res, 0, sizeof(struct ocfs2_lock_res)); 531 spin_lock_init(&res->l_lock); 532 init_waitqueue_head(&res->l_event); 533 INIT_LIST_HEAD(&res->l_blocked_list); 534 INIT_LIST_HEAD(&res->l_mask_waiters); 535 INIT_LIST_HEAD(&res->l_holders); 536 } 537 538 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res, 539 enum ocfs2_lock_type type, 540 unsigned int generation, 541 struct inode *inode) 542 { 543 struct ocfs2_lock_res_ops *ops; 544 545 switch(type) { 546 case OCFS2_LOCK_TYPE_RW: 547 ops = &ocfs2_inode_rw_lops; 548 break; 549 case OCFS2_LOCK_TYPE_META: 550 ops = &ocfs2_inode_inode_lops; 551 break; 552 case OCFS2_LOCK_TYPE_OPEN: 553 ops = &ocfs2_inode_open_lops; 554 break; 555 default: 556 mlog_bug_on_msg(1, "type: %d\n", type); 557 ops = NULL; /* thanks, gcc */ 558 break; 559 }; 560 561 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno, 562 generation, res->l_name); 563 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode); 564 } 565 566 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres) 567 { 568 struct inode *inode = ocfs2_lock_res_inode(lockres); 569 570 return OCFS2_SB(inode->i_sb); 571 } 572 573 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres) 574 { 575 struct ocfs2_mem_dqinfo *info = lockres->l_priv; 576 577 return OCFS2_SB(info->dqi_gi.dqi_sb); 578 } 579 580 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres) 581 { 582 struct ocfs2_file_private *fp = lockres->l_priv; 583 584 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb); 585 } 586 587 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres) 588 { 589 __be64 inode_blkno_be; 590 591 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], 592 sizeof(__be64)); 593 594 return be64_to_cpu(inode_blkno_be); 595 } 596 597 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres) 598 { 599 struct ocfs2_dentry_lock *dl = lockres->l_priv; 600 601 return OCFS2_SB(dl->dl_inode->i_sb); 602 } 603 604 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl, 605 u64 parent, struct inode *inode) 606 { 607 int len; 608 u64 inode_blkno = OCFS2_I(inode)->ip_blkno; 609 __be64 inode_blkno_be = cpu_to_be64(inode_blkno); 610 struct ocfs2_lock_res *lockres = &dl->dl_lockres; 611 612 ocfs2_lock_res_init_once(lockres); 613 614 /* 615 * Unfortunately, the standard lock naming scheme won't work 616 * here because we have two 16 byte values to use. Instead, 617 * we'll stuff the inode number as a binary value. We still 618 * want error prints to show something without garbling the 619 * display, so drop a null byte in there before the inode 620 * number. A future version of OCFS2 will likely use all 621 * binary lock names. The stringified names have been a 622 * tremendous aid in debugging, but now that the debugfs 623 * interface exists, we can mangle things there if need be. 624 * 625 * NOTE: We also drop the standard "pad" value (the total lock 626 * name size stays the same though - the last part is all 627 * zeros due to the memset in ocfs2_lock_res_init_once() 628 */ 629 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START, 630 "%c%016llx", 631 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY), 632 (long long)parent); 633 634 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1)); 635 636 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be, 637 sizeof(__be64)); 638 639 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres, 640 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops, 641 dl); 642 } 643 644 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res, 645 struct ocfs2_super *osb) 646 { 647 /* Superblock lockres doesn't come from a slab so we call init 648 * once on it manually. */ 649 ocfs2_lock_res_init_once(res); 650 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO, 651 0, res->l_name); 652 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER, 653 &ocfs2_super_lops, osb); 654 } 655 656 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res, 657 struct ocfs2_super *osb) 658 { 659 /* Rename lockres doesn't come from a slab so we call init 660 * once on it manually. */ 661 ocfs2_lock_res_init_once(res); 662 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name); 663 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME, 664 &ocfs2_rename_lops, osb); 665 } 666 667 static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res, 668 struct ocfs2_super *osb) 669 { 670 /* nfs_sync lockres doesn't come from a slab so we call init 671 * once on it manually. */ 672 ocfs2_lock_res_init_once(res); 673 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name); 674 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC, 675 &ocfs2_nfs_sync_lops, osb); 676 } 677 678 static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res, 679 struct ocfs2_super *osb) 680 { 681 ocfs2_lock_res_init_once(res); 682 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name); 683 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN, 684 &ocfs2_orphan_scan_lops, osb); 685 } 686 687 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres, 688 struct ocfs2_file_private *fp) 689 { 690 struct inode *inode = fp->fp_file->f_mapping->host; 691 struct ocfs2_inode_info *oi = OCFS2_I(inode); 692 693 ocfs2_lock_res_init_once(lockres); 694 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno, 695 inode->i_generation, lockres->l_name); 696 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres, 697 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops, 698 fp); 699 lockres->l_flags |= OCFS2_LOCK_NOCACHE; 700 } 701 702 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres, 703 struct ocfs2_mem_dqinfo *info) 704 { 705 ocfs2_lock_res_init_once(lockres); 706 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type, 707 0, lockres->l_name); 708 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres, 709 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops, 710 info); 711 } 712 713 void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res *lockres, 714 struct ocfs2_super *osb, u64 ref_blkno, 715 unsigned int generation) 716 { 717 ocfs2_lock_res_init_once(lockres); 718 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT, ref_blkno, 719 generation, lockres->l_name); 720 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_REFCOUNT, 721 &ocfs2_refcount_block_lops, osb); 722 } 723 724 void ocfs2_lock_res_free(struct ocfs2_lock_res *res) 725 { 726 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED)) 727 return; 728 729 ocfs2_remove_lockres_tracking(res); 730 731 mlog_bug_on_msg(!list_empty(&res->l_blocked_list), 732 "Lockres %s is on the blocked list\n", 733 res->l_name); 734 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters), 735 "Lockres %s has mask waiters pending\n", 736 res->l_name); 737 mlog_bug_on_msg(spin_is_locked(&res->l_lock), 738 "Lockres %s is locked\n", 739 res->l_name); 740 mlog_bug_on_msg(res->l_ro_holders, 741 "Lockres %s has %u ro holders\n", 742 res->l_name, res->l_ro_holders); 743 mlog_bug_on_msg(res->l_ex_holders, 744 "Lockres %s has %u ex holders\n", 745 res->l_name, res->l_ex_holders); 746 747 /* Need to clear out the lock status block for the dlm */ 748 memset(&res->l_lksb, 0, sizeof(res->l_lksb)); 749 750 res->l_flags = 0UL; 751 } 752 753 /* 754 * Keep a list of processes who have interest in a lockres. 755 * Note: this is now only uesed for check recursive cluster locking. 756 */ 757 static inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres, 758 struct ocfs2_lock_holder *oh) 759 { 760 INIT_LIST_HEAD(&oh->oh_list); 761 oh->oh_owner_pid = get_pid(task_pid(current)); 762 763 spin_lock(&lockres->l_lock); 764 list_add_tail(&oh->oh_list, &lockres->l_holders); 765 spin_unlock(&lockres->l_lock); 766 } 767 768 static inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres, 769 struct ocfs2_lock_holder *oh) 770 { 771 spin_lock(&lockres->l_lock); 772 list_del(&oh->oh_list); 773 spin_unlock(&lockres->l_lock); 774 775 put_pid(oh->oh_owner_pid); 776 } 777 778 static inline int ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres) 779 { 780 struct ocfs2_lock_holder *oh; 781 struct pid *pid; 782 783 /* look in the list of holders for one with the current task as owner */ 784 spin_lock(&lockres->l_lock); 785 pid = task_pid(current); 786 list_for_each_entry(oh, &lockres->l_holders, oh_list) { 787 if (oh->oh_owner_pid == pid) { 788 spin_unlock(&lockres->l_lock); 789 return 1; 790 } 791 } 792 spin_unlock(&lockres->l_lock); 793 794 return 0; 795 } 796 797 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres, 798 int level) 799 { 800 BUG_ON(!lockres); 801 802 switch(level) { 803 case DLM_LOCK_EX: 804 lockres->l_ex_holders++; 805 break; 806 case DLM_LOCK_PR: 807 lockres->l_ro_holders++; 808 break; 809 default: 810 BUG(); 811 } 812 } 813 814 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres, 815 int level) 816 { 817 BUG_ON(!lockres); 818 819 switch(level) { 820 case DLM_LOCK_EX: 821 BUG_ON(!lockres->l_ex_holders); 822 lockres->l_ex_holders--; 823 break; 824 case DLM_LOCK_PR: 825 BUG_ON(!lockres->l_ro_holders); 826 lockres->l_ro_holders--; 827 break; 828 default: 829 BUG(); 830 } 831 } 832 833 /* WARNING: This function lives in a world where the only three lock 834 * levels are EX, PR, and NL. It *will* have to be adjusted when more 835 * lock types are added. */ 836 static inline int ocfs2_highest_compat_lock_level(int level) 837 { 838 int new_level = DLM_LOCK_EX; 839 840 if (level == DLM_LOCK_EX) 841 new_level = DLM_LOCK_NL; 842 else if (level == DLM_LOCK_PR) 843 new_level = DLM_LOCK_PR; 844 return new_level; 845 } 846 847 static void lockres_set_flags(struct ocfs2_lock_res *lockres, 848 unsigned long newflags) 849 { 850 struct ocfs2_mask_waiter *mw, *tmp; 851 852 assert_spin_locked(&lockres->l_lock); 853 854 lockres->l_flags = newflags; 855 856 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) { 857 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal) 858 continue; 859 860 list_del_init(&mw->mw_item); 861 mw->mw_status = 0; 862 complete(&mw->mw_complete); 863 } 864 } 865 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or) 866 { 867 lockres_set_flags(lockres, lockres->l_flags | or); 868 } 869 static void lockres_clear_flags(struct ocfs2_lock_res *lockres, 870 unsigned long clear) 871 { 872 lockres_set_flags(lockres, lockres->l_flags & ~clear); 873 } 874 875 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres) 876 { 877 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY)); 878 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED)); 879 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED)); 880 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL); 881 882 lockres->l_level = lockres->l_requested; 883 if (lockres->l_level <= 884 ocfs2_highest_compat_lock_level(lockres->l_blocking)) { 885 lockres->l_blocking = DLM_LOCK_NL; 886 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED); 887 } 888 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY); 889 } 890 891 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres) 892 { 893 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY)); 894 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED)); 895 896 /* Convert from RO to EX doesn't really need anything as our 897 * information is already up to data. Convert from NL to 898 * *anything* however should mark ourselves as needing an 899 * update */ 900 if (lockres->l_level == DLM_LOCK_NL && 901 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH) 902 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH); 903 904 lockres->l_level = lockres->l_requested; 905 906 /* 907 * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing 908 * the OCFS2_LOCK_BUSY flag to prevent the dc thread from 909 * downconverting the lock before the upconvert has fully completed. 910 * Do not prevent the dc thread from downconverting if NONBLOCK lock 911 * had already returned. 912 */ 913 if (!(lockres->l_flags & OCFS2_LOCK_NONBLOCK_FINISHED)) 914 lockres_or_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING); 915 else 916 lockres_clear_flags(lockres, OCFS2_LOCK_NONBLOCK_FINISHED); 917 918 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY); 919 } 920 921 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres) 922 { 923 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY))); 924 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED); 925 926 if (lockres->l_requested > DLM_LOCK_NL && 927 !(lockres->l_flags & OCFS2_LOCK_LOCAL) && 928 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH) 929 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH); 930 931 lockres->l_level = lockres->l_requested; 932 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED); 933 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY); 934 } 935 936 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, 937 int level) 938 { 939 int needs_downconvert = 0; 940 941 assert_spin_locked(&lockres->l_lock); 942 943 if (level > lockres->l_blocking) { 944 /* only schedule a downconvert if we haven't already scheduled 945 * one that goes low enough to satisfy the level we're 946 * blocking. this also catches the case where we get 947 * duplicate BASTs */ 948 if (ocfs2_highest_compat_lock_level(level) < 949 ocfs2_highest_compat_lock_level(lockres->l_blocking)) 950 needs_downconvert = 1; 951 952 lockres->l_blocking = level; 953 } 954 955 mlog(ML_BASTS, "lockres %s, block %d, level %d, l_block %d, dwn %d\n", 956 lockres->l_name, level, lockres->l_level, lockres->l_blocking, 957 needs_downconvert); 958 959 if (needs_downconvert) 960 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED); 961 mlog(0, "needs_downconvert = %d\n", needs_downconvert); 962 return needs_downconvert; 963 } 964 965 /* 966 * OCFS2_LOCK_PENDING and l_pending_gen. 967 * 968 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting 969 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock() 970 * for more details on the race. 971 * 972 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces 973 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock() 974 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear 975 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns, 976 * the caller is going to try to clear PENDING again. If nothing else is 977 * happening, __lockres_clear_pending() sees PENDING is unset and does 978 * nothing. 979 * 980 * But what if another path (eg downconvert thread) has just started a 981 * new locking action? The other path has re-set PENDING. Our path 982 * cannot clear PENDING, because that will re-open the original race 983 * window. 984 * 985 * [Example] 986 * 987 * ocfs2_meta_lock() 988 * ocfs2_cluster_lock() 989 * set BUSY 990 * set PENDING 991 * drop l_lock 992 * ocfs2_dlm_lock() 993 * ocfs2_locking_ast() ocfs2_downconvert_thread() 994 * clear PENDING ocfs2_unblock_lock() 995 * take_l_lock 996 * !BUSY 997 * ocfs2_prepare_downconvert() 998 * set BUSY 999 * set PENDING 1000 * drop l_lock 1001 * take l_lock 1002 * clear PENDING 1003 * drop l_lock 1004 * <window> 1005 * ocfs2_dlm_lock() 1006 * 1007 * So as you can see, we now have a window where l_lock is not held, 1008 * PENDING is not set, and ocfs2_dlm_lock() has not been called. 1009 * 1010 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING 1011 * set by ocfs2_prepare_downconvert(). That wasn't nice. 1012 * 1013 * To solve this we introduce l_pending_gen. A call to 1014 * lockres_clear_pending() will only do so when it is passed a generation 1015 * number that matches the lockres. lockres_set_pending() will return the 1016 * current generation number. When ocfs2_cluster_lock() goes to clear 1017 * PENDING, it passes the generation it got from set_pending(). In our 1018 * example above, the generation numbers will *not* match. Thus, 1019 * ocfs2_cluster_lock() will not clear the PENDING set by 1020 * ocfs2_prepare_downconvert(). 1021 */ 1022 1023 /* Unlocked version for ocfs2_locking_ast() */ 1024 static void __lockres_clear_pending(struct ocfs2_lock_res *lockres, 1025 unsigned int generation, 1026 struct ocfs2_super *osb) 1027 { 1028 assert_spin_locked(&lockres->l_lock); 1029 1030 /* 1031 * The ast and locking functions can race us here. The winner 1032 * will clear pending, the loser will not. 1033 */ 1034 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) || 1035 (lockres->l_pending_gen != generation)) 1036 return; 1037 1038 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING); 1039 lockres->l_pending_gen++; 1040 1041 /* 1042 * The downconvert thread may have skipped us because we 1043 * were PENDING. Wake it up. 1044 */ 1045 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) 1046 ocfs2_wake_downconvert_thread(osb); 1047 } 1048 1049 /* Locked version for callers of ocfs2_dlm_lock() */ 1050 static void lockres_clear_pending(struct ocfs2_lock_res *lockres, 1051 unsigned int generation, 1052 struct ocfs2_super *osb) 1053 { 1054 unsigned long flags; 1055 1056 spin_lock_irqsave(&lockres->l_lock, flags); 1057 __lockres_clear_pending(lockres, generation, osb); 1058 spin_unlock_irqrestore(&lockres->l_lock, flags); 1059 } 1060 1061 static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres) 1062 { 1063 assert_spin_locked(&lockres->l_lock); 1064 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY)); 1065 1066 lockres_or_flags(lockres, OCFS2_LOCK_PENDING); 1067 1068 return lockres->l_pending_gen; 1069 } 1070 1071 static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb *lksb, int level) 1072 { 1073 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb); 1074 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres); 1075 int needs_downconvert; 1076 unsigned long flags; 1077 1078 BUG_ON(level <= DLM_LOCK_NL); 1079 1080 mlog(ML_BASTS, "BAST fired for lockres %s, blocking %d, level %d, " 1081 "type %s\n", lockres->l_name, level, lockres->l_level, 1082 ocfs2_lock_type_string(lockres->l_type)); 1083 1084 /* 1085 * We can skip the bast for locks which don't enable caching - 1086 * they'll be dropped at the earliest possible time anyway. 1087 */ 1088 if (lockres->l_flags & OCFS2_LOCK_NOCACHE) 1089 return; 1090 1091 spin_lock_irqsave(&lockres->l_lock, flags); 1092 needs_downconvert = ocfs2_generic_handle_bast(lockres, level); 1093 if (needs_downconvert) 1094 ocfs2_schedule_blocked_lock(osb, lockres); 1095 spin_unlock_irqrestore(&lockres->l_lock, flags); 1096 1097 wake_up(&lockres->l_event); 1098 1099 ocfs2_wake_downconvert_thread(osb); 1100 } 1101 1102 static void ocfs2_locking_ast(struct ocfs2_dlm_lksb *lksb) 1103 { 1104 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb); 1105 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres); 1106 unsigned long flags; 1107 int status; 1108 1109 spin_lock_irqsave(&lockres->l_lock, flags); 1110 1111 status = ocfs2_dlm_lock_status(&lockres->l_lksb); 1112 1113 if (status == -EAGAIN) { 1114 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY); 1115 goto out; 1116 } 1117 1118 if (status) { 1119 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n", 1120 lockres->l_name, status); 1121 spin_unlock_irqrestore(&lockres->l_lock, flags); 1122 return; 1123 } 1124 1125 mlog(ML_BASTS, "AST fired for lockres %s, action %d, unlock %d, " 1126 "level %d => %d\n", lockres->l_name, lockres->l_action, 1127 lockres->l_unlock_action, lockres->l_level, lockres->l_requested); 1128 1129 switch(lockres->l_action) { 1130 case OCFS2_AST_ATTACH: 1131 ocfs2_generic_handle_attach_action(lockres); 1132 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL); 1133 break; 1134 case OCFS2_AST_CONVERT: 1135 ocfs2_generic_handle_convert_action(lockres); 1136 break; 1137 case OCFS2_AST_DOWNCONVERT: 1138 ocfs2_generic_handle_downconvert_action(lockres); 1139 break; 1140 default: 1141 mlog(ML_ERROR, "lockres %s: AST fired with invalid action: %u, " 1142 "flags 0x%lx, unlock: %u\n", 1143 lockres->l_name, lockres->l_action, lockres->l_flags, 1144 lockres->l_unlock_action); 1145 BUG(); 1146 } 1147 out: 1148 /* set it to something invalid so if we get called again we 1149 * can catch it. */ 1150 lockres->l_action = OCFS2_AST_INVALID; 1151 1152 /* Did we try to cancel this lock? Clear that state */ 1153 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) 1154 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID; 1155 1156 /* 1157 * We may have beaten the locking functions here. We certainly 1158 * know that dlm_lock() has been called :-) 1159 * Because we can't have two lock calls in flight at once, we 1160 * can use lockres->l_pending_gen. 1161 */ 1162 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb); 1163 1164 wake_up(&lockres->l_event); 1165 spin_unlock_irqrestore(&lockres->l_lock, flags); 1166 } 1167 1168 static void ocfs2_unlock_ast(struct ocfs2_dlm_lksb *lksb, int error) 1169 { 1170 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb); 1171 unsigned long flags; 1172 1173 mlog(ML_BASTS, "UNLOCK AST fired for lockres %s, action = %d\n", 1174 lockres->l_name, lockres->l_unlock_action); 1175 1176 spin_lock_irqsave(&lockres->l_lock, flags); 1177 if (error) { 1178 mlog(ML_ERROR, "Dlm passes error %d for lock %s, " 1179 "unlock_action %d\n", error, lockres->l_name, 1180 lockres->l_unlock_action); 1181 spin_unlock_irqrestore(&lockres->l_lock, flags); 1182 return; 1183 } 1184 1185 switch(lockres->l_unlock_action) { 1186 case OCFS2_UNLOCK_CANCEL_CONVERT: 1187 mlog(0, "Cancel convert success for %s\n", lockres->l_name); 1188 lockres->l_action = OCFS2_AST_INVALID; 1189 /* Downconvert thread may have requeued this lock, we 1190 * need to wake it. */ 1191 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) 1192 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres)); 1193 break; 1194 case OCFS2_UNLOCK_DROP_LOCK: 1195 lockres->l_level = DLM_LOCK_IV; 1196 break; 1197 default: 1198 BUG(); 1199 } 1200 1201 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY); 1202 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID; 1203 wake_up(&lockres->l_event); 1204 spin_unlock_irqrestore(&lockres->l_lock, flags); 1205 } 1206 1207 /* 1208 * This is the filesystem locking protocol. It provides the lock handling 1209 * hooks for the underlying DLM. It has a maximum version number. 1210 * The version number allows interoperability with systems running at 1211 * the same major number and an equal or smaller minor number. 1212 * 1213 * Whenever the filesystem does new things with locks (adds or removes a 1214 * lock, orders them differently, does different things underneath a lock), 1215 * the version must be changed. The protocol is negotiated when joining 1216 * the dlm domain. A node may join the domain if its major version is 1217 * identical to all other nodes and its minor version is greater than 1218 * or equal to all other nodes. When its minor version is greater than 1219 * the other nodes, it will run at the minor version specified by the 1220 * other nodes. 1221 * 1222 * If a locking change is made that will not be compatible with older 1223 * versions, the major number must be increased and the minor version set 1224 * to zero. If a change merely adds a behavior that can be disabled when 1225 * speaking to older versions, the minor version must be increased. If a 1226 * change adds a fully backwards compatible change (eg, LVB changes that 1227 * are just ignored by older versions), the version does not need to be 1228 * updated. 1229 */ 1230 static struct ocfs2_locking_protocol lproto = { 1231 .lp_max_version = { 1232 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR, 1233 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR, 1234 }, 1235 .lp_lock_ast = ocfs2_locking_ast, 1236 .lp_blocking_ast = ocfs2_blocking_ast, 1237 .lp_unlock_ast = ocfs2_unlock_ast, 1238 }; 1239 1240 void ocfs2_set_locking_protocol(void) 1241 { 1242 ocfs2_stack_glue_set_max_proto_version(&lproto.lp_max_version); 1243 } 1244 1245 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres, 1246 int convert) 1247 { 1248 unsigned long flags; 1249 1250 spin_lock_irqsave(&lockres->l_lock, flags); 1251 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY); 1252 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING); 1253 if (convert) 1254 lockres->l_action = OCFS2_AST_INVALID; 1255 else 1256 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID; 1257 spin_unlock_irqrestore(&lockres->l_lock, flags); 1258 1259 wake_up(&lockres->l_event); 1260 } 1261 1262 /* Note: If we detect another process working on the lock (i.e., 1263 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller 1264 * to do the right thing in that case. 1265 */ 1266 static int ocfs2_lock_create(struct ocfs2_super *osb, 1267 struct ocfs2_lock_res *lockres, 1268 int level, 1269 u32 dlm_flags) 1270 { 1271 int ret = 0; 1272 unsigned long flags; 1273 unsigned int gen; 1274 1275 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level, 1276 dlm_flags); 1277 1278 spin_lock_irqsave(&lockres->l_lock, flags); 1279 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) || 1280 (lockres->l_flags & OCFS2_LOCK_BUSY)) { 1281 spin_unlock_irqrestore(&lockres->l_lock, flags); 1282 goto bail; 1283 } 1284 1285 lockres->l_action = OCFS2_AST_ATTACH; 1286 lockres->l_requested = level; 1287 lockres_or_flags(lockres, OCFS2_LOCK_BUSY); 1288 gen = lockres_set_pending(lockres); 1289 spin_unlock_irqrestore(&lockres->l_lock, flags); 1290 1291 ret = ocfs2_dlm_lock(osb->cconn, 1292 level, 1293 &lockres->l_lksb, 1294 dlm_flags, 1295 lockres->l_name, 1296 OCFS2_LOCK_ID_MAX_LEN - 1); 1297 lockres_clear_pending(lockres, gen, osb); 1298 if (ret) { 1299 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres); 1300 ocfs2_recover_from_dlm_error(lockres, 1); 1301 } 1302 1303 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name); 1304 1305 bail: 1306 return ret; 1307 } 1308 1309 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres, 1310 int flag) 1311 { 1312 unsigned long flags; 1313 int ret; 1314 1315 spin_lock_irqsave(&lockres->l_lock, flags); 1316 ret = lockres->l_flags & flag; 1317 spin_unlock_irqrestore(&lockres->l_lock, flags); 1318 1319 return ret; 1320 } 1321 1322 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres) 1323 1324 { 1325 wait_event(lockres->l_event, 1326 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY)); 1327 } 1328 1329 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres) 1330 1331 { 1332 wait_event(lockres->l_event, 1333 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING)); 1334 } 1335 1336 /* predict what lock level we'll be dropping down to on behalf 1337 * of another node, and return true if the currently wanted 1338 * level will be compatible with it. */ 1339 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres, 1340 int wanted) 1341 { 1342 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED)); 1343 1344 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking); 1345 } 1346 1347 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw) 1348 { 1349 INIT_LIST_HEAD(&mw->mw_item); 1350 init_completion(&mw->mw_complete); 1351 ocfs2_init_start_time(mw); 1352 } 1353 1354 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw) 1355 { 1356 wait_for_completion(&mw->mw_complete); 1357 /* Re-arm the completion in case we want to wait on it again */ 1358 reinit_completion(&mw->mw_complete); 1359 return mw->mw_status; 1360 } 1361 1362 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres, 1363 struct ocfs2_mask_waiter *mw, 1364 unsigned long mask, 1365 unsigned long goal) 1366 { 1367 BUG_ON(!list_empty(&mw->mw_item)); 1368 1369 assert_spin_locked(&lockres->l_lock); 1370 1371 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters); 1372 mw->mw_mask = mask; 1373 mw->mw_goal = goal; 1374 } 1375 1376 /* returns 0 if the mw that was removed was already satisfied, -EBUSY 1377 * if the mask still hadn't reached its goal */ 1378 static int __lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres, 1379 struct ocfs2_mask_waiter *mw) 1380 { 1381 int ret = 0; 1382 1383 assert_spin_locked(&lockres->l_lock); 1384 if (!list_empty(&mw->mw_item)) { 1385 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal) 1386 ret = -EBUSY; 1387 1388 list_del_init(&mw->mw_item); 1389 init_completion(&mw->mw_complete); 1390 } 1391 1392 return ret; 1393 } 1394 1395 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres, 1396 struct ocfs2_mask_waiter *mw) 1397 { 1398 unsigned long flags; 1399 int ret = 0; 1400 1401 spin_lock_irqsave(&lockres->l_lock, flags); 1402 ret = __lockres_remove_mask_waiter(lockres, mw); 1403 spin_unlock_irqrestore(&lockres->l_lock, flags); 1404 1405 return ret; 1406 1407 } 1408 1409 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw, 1410 struct ocfs2_lock_res *lockres) 1411 { 1412 int ret; 1413 1414 ret = wait_for_completion_interruptible(&mw->mw_complete); 1415 if (ret) 1416 lockres_remove_mask_waiter(lockres, mw); 1417 else 1418 ret = mw->mw_status; 1419 /* Re-arm the completion in case we want to wait on it again */ 1420 reinit_completion(&mw->mw_complete); 1421 return ret; 1422 } 1423 1424 static int __ocfs2_cluster_lock(struct ocfs2_super *osb, 1425 struct ocfs2_lock_res *lockres, 1426 int level, 1427 u32 lkm_flags, 1428 int arg_flags, 1429 int l_subclass, 1430 unsigned long caller_ip) 1431 { 1432 struct ocfs2_mask_waiter mw; 1433 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR); 1434 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */ 1435 unsigned long flags; 1436 unsigned int gen; 1437 int noqueue_attempted = 0; 1438 int dlm_locked = 0; 1439 int kick_dc = 0; 1440 1441 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED)) { 1442 mlog_errno(-EINVAL); 1443 return -EINVAL; 1444 } 1445 1446 ocfs2_init_mask_waiter(&mw); 1447 1448 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) 1449 lkm_flags |= DLM_LKF_VALBLK; 1450 1451 again: 1452 wait = 0; 1453 1454 spin_lock_irqsave(&lockres->l_lock, flags); 1455 1456 if (catch_signals && signal_pending(current)) { 1457 ret = -ERESTARTSYS; 1458 goto unlock; 1459 } 1460 1461 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING, 1462 "Cluster lock called on freeing lockres %s! flags " 1463 "0x%lx\n", lockres->l_name, lockres->l_flags); 1464 1465 /* We only compare against the currently granted level 1466 * here. If the lock is blocked waiting on a downconvert, 1467 * we'll get caught below. */ 1468 if (lockres->l_flags & OCFS2_LOCK_BUSY && 1469 level > lockres->l_level) { 1470 /* is someone sitting in dlm_lock? If so, wait on 1471 * them. */ 1472 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0); 1473 wait = 1; 1474 goto unlock; 1475 } 1476 1477 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) { 1478 /* 1479 * We've upconverted. If the lock now has a level we can 1480 * work with, we take it. If, however, the lock is not at the 1481 * required level, we go thru the full cycle. One way this could 1482 * happen is if a process requesting an upconvert to PR is 1483 * closely followed by another requesting upconvert to an EX. 1484 * If the process requesting EX lands here, we want it to 1485 * continue attempting to upconvert and let the process 1486 * requesting PR take the lock. 1487 * If multiple processes request upconvert to PR, the first one 1488 * here will take the lock. The others will have to go thru the 1489 * OCFS2_LOCK_BLOCKED check to ensure that there is no pending 1490 * downconvert request. 1491 */ 1492 if (level <= lockres->l_level) 1493 goto update_holders; 1494 } 1495 1496 if (lockres->l_flags & OCFS2_LOCK_BLOCKED && 1497 !ocfs2_may_continue_on_blocked_lock(lockres, level)) { 1498 /* is the lock is currently blocked on behalf of 1499 * another node */ 1500 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0); 1501 wait = 1; 1502 goto unlock; 1503 } 1504 1505 if (level > lockres->l_level) { 1506 if (noqueue_attempted > 0) { 1507 ret = -EAGAIN; 1508 goto unlock; 1509 } 1510 if (lkm_flags & DLM_LKF_NOQUEUE) 1511 noqueue_attempted = 1; 1512 1513 if (lockres->l_action != OCFS2_AST_INVALID) 1514 mlog(ML_ERROR, "lockres %s has action %u pending\n", 1515 lockres->l_name, lockres->l_action); 1516 1517 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) { 1518 lockres->l_action = OCFS2_AST_ATTACH; 1519 lkm_flags &= ~DLM_LKF_CONVERT; 1520 } else { 1521 lockres->l_action = OCFS2_AST_CONVERT; 1522 lkm_flags |= DLM_LKF_CONVERT; 1523 } 1524 1525 lockres->l_requested = level; 1526 lockres_or_flags(lockres, OCFS2_LOCK_BUSY); 1527 gen = lockres_set_pending(lockres); 1528 spin_unlock_irqrestore(&lockres->l_lock, flags); 1529 1530 BUG_ON(level == DLM_LOCK_IV); 1531 BUG_ON(level == DLM_LOCK_NL); 1532 1533 mlog(ML_BASTS, "lockres %s, convert from %d to %d\n", 1534 lockres->l_name, lockres->l_level, level); 1535 1536 /* call dlm_lock to upgrade lock now */ 1537 ret = ocfs2_dlm_lock(osb->cconn, 1538 level, 1539 &lockres->l_lksb, 1540 lkm_flags, 1541 lockres->l_name, 1542 OCFS2_LOCK_ID_MAX_LEN - 1); 1543 lockres_clear_pending(lockres, gen, osb); 1544 if (ret) { 1545 if (!(lkm_flags & DLM_LKF_NOQUEUE) || 1546 (ret != -EAGAIN)) { 1547 ocfs2_log_dlm_error("ocfs2_dlm_lock", 1548 ret, lockres); 1549 } 1550 ocfs2_recover_from_dlm_error(lockres, 1); 1551 goto out; 1552 } 1553 dlm_locked = 1; 1554 1555 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n", 1556 lockres->l_name); 1557 1558 /* At this point we've gone inside the dlm and need to 1559 * complete our work regardless. */ 1560 catch_signals = 0; 1561 1562 /* wait for busy to clear and carry on */ 1563 goto again; 1564 } 1565 1566 update_holders: 1567 /* Ok, if we get here then we're good to go. */ 1568 ocfs2_inc_holders(lockres, level); 1569 1570 ret = 0; 1571 unlock: 1572 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING); 1573 1574 /* ocfs2_unblock_lock reques on seeing OCFS2_LOCK_UPCONVERT_FINISHING */ 1575 kick_dc = (lockres->l_flags & OCFS2_LOCK_BLOCKED); 1576 1577 spin_unlock_irqrestore(&lockres->l_lock, flags); 1578 if (kick_dc) 1579 ocfs2_wake_downconvert_thread(osb); 1580 out: 1581 /* 1582 * This is helping work around a lock inversion between the page lock 1583 * and dlm locks. One path holds the page lock while calling aops 1584 * which block acquiring dlm locks. The voting thread holds dlm 1585 * locks while acquiring page locks while down converting data locks. 1586 * This block is helping an aop path notice the inversion and back 1587 * off to unlock its page lock before trying the dlm lock again. 1588 */ 1589 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK && 1590 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) { 1591 wait = 0; 1592 spin_lock_irqsave(&lockres->l_lock, flags); 1593 if (__lockres_remove_mask_waiter(lockres, &mw)) { 1594 if (dlm_locked) 1595 lockres_or_flags(lockres, 1596 OCFS2_LOCK_NONBLOCK_FINISHED); 1597 spin_unlock_irqrestore(&lockres->l_lock, flags); 1598 ret = -EAGAIN; 1599 } else { 1600 spin_unlock_irqrestore(&lockres->l_lock, flags); 1601 goto again; 1602 } 1603 } 1604 if (wait) { 1605 ret = ocfs2_wait_for_mask(&mw); 1606 if (ret == 0) 1607 goto again; 1608 mlog_errno(ret); 1609 } 1610 ocfs2_update_lock_stats(lockres, level, &mw, ret); 1611 1612 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1613 if (!ret && lockres->l_lockdep_map.key != NULL) { 1614 if (level == DLM_LOCK_PR) 1615 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass, 1616 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE), 1617 caller_ip); 1618 else 1619 rwsem_acquire(&lockres->l_lockdep_map, l_subclass, 1620 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE), 1621 caller_ip); 1622 } 1623 #endif 1624 return ret; 1625 } 1626 1627 static inline int ocfs2_cluster_lock(struct ocfs2_super *osb, 1628 struct ocfs2_lock_res *lockres, 1629 int level, 1630 u32 lkm_flags, 1631 int arg_flags) 1632 { 1633 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags, 1634 0, _RET_IP_); 1635 } 1636 1637 1638 static void __ocfs2_cluster_unlock(struct ocfs2_super *osb, 1639 struct ocfs2_lock_res *lockres, 1640 int level, 1641 unsigned long caller_ip) 1642 { 1643 unsigned long flags; 1644 1645 spin_lock_irqsave(&lockres->l_lock, flags); 1646 ocfs2_dec_holders(lockres, level); 1647 ocfs2_downconvert_on_unlock(osb, lockres); 1648 spin_unlock_irqrestore(&lockres->l_lock, flags); 1649 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1650 if (lockres->l_lockdep_map.key != NULL) 1651 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip); 1652 #endif 1653 } 1654 1655 static int ocfs2_create_new_lock(struct ocfs2_super *osb, 1656 struct ocfs2_lock_res *lockres, 1657 int ex, 1658 int local) 1659 { 1660 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 1661 unsigned long flags; 1662 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0; 1663 1664 spin_lock_irqsave(&lockres->l_lock, flags); 1665 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED); 1666 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL); 1667 spin_unlock_irqrestore(&lockres->l_lock, flags); 1668 1669 return ocfs2_lock_create(osb, lockres, level, lkm_flags); 1670 } 1671 1672 /* Grants us an EX lock on the data and metadata resources, skipping 1673 * the normal cluster directory lookup. Use this ONLY on newly created 1674 * inodes which other nodes can't possibly see, and which haven't been 1675 * hashed in the inode hash yet. This can give us a good performance 1676 * increase as it'll skip the network broadcast normally associated 1677 * with creating a new lock resource. */ 1678 int ocfs2_create_new_inode_locks(struct inode *inode) 1679 { 1680 int ret; 1681 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1682 1683 BUG_ON(!ocfs2_inode_is_new(inode)); 1684 1685 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno); 1686 1687 /* NOTE: That we don't increment any of the holder counts, nor 1688 * do we add anything to a journal handle. Since this is 1689 * supposed to be a new inode which the cluster doesn't know 1690 * about yet, there is no need to. As far as the LVB handling 1691 * is concerned, this is basically like acquiring an EX lock 1692 * on a resource which has an invalid one -- we'll set it 1693 * valid when we release the EX. */ 1694 1695 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1); 1696 if (ret) { 1697 mlog_errno(ret); 1698 goto bail; 1699 } 1700 1701 /* 1702 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they 1703 * don't use a generation in their lock names. 1704 */ 1705 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0); 1706 if (ret) { 1707 mlog_errno(ret); 1708 goto bail; 1709 } 1710 1711 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0); 1712 if (ret) 1713 mlog_errno(ret); 1714 1715 bail: 1716 return ret; 1717 } 1718 1719 int ocfs2_rw_lock(struct inode *inode, int write) 1720 { 1721 int status, level; 1722 struct ocfs2_lock_res *lockres; 1723 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1724 1725 mlog(0, "inode %llu take %s RW lock\n", 1726 (unsigned long long)OCFS2_I(inode)->ip_blkno, 1727 write ? "EXMODE" : "PRMODE"); 1728 1729 if (ocfs2_mount_local(osb)) 1730 return 0; 1731 1732 lockres = &OCFS2_I(inode)->ip_rw_lockres; 1733 1734 level = write ? DLM_LOCK_EX : DLM_LOCK_PR; 1735 1736 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0, 1737 0); 1738 if (status < 0) 1739 mlog_errno(status); 1740 1741 return status; 1742 } 1743 1744 void ocfs2_rw_unlock(struct inode *inode, int write) 1745 { 1746 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR; 1747 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres; 1748 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1749 1750 mlog(0, "inode %llu drop %s RW lock\n", 1751 (unsigned long long)OCFS2_I(inode)->ip_blkno, 1752 write ? "EXMODE" : "PRMODE"); 1753 1754 if (!ocfs2_mount_local(osb)) 1755 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level); 1756 } 1757 1758 /* 1759 * ocfs2_open_lock always get PR mode lock. 1760 */ 1761 int ocfs2_open_lock(struct inode *inode) 1762 { 1763 int status = 0; 1764 struct ocfs2_lock_res *lockres; 1765 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1766 1767 mlog(0, "inode %llu take PRMODE open lock\n", 1768 (unsigned long long)OCFS2_I(inode)->ip_blkno); 1769 1770 if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb)) 1771 goto out; 1772 1773 lockres = &OCFS2_I(inode)->ip_open_lockres; 1774 1775 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, 1776 DLM_LOCK_PR, 0, 0); 1777 if (status < 0) 1778 mlog_errno(status); 1779 1780 out: 1781 return status; 1782 } 1783 1784 int ocfs2_try_open_lock(struct inode *inode, int write) 1785 { 1786 int status = 0, level; 1787 struct ocfs2_lock_res *lockres; 1788 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1789 1790 mlog(0, "inode %llu try to take %s open lock\n", 1791 (unsigned long long)OCFS2_I(inode)->ip_blkno, 1792 write ? "EXMODE" : "PRMODE"); 1793 1794 if (ocfs2_is_hard_readonly(osb)) { 1795 if (write) 1796 status = -EROFS; 1797 goto out; 1798 } 1799 1800 if (ocfs2_mount_local(osb)) 1801 goto out; 1802 1803 lockres = &OCFS2_I(inode)->ip_open_lockres; 1804 1805 level = write ? DLM_LOCK_EX : DLM_LOCK_PR; 1806 1807 /* 1808 * The file system may already holding a PRMODE/EXMODE open lock. 1809 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on 1810 * other nodes and the -EAGAIN will indicate to the caller that 1811 * this inode is still in use. 1812 */ 1813 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, 1814 level, DLM_LKF_NOQUEUE, 0); 1815 1816 out: 1817 return status; 1818 } 1819 1820 /* 1821 * ocfs2_open_unlock unlock PR and EX mode open locks. 1822 */ 1823 void ocfs2_open_unlock(struct inode *inode) 1824 { 1825 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres; 1826 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1827 1828 mlog(0, "inode %llu drop open lock\n", 1829 (unsigned long long)OCFS2_I(inode)->ip_blkno); 1830 1831 if (ocfs2_mount_local(osb)) 1832 goto out; 1833 1834 if(lockres->l_ro_holders) 1835 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, 1836 DLM_LOCK_PR); 1837 if(lockres->l_ex_holders) 1838 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, 1839 DLM_LOCK_EX); 1840 1841 out: 1842 return; 1843 } 1844 1845 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres, 1846 int level) 1847 { 1848 int ret; 1849 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres); 1850 unsigned long flags; 1851 struct ocfs2_mask_waiter mw; 1852 1853 ocfs2_init_mask_waiter(&mw); 1854 1855 retry_cancel: 1856 spin_lock_irqsave(&lockres->l_lock, flags); 1857 if (lockres->l_flags & OCFS2_LOCK_BUSY) { 1858 ret = ocfs2_prepare_cancel_convert(osb, lockres); 1859 if (ret) { 1860 spin_unlock_irqrestore(&lockres->l_lock, flags); 1861 ret = ocfs2_cancel_convert(osb, lockres); 1862 if (ret < 0) { 1863 mlog_errno(ret); 1864 goto out; 1865 } 1866 goto retry_cancel; 1867 } 1868 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0); 1869 spin_unlock_irqrestore(&lockres->l_lock, flags); 1870 1871 ocfs2_wait_for_mask(&mw); 1872 goto retry_cancel; 1873 } 1874 1875 ret = -ERESTARTSYS; 1876 /* 1877 * We may still have gotten the lock, in which case there's no 1878 * point to restarting the syscall. 1879 */ 1880 if (lockres->l_level == level) 1881 ret = 0; 1882 1883 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret, 1884 lockres->l_flags, lockres->l_level, lockres->l_action); 1885 1886 spin_unlock_irqrestore(&lockres->l_lock, flags); 1887 1888 out: 1889 return ret; 1890 } 1891 1892 /* 1893 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of 1894 * flock() calls. The locking approach this requires is sufficiently 1895 * different from all other cluster lock types that we implement a 1896 * separate path to the "low-level" dlm calls. In particular: 1897 * 1898 * - No optimization of lock levels is done - we take at exactly 1899 * what's been requested. 1900 * 1901 * - No lock caching is employed. We immediately downconvert to 1902 * no-lock at unlock time. This also means flock locks never go on 1903 * the blocking list). 1904 * 1905 * - Since userspace can trivially deadlock itself with flock, we make 1906 * sure to allow cancellation of a misbehaving applications flock() 1907 * request. 1908 * 1909 * - Access to any flock lockres doesn't require concurrency, so we 1910 * can simplify the code by requiring the caller to guarantee 1911 * serialization of dlmglue flock calls. 1912 */ 1913 int ocfs2_file_lock(struct file *file, int ex, int trylock) 1914 { 1915 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 1916 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0; 1917 unsigned long flags; 1918 struct ocfs2_file_private *fp = file->private_data; 1919 struct ocfs2_lock_res *lockres = &fp->fp_flock; 1920 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb); 1921 struct ocfs2_mask_waiter mw; 1922 1923 ocfs2_init_mask_waiter(&mw); 1924 1925 if ((lockres->l_flags & OCFS2_LOCK_BUSY) || 1926 (lockres->l_level > DLM_LOCK_NL)) { 1927 mlog(ML_ERROR, 1928 "File lock \"%s\" has busy or locked state: flags: 0x%lx, " 1929 "level: %u\n", lockres->l_name, lockres->l_flags, 1930 lockres->l_level); 1931 return -EINVAL; 1932 } 1933 1934 spin_lock_irqsave(&lockres->l_lock, flags); 1935 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) { 1936 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0); 1937 spin_unlock_irqrestore(&lockres->l_lock, flags); 1938 1939 /* 1940 * Get the lock at NLMODE to start - that way we 1941 * can cancel the upconvert request if need be. 1942 */ 1943 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0); 1944 if (ret < 0) { 1945 mlog_errno(ret); 1946 goto out; 1947 } 1948 1949 ret = ocfs2_wait_for_mask(&mw); 1950 if (ret) { 1951 mlog_errno(ret); 1952 goto out; 1953 } 1954 spin_lock_irqsave(&lockres->l_lock, flags); 1955 } 1956 1957 lockres->l_action = OCFS2_AST_CONVERT; 1958 lkm_flags |= DLM_LKF_CONVERT; 1959 lockres->l_requested = level; 1960 lockres_or_flags(lockres, OCFS2_LOCK_BUSY); 1961 1962 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0); 1963 spin_unlock_irqrestore(&lockres->l_lock, flags); 1964 1965 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags, 1966 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1); 1967 if (ret) { 1968 if (!trylock || (ret != -EAGAIN)) { 1969 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres); 1970 ret = -EINVAL; 1971 } 1972 1973 ocfs2_recover_from_dlm_error(lockres, 1); 1974 lockres_remove_mask_waiter(lockres, &mw); 1975 goto out; 1976 } 1977 1978 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres); 1979 if (ret == -ERESTARTSYS) { 1980 /* 1981 * Userspace can cause deadlock itself with 1982 * flock(). Current behavior locally is to allow the 1983 * deadlock, but abort the system call if a signal is 1984 * received. We follow this example, otherwise a 1985 * poorly written program could sit in kernel until 1986 * reboot. 1987 * 1988 * Handling this is a bit more complicated for Ocfs2 1989 * though. We can't exit this function with an 1990 * outstanding lock request, so a cancel convert is 1991 * required. We intentionally overwrite 'ret' - if the 1992 * cancel fails and the lock was granted, it's easier 1993 * to just bubble success back up to the user. 1994 */ 1995 ret = ocfs2_flock_handle_signal(lockres, level); 1996 } else if (!ret && (level > lockres->l_level)) { 1997 /* Trylock failed asynchronously */ 1998 BUG_ON(!trylock); 1999 ret = -EAGAIN; 2000 } 2001 2002 out: 2003 2004 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n", 2005 lockres->l_name, ex, trylock, ret); 2006 return ret; 2007 } 2008 2009 void ocfs2_file_unlock(struct file *file) 2010 { 2011 int ret; 2012 unsigned int gen; 2013 unsigned long flags; 2014 struct ocfs2_file_private *fp = file->private_data; 2015 struct ocfs2_lock_res *lockres = &fp->fp_flock; 2016 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb); 2017 struct ocfs2_mask_waiter mw; 2018 2019 ocfs2_init_mask_waiter(&mw); 2020 2021 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) 2022 return; 2023 2024 if (lockres->l_level == DLM_LOCK_NL) 2025 return; 2026 2027 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n", 2028 lockres->l_name, lockres->l_flags, lockres->l_level, 2029 lockres->l_action); 2030 2031 spin_lock_irqsave(&lockres->l_lock, flags); 2032 /* 2033 * Fake a blocking ast for the downconvert code. 2034 */ 2035 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED); 2036 lockres->l_blocking = DLM_LOCK_EX; 2037 2038 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL); 2039 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0); 2040 spin_unlock_irqrestore(&lockres->l_lock, flags); 2041 2042 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen); 2043 if (ret) { 2044 mlog_errno(ret); 2045 return; 2046 } 2047 2048 ret = ocfs2_wait_for_mask(&mw); 2049 if (ret) 2050 mlog_errno(ret); 2051 } 2052 2053 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb, 2054 struct ocfs2_lock_res *lockres) 2055 { 2056 int kick = 0; 2057 2058 /* If we know that another node is waiting on our lock, kick 2059 * the downconvert thread * pre-emptively when we reach a release 2060 * condition. */ 2061 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) { 2062 switch(lockres->l_blocking) { 2063 case DLM_LOCK_EX: 2064 if (!lockres->l_ex_holders && !lockres->l_ro_holders) 2065 kick = 1; 2066 break; 2067 case DLM_LOCK_PR: 2068 if (!lockres->l_ex_holders) 2069 kick = 1; 2070 break; 2071 default: 2072 BUG(); 2073 } 2074 } 2075 2076 if (kick) 2077 ocfs2_wake_downconvert_thread(osb); 2078 } 2079 2080 #define OCFS2_SEC_BITS 34 2081 #define OCFS2_SEC_SHIFT (64 - 34) 2082 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1) 2083 2084 /* LVB only has room for 64 bits of time here so we pack it for 2085 * now. */ 2086 static u64 ocfs2_pack_timespec(struct timespec *spec) 2087 { 2088 u64 res; 2089 u64 sec = spec->tv_sec; 2090 u32 nsec = spec->tv_nsec; 2091 2092 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK); 2093 2094 return res; 2095 } 2096 2097 /* Call this with the lockres locked. I am reasonably sure we don't 2098 * need ip_lock in this function as anyone who would be changing those 2099 * values is supposed to be blocked in ocfs2_inode_lock right now. */ 2100 static void __ocfs2_stuff_meta_lvb(struct inode *inode) 2101 { 2102 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2103 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres; 2104 struct ocfs2_meta_lvb *lvb; 2105 2106 lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 2107 2108 /* 2109 * Invalidate the LVB of a deleted inode - this way other 2110 * nodes are forced to go to disk and discover the new inode 2111 * status. 2112 */ 2113 if (oi->ip_flags & OCFS2_INODE_DELETED) { 2114 lvb->lvb_version = 0; 2115 goto out; 2116 } 2117 2118 lvb->lvb_version = OCFS2_LVB_VERSION; 2119 lvb->lvb_isize = cpu_to_be64(i_size_read(inode)); 2120 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters); 2121 lvb->lvb_iuid = cpu_to_be32(i_uid_read(inode)); 2122 lvb->lvb_igid = cpu_to_be32(i_gid_read(inode)); 2123 lvb->lvb_imode = cpu_to_be16(inode->i_mode); 2124 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink); 2125 lvb->lvb_iatime_packed = 2126 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime)); 2127 lvb->lvb_ictime_packed = 2128 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime)); 2129 lvb->lvb_imtime_packed = 2130 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime)); 2131 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr); 2132 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features); 2133 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation); 2134 2135 out: 2136 mlog_meta_lvb(0, lockres); 2137 } 2138 2139 static void ocfs2_unpack_timespec(struct timespec *spec, 2140 u64 packed_time) 2141 { 2142 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT; 2143 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK; 2144 } 2145 2146 static void ocfs2_refresh_inode_from_lvb(struct inode *inode) 2147 { 2148 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2149 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres; 2150 struct ocfs2_meta_lvb *lvb; 2151 2152 mlog_meta_lvb(0, lockres); 2153 2154 lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 2155 2156 /* We're safe here without the lockres lock... */ 2157 spin_lock(&oi->ip_lock); 2158 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters); 2159 i_size_write(inode, be64_to_cpu(lvb->lvb_isize)); 2160 2161 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr); 2162 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures); 2163 ocfs2_set_inode_flags(inode); 2164 2165 /* fast-symlinks are a special case */ 2166 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters) 2167 inode->i_blocks = 0; 2168 else 2169 inode->i_blocks = ocfs2_inode_sector_count(inode); 2170 2171 i_uid_write(inode, be32_to_cpu(lvb->lvb_iuid)); 2172 i_gid_write(inode, be32_to_cpu(lvb->lvb_igid)); 2173 inode->i_mode = be16_to_cpu(lvb->lvb_imode); 2174 set_nlink(inode, be16_to_cpu(lvb->lvb_inlink)); 2175 ocfs2_unpack_timespec(&inode->i_atime, 2176 be64_to_cpu(lvb->lvb_iatime_packed)); 2177 ocfs2_unpack_timespec(&inode->i_mtime, 2178 be64_to_cpu(lvb->lvb_imtime_packed)); 2179 ocfs2_unpack_timespec(&inode->i_ctime, 2180 be64_to_cpu(lvb->lvb_ictime_packed)); 2181 spin_unlock(&oi->ip_lock); 2182 } 2183 2184 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode, 2185 struct ocfs2_lock_res *lockres) 2186 { 2187 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 2188 2189 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) 2190 && lvb->lvb_version == OCFS2_LVB_VERSION 2191 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation) 2192 return 1; 2193 return 0; 2194 } 2195 2196 /* Determine whether a lock resource needs to be refreshed, and 2197 * arbitrate who gets to refresh it. 2198 * 2199 * 0 means no refresh needed. 2200 * 2201 * > 0 means you need to refresh this and you MUST call 2202 * ocfs2_complete_lock_res_refresh afterwards. */ 2203 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres) 2204 { 2205 unsigned long flags; 2206 int status = 0; 2207 2208 refresh_check: 2209 spin_lock_irqsave(&lockres->l_lock, flags); 2210 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) { 2211 spin_unlock_irqrestore(&lockres->l_lock, flags); 2212 goto bail; 2213 } 2214 2215 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) { 2216 spin_unlock_irqrestore(&lockres->l_lock, flags); 2217 2218 ocfs2_wait_on_refreshing_lock(lockres); 2219 goto refresh_check; 2220 } 2221 2222 /* Ok, I'll be the one to refresh this lock. */ 2223 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING); 2224 spin_unlock_irqrestore(&lockres->l_lock, flags); 2225 2226 status = 1; 2227 bail: 2228 mlog(0, "status %d\n", status); 2229 return status; 2230 } 2231 2232 /* If status is non zero, I'll mark it as not being in refresh 2233 * anymroe, but i won't clear the needs refresh flag. */ 2234 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres, 2235 int status) 2236 { 2237 unsigned long flags; 2238 2239 spin_lock_irqsave(&lockres->l_lock, flags); 2240 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING); 2241 if (!status) 2242 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH); 2243 spin_unlock_irqrestore(&lockres->l_lock, flags); 2244 2245 wake_up(&lockres->l_event); 2246 } 2247 2248 /* may or may not return a bh if it went to disk. */ 2249 static int ocfs2_inode_lock_update(struct inode *inode, 2250 struct buffer_head **bh) 2251 { 2252 int status = 0; 2253 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2254 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres; 2255 struct ocfs2_dinode *fe; 2256 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2257 2258 if (ocfs2_mount_local(osb)) 2259 goto bail; 2260 2261 spin_lock(&oi->ip_lock); 2262 if (oi->ip_flags & OCFS2_INODE_DELETED) { 2263 mlog(0, "Orphaned inode %llu was deleted while we " 2264 "were waiting on a lock. ip_flags = 0x%x\n", 2265 (unsigned long long)oi->ip_blkno, oi->ip_flags); 2266 spin_unlock(&oi->ip_lock); 2267 status = -ENOENT; 2268 goto bail; 2269 } 2270 spin_unlock(&oi->ip_lock); 2271 2272 if (!ocfs2_should_refresh_lock_res(lockres)) 2273 goto bail; 2274 2275 /* This will discard any caching information we might have had 2276 * for the inode metadata. */ 2277 ocfs2_metadata_cache_purge(INODE_CACHE(inode)); 2278 2279 ocfs2_extent_map_trunc(inode, 0); 2280 2281 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) { 2282 mlog(0, "Trusting LVB on inode %llu\n", 2283 (unsigned long long)oi->ip_blkno); 2284 ocfs2_refresh_inode_from_lvb(inode); 2285 } else { 2286 /* Boo, we have to go to disk. */ 2287 /* read bh, cast, ocfs2_refresh_inode */ 2288 status = ocfs2_read_inode_block(inode, bh); 2289 if (status < 0) { 2290 mlog_errno(status); 2291 goto bail_refresh; 2292 } 2293 fe = (struct ocfs2_dinode *) (*bh)->b_data; 2294 2295 /* This is a good chance to make sure we're not 2296 * locking an invalid object. ocfs2_read_inode_block() 2297 * already checked that the inode block is sane. 2298 * 2299 * We bug on a stale inode here because we checked 2300 * above whether it was wiped from disk. The wiping 2301 * node provides a guarantee that we receive that 2302 * message and can mark the inode before dropping any 2303 * locks associated with it. */ 2304 mlog_bug_on_msg(inode->i_generation != 2305 le32_to_cpu(fe->i_generation), 2306 "Invalid dinode %llu disk generation: %u " 2307 "inode->i_generation: %u\n", 2308 (unsigned long long)oi->ip_blkno, 2309 le32_to_cpu(fe->i_generation), 2310 inode->i_generation); 2311 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) || 2312 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)), 2313 "Stale dinode %llu dtime: %llu flags: 0x%x\n", 2314 (unsigned long long)oi->ip_blkno, 2315 (unsigned long long)le64_to_cpu(fe->i_dtime), 2316 le32_to_cpu(fe->i_flags)); 2317 2318 ocfs2_refresh_inode(inode, fe); 2319 ocfs2_track_lock_refresh(lockres); 2320 } 2321 2322 status = 0; 2323 bail_refresh: 2324 ocfs2_complete_lock_res_refresh(lockres, status); 2325 bail: 2326 return status; 2327 } 2328 2329 static int ocfs2_assign_bh(struct inode *inode, 2330 struct buffer_head **ret_bh, 2331 struct buffer_head *passed_bh) 2332 { 2333 int status; 2334 2335 if (passed_bh) { 2336 /* Ok, the update went to disk for us, use the 2337 * returned bh. */ 2338 *ret_bh = passed_bh; 2339 get_bh(*ret_bh); 2340 2341 return 0; 2342 } 2343 2344 status = ocfs2_read_inode_block(inode, ret_bh); 2345 if (status < 0) 2346 mlog_errno(status); 2347 2348 return status; 2349 } 2350 2351 /* 2352 * returns < 0 error if the callback will never be called, otherwise 2353 * the result of the lock will be communicated via the callback. 2354 */ 2355 int ocfs2_inode_lock_full_nested(struct inode *inode, 2356 struct buffer_head **ret_bh, 2357 int ex, 2358 int arg_flags, 2359 int subclass) 2360 { 2361 int status, level, acquired; 2362 u32 dlm_flags; 2363 struct ocfs2_lock_res *lockres = NULL; 2364 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2365 struct buffer_head *local_bh = NULL; 2366 2367 mlog(0, "inode %llu, take %s META lock\n", 2368 (unsigned long long)OCFS2_I(inode)->ip_blkno, 2369 ex ? "EXMODE" : "PRMODE"); 2370 2371 status = 0; 2372 acquired = 0; 2373 /* We'll allow faking a readonly metadata lock for 2374 * rodevices. */ 2375 if (ocfs2_is_hard_readonly(osb)) { 2376 if (ex) 2377 status = -EROFS; 2378 goto getbh; 2379 } 2380 2381 if ((arg_flags & OCFS2_META_LOCK_GETBH) || 2382 ocfs2_mount_local(osb)) 2383 goto update; 2384 2385 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY)) 2386 ocfs2_wait_for_recovery(osb); 2387 2388 lockres = &OCFS2_I(inode)->ip_inode_lockres; 2389 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 2390 dlm_flags = 0; 2391 if (arg_flags & OCFS2_META_LOCK_NOQUEUE) 2392 dlm_flags |= DLM_LKF_NOQUEUE; 2393 2394 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags, 2395 arg_flags, subclass, _RET_IP_); 2396 if (status < 0) { 2397 if (status != -EAGAIN) 2398 mlog_errno(status); 2399 goto bail; 2400 } 2401 2402 /* Notify the error cleanup path to drop the cluster lock. */ 2403 acquired = 1; 2404 2405 /* We wait twice because a node may have died while we were in 2406 * the lower dlm layers. The second time though, we've 2407 * committed to owning this lock so we don't allow signals to 2408 * abort the operation. */ 2409 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY)) 2410 ocfs2_wait_for_recovery(osb); 2411 2412 update: 2413 /* 2414 * We only see this flag if we're being called from 2415 * ocfs2_read_locked_inode(). It means we're locking an inode 2416 * which hasn't been populated yet, so clear the refresh flag 2417 * and let the caller handle it. 2418 */ 2419 if (inode->i_state & I_NEW) { 2420 status = 0; 2421 if (lockres) 2422 ocfs2_complete_lock_res_refresh(lockres, 0); 2423 goto bail; 2424 } 2425 2426 /* This is fun. The caller may want a bh back, or it may 2427 * not. ocfs2_inode_lock_update definitely wants one in, but 2428 * may or may not read one, depending on what's in the 2429 * LVB. The result of all of this is that we've *only* gone to 2430 * disk if we have to, so the complexity is worthwhile. */ 2431 status = ocfs2_inode_lock_update(inode, &local_bh); 2432 if (status < 0) { 2433 if (status != -ENOENT) 2434 mlog_errno(status); 2435 goto bail; 2436 } 2437 getbh: 2438 if (ret_bh) { 2439 status = ocfs2_assign_bh(inode, ret_bh, local_bh); 2440 if (status < 0) { 2441 mlog_errno(status); 2442 goto bail; 2443 } 2444 } 2445 2446 bail: 2447 if (status < 0) { 2448 if (ret_bh && (*ret_bh)) { 2449 brelse(*ret_bh); 2450 *ret_bh = NULL; 2451 } 2452 if (acquired) 2453 ocfs2_inode_unlock(inode, ex); 2454 } 2455 2456 if (local_bh) 2457 brelse(local_bh); 2458 2459 return status; 2460 } 2461 2462 /* 2463 * This is working around a lock inversion between tasks acquiring DLM 2464 * locks while holding a page lock and the downconvert thread which 2465 * blocks dlm lock acquiry while acquiring page locks. 2466 * 2467 * ** These _with_page variantes are only intended to be called from aop 2468 * methods that hold page locks and return a very specific *positive* error 2469 * code that aop methods pass up to the VFS -- test for errors with != 0. ** 2470 * 2471 * The DLM is called such that it returns -EAGAIN if it would have 2472 * blocked waiting for the downconvert thread. In that case we unlock 2473 * our page so the downconvert thread can make progress. Once we've 2474 * done this we have to return AOP_TRUNCATED_PAGE so the aop method 2475 * that called us can bubble that back up into the VFS who will then 2476 * immediately retry the aop call. 2477 */ 2478 int ocfs2_inode_lock_with_page(struct inode *inode, 2479 struct buffer_head **ret_bh, 2480 int ex, 2481 struct page *page) 2482 { 2483 int ret; 2484 2485 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK); 2486 if (ret == -EAGAIN) { 2487 unlock_page(page); 2488 ret = AOP_TRUNCATED_PAGE; 2489 } 2490 2491 return ret; 2492 } 2493 2494 int ocfs2_inode_lock_atime(struct inode *inode, 2495 struct vfsmount *vfsmnt, 2496 int *level) 2497 { 2498 int ret; 2499 2500 ret = ocfs2_inode_lock(inode, NULL, 0); 2501 if (ret < 0) { 2502 mlog_errno(ret); 2503 return ret; 2504 } 2505 2506 /* 2507 * If we should update atime, we will get EX lock, 2508 * otherwise we just get PR lock. 2509 */ 2510 if (ocfs2_should_update_atime(inode, vfsmnt)) { 2511 struct buffer_head *bh = NULL; 2512 2513 ocfs2_inode_unlock(inode, 0); 2514 ret = ocfs2_inode_lock(inode, &bh, 1); 2515 if (ret < 0) { 2516 mlog_errno(ret); 2517 return ret; 2518 } 2519 *level = 1; 2520 if (ocfs2_should_update_atime(inode, vfsmnt)) 2521 ocfs2_update_inode_atime(inode, bh); 2522 if (bh) 2523 brelse(bh); 2524 } else 2525 *level = 0; 2526 2527 return ret; 2528 } 2529 2530 void ocfs2_inode_unlock(struct inode *inode, 2531 int ex) 2532 { 2533 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 2534 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres; 2535 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2536 2537 mlog(0, "inode %llu drop %s META lock\n", 2538 (unsigned long long)OCFS2_I(inode)->ip_blkno, 2539 ex ? "EXMODE" : "PRMODE"); 2540 2541 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) && 2542 !ocfs2_mount_local(osb)) 2543 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level); 2544 } 2545 2546 /* 2547 * This _tracker variantes are introduced to deal with the recursive cluster 2548 * locking issue. The idea is to keep track of a lock holder on the stack of 2549 * the current process. If there's a lock holder on the stack, we know the 2550 * task context is already protected by cluster locking. Currently, they're 2551 * used in some VFS entry routines. 2552 * 2553 * return < 0 on error, return == 0 if there's no lock holder on the stack 2554 * before this call, return == 1 if this call would be a recursive locking. 2555 */ 2556 int ocfs2_inode_lock_tracker(struct inode *inode, 2557 struct buffer_head **ret_bh, 2558 int ex, 2559 struct ocfs2_lock_holder *oh) 2560 { 2561 int status; 2562 int arg_flags = 0, has_locked; 2563 struct ocfs2_lock_res *lockres; 2564 2565 lockres = &OCFS2_I(inode)->ip_inode_lockres; 2566 has_locked = ocfs2_is_locked_by_me(lockres); 2567 /* Just get buffer head if the cluster lock has been taken */ 2568 if (has_locked) 2569 arg_flags = OCFS2_META_LOCK_GETBH; 2570 2571 if (likely(!has_locked || ret_bh)) { 2572 status = ocfs2_inode_lock_full(inode, ret_bh, ex, arg_flags); 2573 if (status < 0) { 2574 if (status != -ENOENT) 2575 mlog_errno(status); 2576 return status; 2577 } 2578 } 2579 if (!has_locked) 2580 ocfs2_add_holder(lockres, oh); 2581 2582 return has_locked; 2583 } 2584 2585 void ocfs2_inode_unlock_tracker(struct inode *inode, 2586 int ex, 2587 struct ocfs2_lock_holder *oh, 2588 int had_lock) 2589 { 2590 struct ocfs2_lock_res *lockres; 2591 2592 lockres = &OCFS2_I(inode)->ip_inode_lockres; 2593 if (!had_lock) { 2594 ocfs2_remove_holder(lockres, oh); 2595 ocfs2_inode_unlock(inode, ex); 2596 } 2597 } 2598 2599 int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno) 2600 { 2601 struct ocfs2_lock_res *lockres; 2602 struct ocfs2_orphan_scan_lvb *lvb; 2603 int status = 0; 2604 2605 if (ocfs2_is_hard_readonly(osb)) 2606 return -EROFS; 2607 2608 if (ocfs2_mount_local(osb)) 2609 return 0; 2610 2611 lockres = &osb->osb_orphan_scan.os_lockres; 2612 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0); 2613 if (status < 0) 2614 return status; 2615 2616 lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 2617 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) && 2618 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION) 2619 *seqno = be32_to_cpu(lvb->lvb_os_seqno); 2620 else 2621 *seqno = osb->osb_orphan_scan.os_seqno + 1; 2622 2623 return status; 2624 } 2625 2626 void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno) 2627 { 2628 struct ocfs2_lock_res *lockres; 2629 struct ocfs2_orphan_scan_lvb *lvb; 2630 2631 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) { 2632 lockres = &osb->osb_orphan_scan.os_lockres; 2633 lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 2634 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION; 2635 lvb->lvb_os_seqno = cpu_to_be32(seqno); 2636 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX); 2637 } 2638 } 2639 2640 int ocfs2_super_lock(struct ocfs2_super *osb, 2641 int ex) 2642 { 2643 int status = 0; 2644 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 2645 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres; 2646 2647 if (ocfs2_is_hard_readonly(osb)) 2648 return -EROFS; 2649 2650 if (ocfs2_mount_local(osb)) 2651 goto bail; 2652 2653 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0); 2654 if (status < 0) { 2655 mlog_errno(status); 2656 goto bail; 2657 } 2658 2659 /* The super block lock path is really in the best position to 2660 * know when resources covered by the lock need to be 2661 * refreshed, so we do it here. Of course, making sense of 2662 * everything is up to the caller :) */ 2663 status = ocfs2_should_refresh_lock_res(lockres); 2664 if (status) { 2665 status = ocfs2_refresh_slot_info(osb); 2666 2667 ocfs2_complete_lock_res_refresh(lockres, status); 2668 2669 if (status < 0) { 2670 ocfs2_cluster_unlock(osb, lockres, level); 2671 mlog_errno(status); 2672 } 2673 ocfs2_track_lock_refresh(lockres); 2674 } 2675 bail: 2676 return status; 2677 } 2678 2679 void ocfs2_super_unlock(struct ocfs2_super *osb, 2680 int ex) 2681 { 2682 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 2683 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres; 2684 2685 if (!ocfs2_mount_local(osb)) 2686 ocfs2_cluster_unlock(osb, lockres, level); 2687 } 2688 2689 int ocfs2_rename_lock(struct ocfs2_super *osb) 2690 { 2691 int status; 2692 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres; 2693 2694 if (ocfs2_is_hard_readonly(osb)) 2695 return -EROFS; 2696 2697 if (ocfs2_mount_local(osb)) 2698 return 0; 2699 2700 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0); 2701 if (status < 0) 2702 mlog_errno(status); 2703 2704 return status; 2705 } 2706 2707 void ocfs2_rename_unlock(struct ocfs2_super *osb) 2708 { 2709 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres; 2710 2711 if (!ocfs2_mount_local(osb)) 2712 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX); 2713 } 2714 2715 int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex) 2716 { 2717 int status; 2718 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres; 2719 2720 if (ocfs2_is_hard_readonly(osb)) 2721 return -EROFS; 2722 2723 if (ocfs2_mount_local(osb)) 2724 return 0; 2725 2726 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE, 2727 0, 0); 2728 if (status < 0) 2729 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status); 2730 2731 return status; 2732 } 2733 2734 void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex) 2735 { 2736 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres; 2737 2738 if (!ocfs2_mount_local(osb)) 2739 ocfs2_cluster_unlock(osb, lockres, 2740 ex ? LKM_EXMODE : LKM_PRMODE); 2741 } 2742 2743 int ocfs2_dentry_lock(struct dentry *dentry, int ex) 2744 { 2745 int ret; 2746 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 2747 struct ocfs2_dentry_lock *dl = dentry->d_fsdata; 2748 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); 2749 2750 BUG_ON(!dl); 2751 2752 if (ocfs2_is_hard_readonly(osb)) { 2753 if (ex) 2754 return -EROFS; 2755 return 0; 2756 } 2757 2758 if (ocfs2_mount_local(osb)) 2759 return 0; 2760 2761 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0); 2762 if (ret < 0) 2763 mlog_errno(ret); 2764 2765 return ret; 2766 } 2767 2768 void ocfs2_dentry_unlock(struct dentry *dentry, int ex) 2769 { 2770 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 2771 struct ocfs2_dentry_lock *dl = dentry->d_fsdata; 2772 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); 2773 2774 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) 2775 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level); 2776 } 2777 2778 /* Reference counting of the dlm debug structure. We want this because 2779 * open references on the debug inodes can live on after a mount, so 2780 * we can't rely on the ocfs2_super to always exist. */ 2781 static void ocfs2_dlm_debug_free(struct kref *kref) 2782 { 2783 struct ocfs2_dlm_debug *dlm_debug; 2784 2785 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt); 2786 2787 kfree(dlm_debug); 2788 } 2789 2790 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug) 2791 { 2792 if (dlm_debug) 2793 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free); 2794 } 2795 2796 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug) 2797 { 2798 kref_get(&debug->d_refcnt); 2799 } 2800 2801 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void) 2802 { 2803 struct ocfs2_dlm_debug *dlm_debug; 2804 2805 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL); 2806 if (!dlm_debug) { 2807 mlog_errno(-ENOMEM); 2808 goto out; 2809 } 2810 2811 kref_init(&dlm_debug->d_refcnt); 2812 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking); 2813 dlm_debug->d_locking_state = NULL; 2814 out: 2815 return dlm_debug; 2816 } 2817 2818 /* Access to this is arbitrated for us via seq_file->sem. */ 2819 struct ocfs2_dlm_seq_priv { 2820 struct ocfs2_dlm_debug *p_dlm_debug; 2821 struct ocfs2_lock_res p_iter_res; 2822 struct ocfs2_lock_res p_tmp_res; 2823 }; 2824 2825 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start, 2826 struct ocfs2_dlm_seq_priv *priv) 2827 { 2828 struct ocfs2_lock_res *iter, *ret = NULL; 2829 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug; 2830 2831 assert_spin_locked(&ocfs2_dlm_tracking_lock); 2832 2833 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) { 2834 /* discover the head of the list */ 2835 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) { 2836 mlog(0, "End of list found, %p\n", ret); 2837 break; 2838 } 2839 2840 /* We track our "dummy" iteration lockres' by a NULL 2841 * l_ops field. */ 2842 if (iter->l_ops != NULL) { 2843 ret = iter; 2844 break; 2845 } 2846 } 2847 2848 return ret; 2849 } 2850 2851 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos) 2852 { 2853 struct ocfs2_dlm_seq_priv *priv = m->private; 2854 struct ocfs2_lock_res *iter; 2855 2856 spin_lock(&ocfs2_dlm_tracking_lock); 2857 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv); 2858 if (iter) { 2859 /* Since lockres' have the lifetime of their container 2860 * (which can be inodes, ocfs2_supers, etc) we want to 2861 * copy this out to a temporary lockres while still 2862 * under the spinlock. Obviously after this we can't 2863 * trust any pointers on the copy returned, but that's 2864 * ok as the information we want isn't typically held 2865 * in them. */ 2866 priv->p_tmp_res = *iter; 2867 iter = &priv->p_tmp_res; 2868 } 2869 spin_unlock(&ocfs2_dlm_tracking_lock); 2870 2871 return iter; 2872 } 2873 2874 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v) 2875 { 2876 } 2877 2878 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos) 2879 { 2880 struct ocfs2_dlm_seq_priv *priv = m->private; 2881 struct ocfs2_lock_res *iter = v; 2882 struct ocfs2_lock_res *dummy = &priv->p_iter_res; 2883 2884 spin_lock(&ocfs2_dlm_tracking_lock); 2885 iter = ocfs2_dlm_next_res(iter, priv); 2886 list_del_init(&dummy->l_debug_list); 2887 if (iter) { 2888 list_add(&dummy->l_debug_list, &iter->l_debug_list); 2889 priv->p_tmp_res = *iter; 2890 iter = &priv->p_tmp_res; 2891 } 2892 spin_unlock(&ocfs2_dlm_tracking_lock); 2893 2894 return iter; 2895 } 2896 2897 /* 2898 * Version is used by debugfs.ocfs2 to determine the format being used 2899 * 2900 * New in version 2 2901 * - Lock stats printed 2902 * New in version 3 2903 * - Max time in lock stats is in usecs (instead of nsecs) 2904 */ 2905 #define OCFS2_DLM_DEBUG_STR_VERSION 3 2906 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v) 2907 { 2908 int i; 2909 char *lvb; 2910 struct ocfs2_lock_res *lockres = v; 2911 2912 if (!lockres) 2913 return -EINVAL; 2914 2915 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION); 2916 2917 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY) 2918 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1, 2919 lockres->l_name, 2920 (unsigned int)ocfs2_get_dentry_lock_ino(lockres)); 2921 else 2922 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name); 2923 2924 seq_printf(m, "%d\t" 2925 "0x%lx\t" 2926 "0x%x\t" 2927 "0x%x\t" 2928 "%u\t" 2929 "%u\t" 2930 "%d\t" 2931 "%d\t", 2932 lockres->l_level, 2933 lockres->l_flags, 2934 lockres->l_action, 2935 lockres->l_unlock_action, 2936 lockres->l_ro_holders, 2937 lockres->l_ex_holders, 2938 lockres->l_requested, 2939 lockres->l_blocking); 2940 2941 /* Dump the raw LVB */ 2942 lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 2943 for(i = 0; i < DLM_LVB_LEN; i++) 2944 seq_printf(m, "0x%x\t", lvb[i]); 2945 2946 #ifdef CONFIG_OCFS2_FS_STATS 2947 # define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets) 2948 # define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets) 2949 # define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail) 2950 # define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail) 2951 # define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total) 2952 # define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total) 2953 # define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max) 2954 # define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max) 2955 # define lock_refresh(_l) ((_l)->l_lock_refresh) 2956 #else 2957 # define lock_num_prmode(_l) (0) 2958 # define lock_num_exmode(_l) (0) 2959 # define lock_num_prmode_failed(_l) (0) 2960 # define lock_num_exmode_failed(_l) (0) 2961 # define lock_total_prmode(_l) (0ULL) 2962 # define lock_total_exmode(_l) (0ULL) 2963 # define lock_max_prmode(_l) (0) 2964 # define lock_max_exmode(_l) (0) 2965 # define lock_refresh(_l) (0) 2966 #endif 2967 /* The following seq_print was added in version 2 of this output */ 2968 seq_printf(m, "%u\t" 2969 "%u\t" 2970 "%u\t" 2971 "%u\t" 2972 "%llu\t" 2973 "%llu\t" 2974 "%u\t" 2975 "%u\t" 2976 "%u\t", 2977 lock_num_prmode(lockres), 2978 lock_num_exmode(lockres), 2979 lock_num_prmode_failed(lockres), 2980 lock_num_exmode_failed(lockres), 2981 lock_total_prmode(lockres), 2982 lock_total_exmode(lockres), 2983 lock_max_prmode(lockres), 2984 lock_max_exmode(lockres), 2985 lock_refresh(lockres)); 2986 2987 /* End the line */ 2988 seq_printf(m, "\n"); 2989 return 0; 2990 } 2991 2992 static const struct seq_operations ocfs2_dlm_seq_ops = { 2993 .start = ocfs2_dlm_seq_start, 2994 .stop = ocfs2_dlm_seq_stop, 2995 .next = ocfs2_dlm_seq_next, 2996 .show = ocfs2_dlm_seq_show, 2997 }; 2998 2999 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file) 3000 { 3001 struct seq_file *seq = file->private_data; 3002 struct ocfs2_dlm_seq_priv *priv = seq->private; 3003 struct ocfs2_lock_res *res = &priv->p_iter_res; 3004 3005 ocfs2_remove_lockres_tracking(res); 3006 ocfs2_put_dlm_debug(priv->p_dlm_debug); 3007 return seq_release_private(inode, file); 3008 } 3009 3010 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file) 3011 { 3012 struct ocfs2_dlm_seq_priv *priv; 3013 struct ocfs2_super *osb; 3014 3015 priv = __seq_open_private(file, &ocfs2_dlm_seq_ops, sizeof(*priv)); 3016 if (!priv) { 3017 mlog_errno(-ENOMEM); 3018 return -ENOMEM; 3019 } 3020 3021 osb = inode->i_private; 3022 ocfs2_get_dlm_debug(osb->osb_dlm_debug); 3023 priv->p_dlm_debug = osb->osb_dlm_debug; 3024 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list); 3025 3026 ocfs2_add_lockres_tracking(&priv->p_iter_res, 3027 priv->p_dlm_debug); 3028 3029 return 0; 3030 } 3031 3032 static const struct file_operations ocfs2_dlm_debug_fops = { 3033 .open = ocfs2_dlm_debug_open, 3034 .release = ocfs2_dlm_debug_release, 3035 .read = seq_read, 3036 .llseek = seq_lseek, 3037 }; 3038 3039 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb) 3040 { 3041 int ret = 0; 3042 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug; 3043 3044 dlm_debug->d_locking_state = debugfs_create_file("locking_state", 3045 S_IFREG|S_IRUSR, 3046 osb->osb_debug_root, 3047 osb, 3048 &ocfs2_dlm_debug_fops); 3049 if (!dlm_debug->d_locking_state) { 3050 ret = -EINVAL; 3051 mlog(ML_ERROR, 3052 "Unable to create locking state debugfs file.\n"); 3053 goto out; 3054 } 3055 3056 ocfs2_get_dlm_debug(dlm_debug); 3057 out: 3058 return ret; 3059 } 3060 3061 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb) 3062 { 3063 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug; 3064 3065 if (dlm_debug) { 3066 debugfs_remove(dlm_debug->d_locking_state); 3067 ocfs2_put_dlm_debug(dlm_debug); 3068 } 3069 } 3070 3071 int ocfs2_dlm_init(struct ocfs2_super *osb) 3072 { 3073 int status = 0; 3074 struct ocfs2_cluster_connection *conn = NULL; 3075 3076 if (ocfs2_mount_local(osb)) { 3077 osb->node_num = 0; 3078 goto local; 3079 } 3080 3081 status = ocfs2_dlm_init_debug(osb); 3082 if (status < 0) { 3083 mlog_errno(status); 3084 goto bail; 3085 } 3086 3087 /* launch downconvert thread */ 3088 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc-%s", 3089 osb->uuid_str); 3090 if (IS_ERR(osb->dc_task)) { 3091 status = PTR_ERR(osb->dc_task); 3092 osb->dc_task = NULL; 3093 mlog_errno(status); 3094 goto bail; 3095 } 3096 3097 /* for now, uuid == domain */ 3098 status = ocfs2_cluster_connect(osb->osb_cluster_stack, 3099 osb->osb_cluster_name, 3100 strlen(osb->osb_cluster_name), 3101 osb->uuid_str, 3102 strlen(osb->uuid_str), 3103 &lproto, ocfs2_do_node_down, osb, 3104 &conn); 3105 if (status) { 3106 mlog_errno(status); 3107 goto bail; 3108 } 3109 3110 status = ocfs2_cluster_this_node(conn, &osb->node_num); 3111 if (status < 0) { 3112 mlog_errno(status); 3113 mlog(ML_ERROR, 3114 "could not find this host's node number\n"); 3115 ocfs2_cluster_disconnect(conn, 0); 3116 goto bail; 3117 } 3118 3119 local: 3120 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb); 3121 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb); 3122 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb); 3123 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb); 3124 3125 osb->cconn = conn; 3126 bail: 3127 if (status < 0) { 3128 ocfs2_dlm_shutdown_debug(osb); 3129 if (osb->dc_task) 3130 kthread_stop(osb->dc_task); 3131 } 3132 3133 return status; 3134 } 3135 3136 void ocfs2_dlm_shutdown(struct ocfs2_super *osb, 3137 int hangup_pending) 3138 { 3139 ocfs2_drop_osb_locks(osb); 3140 3141 /* 3142 * Now that we have dropped all locks and ocfs2_dismount_volume() 3143 * has disabled recovery, the DLM won't be talking to us. It's 3144 * safe to tear things down before disconnecting the cluster. 3145 */ 3146 3147 if (osb->dc_task) { 3148 kthread_stop(osb->dc_task); 3149 osb->dc_task = NULL; 3150 } 3151 3152 ocfs2_lock_res_free(&osb->osb_super_lockres); 3153 ocfs2_lock_res_free(&osb->osb_rename_lockres); 3154 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres); 3155 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres); 3156 3157 ocfs2_cluster_disconnect(osb->cconn, hangup_pending); 3158 osb->cconn = NULL; 3159 3160 ocfs2_dlm_shutdown_debug(osb); 3161 } 3162 3163 static int ocfs2_drop_lock(struct ocfs2_super *osb, 3164 struct ocfs2_lock_res *lockres) 3165 { 3166 int ret; 3167 unsigned long flags; 3168 u32 lkm_flags = 0; 3169 3170 /* We didn't get anywhere near actually using this lockres. */ 3171 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED)) 3172 goto out; 3173 3174 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) 3175 lkm_flags |= DLM_LKF_VALBLK; 3176 3177 spin_lock_irqsave(&lockres->l_lock, flags); 3178 3179 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING), 3180 "lockres %s, flags 0x%lx\n", 3181 lockres->l_name, lockres->l_flags); 3182 3183 while (lockres->l_flags & OCFS2_LOCK_BUSY) { 3184 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = " 3185 "%u, unlock_action = %u\n", 3186 lockres->l_name, lockres->l_flags, lockres->l_action, 3187 lockres->l_unlock_action); 3188 3189 spin_unlock_irqrestore(&lockres->l_lock, flags); 3190 3191 /* XXX: Today we just wait on any busy 3192 * locks... Perhaps we need to cancel converts in the 3193 * future? */ 3194 ocfs2_wait_on_busy_lock(lockres); 3195 3196 spin_lock_irqsave(&lockres->l_lock, flags); 3197 } 3198 3199 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) { 3200 if (lockres->l_flags & OCFS2_LOCK_ATTACHED && 3201 lockres->l_level == DLM_LOCK_EX && 3202 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) 3203 lockres->l_ops->set_lvb(lockres); 3204 } 3205 3206 if (lockres->l_flags & OCFS2_LOCK_BUSY) 3207 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n", 3208 lockres->l_name); 3209 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) 3210 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name); 3211 3212 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) { 3213 spin_unlock_irqrestore(&lockres->l_lock, flags); 3214 goto out; 3215 } 3216 3217 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED); 3218 3219 /* make sure we never get here while waiting for an ast to 3220 * fire. */ 3221 BUG_ON(lockres->l_action != OCFS2_AST_INVALID); 3222 3223 /* is this necessary? */ 3224 lockres_or_flags(lockres, OCFS2_LOCK_BUSY); 3225 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK; 3226 spin_unlock_irqrestore(&lockres->l_lock, flags); 3227 3228 mlog(0, "lock %s\n", lockres->l_name); 3229 3230 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags); 3231 if (ret) { 3232 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres); 3233 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags); 3234 ocfs2_dlm_dump_lksb(&lockres->l_lksb); 3235 BUG(); 3236 } 3237 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n", 3238 lockres->l_name); 3239 3240 ocfs2_wait_on_busy_lock(lockres); 3241 out: 3242 return 0; 3243 } 3244 3245 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb, 3246 struct ocfs2_lock_res *lockres); 3247 3248 /* Mark the lockres as being dropped. It will no longer be 3249 * queued if blocking, but we still may have to wait on it 3250 * being dequeued from the downconvert thread before we can consider 3251 * it safe to drop. 3252 * 3253 * You can *not* attempt to call cluster_lock on this lockres anymore. */ 3254 void ocfs2_mark_lockres_freeing(struct ocfs2_super *osb, 3255 struct ocfs2_lock_res *lockres) 3256 { 3257 int status; 3258 struct ocfs2_mask_waiter mw; 3259 unsigned long flags, flags2; 3260 3261 ocfs2_init_mask_waiter(&mw); 3262 3263 spin_lock_irqsave(&lockres->l_lock, flags); 3264 lockres->l_flags |= OCFS2_LOCK_FREEING; 3265 if (lockres->l_flags & OCFS2_LOCK_QUEUED && current == osb->dc_task) { 3266 /* 3267 * We know the downconvert is queued but not in progress 3268 * because we are the downconvert thread and processing 3269 * different lock. So we can just remove the lock from the 3270 * queue. This is not only an optimization but also a way 3271 * to avoid the following deadlock: 3272 * ocfs2_dentry_post_unlock() 3273 * ocfs2_dentry_lock_put() 3274 * ocfs2_drop_dentry_lock() 3275 * iput() 3276 * ocfs2_evict_inode() 3277 * ocfs2_clear_inode() 3278 * ocfs2_mark_lockres_freeing() 3279 * ... blocks waiting for OCFS2_LOCK_QUEUED 3280 * since we are the downconvert thread which 3281 * should clear the flag. 3282 */ 3283 spin_unlock_irqrestore(&lockres->l_lock, flags); 3284 spin_lock_irqsave(&osb->dc_task_lock, flags2); 3285 list_del_init(&lockres->l_blocked_list); 3286 osb->blocked_lock_count--; 3287 spin_unlock_irqrestore(&osb->dc_task_lock, flags2); 3288 /* 3289 * Warn if we recurse into another post_unlock call. Strictly 3290 * speaking it isn't a problem but we need to be careful if 3291 * that happens (stack overflow, deadlocks, ...) so warn if 3292 * ocfs2 grows a path for which this can happen. 3293 */ 3294 WARN_ON_ONCE(lockres->l_ops->post_unlock); 3295 /* Since the lock is freeing we don't do much in the fn below */ 3296 ocfs2_process_blocked_lock(osb, lockres); 3297 return; 3298 } 3299 while (lockres->l_flags & OCFS2_LOCK_QUEUED) { 3300 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0); 3301 spin_unlock_irqrestore(&lockres->l_lock, flags); 3302 3303 mlog(0, "Waiting on lockres %s\n", lockres->l_name); 3304 3305 status = ocfs2_wait_for_mask(&mw); 3306 if (status) 3307 mlog_errno(status); 3308 3309 spin_lock_irqsave(&lockres->l_lock, flags); 3310 } 3311 spin_unlock_irqrestore(&lockres->l_lock, flags); 3312 } 3313 3314 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb, 3315 struct ocfs2_lock_res *lockres) 3316 { 3317 int ret; 3318 3319 ocfs2_mark_lockres_freeing(osb, lockres); 3320 ret = ocfs2_drop_lock(osb, lockres); 3321 if (ret) 3322 mlog_errno(ret); 3323 } 3324 3325 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb) 3326 { 3327 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres); 3328 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres); 3329 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres); 3330 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres); 3331 } 3332 3333 int ocfs2_drop_inode_locks(struct inode *inode) 3334 { 3335 int status, err; 3336 3337 /* No need to call ocfs2_mark_lockres_freeing here - 3338 * ocfs2_clear_inode has done it for us. */ 3339 3340 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb), 3341 &OCFS2_I(inode)->ip_open_lockres); 3342 if (err < 0) 3343 mlog_errno(err); 3344 3345 status = err; 3346 3347 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb), 3348 &OCFS2_I(inode)->ip_inode_lockres); 3349 if (err < 0) 3350 mlog_errno(err); 3351 if (err < 0 && !status) 3352 status = err; 3353 3354 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb), 3355 &OCFS2_I(inode)->ip_rw_lockres); 3356 if (err < 0) 3357 mlog_errno(err); 3358 if (err < 0 && !status) 3359 status = err; 3360 3361 return status; 3362 } 3363 3364 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres, 3365 int new_level) 3366 { 3367 assert_spin_locked(&lockres->l_lock); 3368 3369 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL); 3370 3371 if (lockres->l_level <= new_level) { 3372 mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, " 3373 "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, " 3374 "block %d, pgen %d\n", lockres->l_name, lockres->l_level, 3375 new_level, list_empty(&lockres->l_blocked_list), 3376 list_empty(&lockres->l_mask_waiters), lockres->l_type, 3377 lockres->l_flags, lockres->l_ro_holders, 3378 lockres->l_ex_holders, lockres->l_action, 3379 lockres->l_unlock_action, lockres->l_requested, 3380 lockres->l_blocking, lockres->l_pending_gen); 3381 BUG(); 3382 } 3383 3384 mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n", 3385 lockres->l_name, lockres->l_level, new_level, lockres->l_blocking); 3386 3387 lockres->l_action = OCFS2_AST_DOWNCONVERT; 3388 lockres->l_requested = new_level; 3389 lockres_or_flags(lockres, OCFS2_LOCK_BUSY); 3390 return lockres_set_pending(lockres); 3391 } 3392 3393 static int ocfs2_downconvert_lock(struct ocfs2_super *osb, 3394 struct ocfs2_lock_res *lockres, 3395 int new_level, 3396 int lvb, 3397 unsigned int generation) 3398 { 3399 int ret; 3400 u32 dlm_flags = DLM_LKF_CONVERT; 3401 3402 mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name, 3403 lockres->l_level, new_level); 3404 3405 /* 3406 * On DLM_LKF_VALBLK, fsdlm behaves differently with o2cb. It always 3407 * expects DLM_LKF_VALBLK being set if the LKB has LVB, so that 3408 * we can recover correctly from node failure. Otherwise, we may get 3409 * invalid LVB in LKB, but without DLM_SBF_VALNOTVALID being set. 3410 */ 3411 if (!ocfs2_is_o2cb_active() && 3412 lockres->l_ops->flags & LOCK_TYPE_USES_LVB) 3413 lvb = 1; 3414 3415 if (lvb) 3416 dlm_flags |= DLM_LKF_VALBLK; 3417 3418 ret = ocfs2_dlm_lock(osb->cconn, 3419 new_level, 3420 &lockres->l_lksb, 3421 dlm_flags, 3422 lockres->l_name, 3423 OCFS2_LOCK_ID_MAX_LEN - 1); 3424 lockres_clear_pending(lockres, generation, osb); 3425 if (ret) { 3426 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres); 3427 ocfs2_recover_from_dlm_error(lockres, 1); 3428 goto bail; 3429 } 3430 3431 ret = 0; 3432 bail: 3433 return ret; 3434 } 3435 3436 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */ 3437 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb, 3438 struct ocfs2_lock_res *lockres) 3439 { 3440 assert_spin_locked(&lockres->l_lock); 3441 3442 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) { 3443 /* If we're already trying to cancel a lock conversion 3444 * then just drop the spinlock and allow the caller to 3445 * requeue this lock. */ 3446 mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name); 3447 return 0; 3448 } 3449 3450 /* were we in a convert when we got the bast fire? */ 3451 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT && 3452 lockres->l_action != OCFS2_AST_DOWNCONVERT); 3453 /* set things up for the unlockast to know to just 3454 * clear out the ast_action and unset busy, etc. */ 3455 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT; 3456 3457 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY), 3458 "lock %s, invalid flags: 0x%lx\n", 3459 lockres->l_name, lockres->l_flags); 3460 3461 mlog(ML_BASTS, "lockres %s\n", lockres->l_name); 3462 3463 return 1; 3464 } 3465 3466 static int ocfs2_cancel_convert(struct ocfs2_super *osb, 3467 struct ocfs2_lock_res *lockres) 3468 { 3469 int ret; 3470 3471 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, 3472 DLM_LKF_CANCEL); 3473 if (ret) { 3474 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres); 3475 ocfs2_recover_from_dlm_error(lockres, 0); 3476 } 3477 3478 mlog(ML_BASTS, "lockres %s\n", lockres->l_name); 3479 3480 return ret; 3481 } 3482 3483 static int ocfs2_unblock_lock(struct ocfs2_super *osb, 3484 struct ocfs2_lock_res *lockres, 3485 struct ocfs2_unblock_ctl *ctl) 3486 { 3487 unsigned long flags; 3488 int blocking; 3489 int new_level; 3490 int level; 3491 int ret = 0; 3492 int set_lvb = 0; 3493 unsigned int gen; 3494 3495 spin_lock_irqsave(&lockres->l_lock, flags); 3496 3497 recheck: 3498 /* 3499 * Is it still blocking? If not, we have no more work to do. 3500 */ 3501 if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) { 3502 BUG_ON(lockres->l_blocking != DLM_LOCK_NL); 3503 spin_unlock_irqrestore(&lockres->l_lock, flags); 3504 ret = 0; 3505 goto leave; 3506 } 3507 3508 if (lockres->l_flags & OCFS2_LOCK_BUSY) { 3509 /* XXX 3510 * This is a *big* race. The OCFS2_LOCK_PENDING flag 3511 * exists entirely for one reason - another thread has set 3512 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock(). 3513 * 3514 * If we do ocfs2_cancel_convert() before the other thread 3515 * calls dlm_lock(), our cancel will do nothing. We will 3516 * get no ast, and we will have no way of knowing the 3517 * cancel failed. Meanwhile, the other thread will call 3518 * into dlm_lock() and wait...forever. 3519 * 3520 * Why forever? Because another node has asked for the 3521 * lock first; that's why we're here in unblock_lock(). 3522 * 3523 * The solution is OCFS2_LOCK_PENDING. When PENDING is 3524 * set, we just requeue the unblock. Only when the other 3525 * thread has called dlm_lock() and cleared PENDING will 3526 * we then cancel their request. 3527 * 3528 * All callers of dlm_lock() must set OCFS2_DLM_PENDING 3529 * at the same time they set OCFS2_DLM_BUSY. They must 3530 * clear OCFS2_DLM_PENDING after dlm_lock() returns. 3531 */ 3532 if (lockres->l_flags & OCFS2_LOCK_PENDING) { 3533 mlog(ML_BASTS, "lockres %s, ReQ: Pending\n", 3534 lockres->l_name); 3535 goto leave_requeue; 3536 } 3537 3538 ctl->requeue = 1; 3539 ret = ocfs2_prepare_cancel_convert(osb, lockres); 3540 spin_unlock_irqrestore(&lockres->l_lock, flags); 3541 if (ret) { 3542 ret = ocfs2_cancel_convert(osb, lockres); 3543 if (ret < 0) 3544 mlog_errno(ret); 3545 } 3546 goto leave; 3547 } 3548 3549 /* 3550 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is 3551 * set when the ast is received for an upconvert just before the 3552 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast 3553 * on the heels of the ast, we want to delay the downconvert just 3554 * enough to allow the up requestor to do its task. Because this 3555 * lock is in the blocked queue, the lock will be downconverted 3556 * as soon as the requestor is done with the lock. 3557 */ 3558 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) 3559 goto leave_requeue; 3560 3561 /* 3562 * How can we block and yet be at NL? We were trying to upconvert 3563 * from NL and got canceled. The code comes back here, and now 3564 * we notice and clear BLOCKING. 3565 */ 3566 if (lockres->l_level == DLM_LOCK_NL) { 3567 BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders); 3568 mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name); 3569 lockres->l_blocking = DLM_LOCK_NL; 3570 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED); 3571 spin_unlock_irqrestore(&lockres->l_lock, flags); 3572 goto leave; 3573 } 3574 3575 /* if we're blocking an exclusive and we have *any* holders, 3576 * then requeue. */ 3577 if ((lockres->l_blocking == DLM_LOCK_EX) 3578 && (lockres->l_ex_holders || lockres->l_ro_holders)) { 3579 mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n", 3580 lockres->l_name, lockres->l_ex_holders, 3581 lockres->l_ro_holders); 3582 goto leave_requeue; 3583 } 3584 3585 /* If it's a PR we're blocking, then only 3586 * requeue if we've got any EX holders */ 3587 if (lockres->l_blocking == DLM_LOCK_PR && 3588 lockres->l_ex_holders) { 3589 mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n", 3590 lockres->l_name, lockres->l_ex_holders); 3591 goto leave_requeue; 3592 } 3593 3594 /* 3595 * Can we get a lock in this state if the holder counts are 3596 * zero? The meta data unblock code used to check this. 3597 */ 3598 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH) 3599 && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) { 3600 mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n", 3601 lockres->l_name); 3602 goto leave_requeue; 3603 } 3604 3605 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking); 3606 3607 if (lockres->l_ops->check_downconvert 3608 && !lockres->l_ops->check_downconvert(lockres, new_level)) { 3609 mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n", 3610 lockres->l_name); 3611 goto leave_requeue; 3612 } 3613 3614 /* If we get here, then we know that there are no more 3615 * incompatible holders (and anyone asking for an incompatible 3616 * lock is blocked). We can now downconvert the lock */ 3617 if (!lockres->l_ops->downconvert_worker) 3618 goto downconvert; 3619 3620 /* Some lockres types want to do a bit of work before 3621 * downconverting a lock. Allow that here. The worker function 3622 * may sleep, so we save off a copy of what we're blocking as 3623 * it may change while we're not holding the spin lock. */ 3624 blocking = lockres->l_blocking; 3625 level = lockres->l_level; 3626 spin_unlock_irqrestore(&lockres->l_lock, flags); 3627 3628 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking); 3629 3630 if (ctl->unblock_action == UNBLOCK_STOP_POST) { 3631 mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n", 3632 lockres->l_name); 3633 goto leave; 3634 } 3635 3636 spin_lock_irqsave(&lockres->l_lock, flags); 3637 if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) { 3638 /* If this changed underneath us, then we can't drop 3639 * it just yet. */ 3640 mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, " 3641 "Recheck\n", lockres->l_name, blocking, 3642 lockres->l_blocking, level, lockres->l_level); 3643 goto recheck; 3644 } 3645 3646 downconvert: 3647 ctl->requeue = 0; 3648 3649 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) { 3650 if (lockres->l_level == DLM_LOCK_EX) 3651 set_lvb = 1; 3652 3653 /* 3654 * We only set the lvb if the lock has been fully 3655 * refreshed - otherwise we risk setting stale 3656 * data. Otherwise, there's no need to actually clear 3657 * out the lvb here as it's value is still valid. 3658 */ 3659 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) 3660 lockres->l_ops->set_lvb(lockres); 3661 } 3662 3663 gen = ocfs2_prepare_downconvert(lockres, new_level); 3664 spin_unlock_irqrestore(&lockres->l_lock, flags); 3665 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb, 3666 gen); 3667 3668 leave: 3669 if (ret) 3670 mlog_errno(ret); 3671 return ret; 3672 3673 leave_requeue: 3674 spin_unlock_irqrestore(&lockres->l_lock, flags); 3675 ctl->requeue = 1; 3676 3677 return 0; 3678 } 3679 3680 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres, 3681 int blocking) 3682 { 3683 struct inode *inode; 3684 struct address_space *mapping; 3685 struct ocfs2_inode_info *oi; 3686 3687 inode = ocfs2_lock_res_inode(lockres); 3688 mapping = inode->i_mapping; 3689 3690 if (S_ISDIR(inode->i_mode)) { 3691 oi = OCFS2_I(inode); 3692 oi->ip_dir_lock_gen++; 3693 mlog(0, "generation: %u\n", oi->ip_dir_lock_gen); 3694 goto out; 3695 } 3696 3697 if (!S_ISREG(inode->i_mode)) 3698 goto out; 3699 3700 /* 3701 * We need this before the filemap_fdatawrite() so that it can 3702 * transfer the dirty bit from the PTE to the 3703 * page. Unfortunately this means that even for EX->PR 3704 * downconverts, we'll lose our mappings and have to build 3705 * them up again. 3706 */ 3707 unmap_mapping_range(mapping, 0, 0, 0); 3708 3709 if (filemap_fdatawrite(mapping)) { 3710 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!", 3711 (unsigned long long)OCFS2_I(inode)->ip_blkno); 3712 } 3713 sync_mapping_buffers(mapping); 3714 if (blocking == DLM_LOCK_EX) { 3715 truncate_inode_pages(mapping, 0); 3716 } else { 3717 /* We only need to wait on the I/O if we're not also 3718 * truncating pages because truncate_inode_pages waits 3719 * for us above. We don't truncate pages if we're 3720 * blocking anything < EXMODE because we want to keep 3721 * them around in that case. */ 3722 filemap_fdatawait(mapping); 3723 } 3724 3725 forget_all_cached_acls(inode); 3726 3727 out: 3728 return UNBLOCK_CONTINUE; 3729 } 3730 3731 static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci, 3732 struct ocfs2_lock_res *lockres, 3733 int new_level) 3734 { 3735 int checkpointed = ocfs2_ci_fully_checkpointed(ci); 3736 3737 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR); 3738 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed); 3739 3740 if (checkpointed) 3741 return 1; 3742 3743 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci))); 3744 return 0; 3745 } 3746 3747 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres, 3748 int new_level) 3749 { 3750 struct inode *inode = ocfs2_lock_res_inode(lockres); 3751 3752 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level); 3753 } 3754 3755 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres) 3756 { 3757 struct inode *inode = ocfs2_lock_res_inode(lockres); 3758 3759 __ocfs2_stuff_meta_lvb(inode); 3760 } 3761 3762 /* 3763 * Does the final reference drop on our dentry lock. Right now this 3764 * happens in the downconvert thread, but we could choose to simplify the 3765 * dlmglue API and push these off to the ocfs2_wq in the future. 3766 */ 3767 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb, 3768 struct ocfs2_lock_res *lockres) 3769 { 3770 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres); 3771 ocfs2_dentry_lock_put(osb, dl); 3772 } 3773 3774 /* 3775 * d_delete() matching dentries before the lock downconvert. 3776 * 3777 * At this point, any process waiting to destroy the 3778 * dentry_lock due to last ref count is stopped by the 3779 * OCFS2_LOCK_QUEUED flag. 3780 * 3781 * We have two potential problems 3782 * 3783 * 1) If we do the last reference drop on our dentry_lock (via dput) 3784 * we'll wind up in ocfs2_release_dentry_lock(), waiting on 3785 * the downconvert to finish. Instead we take an elevated 3786 * reference and push the drop until after we've completed our 3787 * unblock processing. 3788 * 3789 * 2) There might be another process with a final reference, 3790 * waiting on us to finish processing. If this is the case, we 3791 * detect it and exit out - there's no more dentries anyway. 3792 */ 3793 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres, 3794 int blocking) 3795 { 3796 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres); 3797 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode); 3798 struct dentry *dentry; 3799 unsigned long flags; 3800 int extra_ref = 0; 3801 3802 /* 3803 * This node is blocking another node from getting a read 3804 * lock. This happens when we've renamed within a 3805 * directory. We've forced the other nodes to d_delete(), but 3806 * we never actually dropped our lock because it's still 3807 * valid. The downconvert code will retain a PR for this node, 3808 * so there's no further work to do. 3809 */ 3810 if (blocking == DLM_LOCK_PR) 3811 return UNBLOCK_CONTINUE; 3812 3813 /* 3814 * Mark this inode as potentially orphaned. The code in 3815 * ocfs2_delete_inode() will figure out whether it actually 3816 * needs to be freed or not. 3817 */ 3818 spin_lock(&oi->ip_lock); 3819 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED; 3820 spin_unlock(&oi->ip_lock); 3821 3822 /* 3823 * Yuck. We need to make sure however that the check of 3824 * OCFS2_LOCK_FREEING and the extra reference are atomic with 3825 * respect to a reference decrement or the setting of that 3826 * flag. 3827 */ 3828 spin_lock_irqsave(&lockres->l_lock, flags); 3829 spin_lock(&dentry_attach_lock); 3830 if (!(lockres->l_flags & OCFS2_LOCK_FREEING) 3831 && dl->dl_count) { 3832 dl->dl_count++; 3833 extra_ref = 1; 3834 } 3835 spin_unlock(&dentry_attach_lock); 3836 spin_unlock_irqrestore(&lockres->l_lock, flags); 3837 3838 mlog(0, "extra_ref = %d\n", extra_ref); 3839 3840 /* 3841 * We have a process waiting on us in ocfs2_dentry_iput(), 3842 * which means we can't have any more outstanding 3843 * aliases. There's no need to do any more work. 3844 */ 3845 if (!extra_ref) 3846 return UNBLOCK_CONTINUE; 3847 3848 spin_lock(&dentry_attach_lock); 3849 while (1) { 3850 dentry = ocfs2_find_local_alias(dl->dl_inode, 3851 dl->dl_parent_blkno, 1); 3852 if (!dentry) 3853 break; 3854 spin_unlock(&dentry_attach_lock); 3855 3856 if (S_ISDIR(dl->dl_inode->i_mode)) 3857 shrink_dcache_parent(dentry); 3858 3859 mlog(0, "d_delete(%pd);\n", dentry); 3860 3861 /* 3862 * The following dcache calls may do an 3863 * iput(). Normally we don't want that from the 3864 * downconverting thread, but in this case it's ok 3865 * because the requesting node already has an 3866 * exclusive lock on the inode, so it can't be queued 3867 * for a downconvert. 3868 */ 3869 d_delete(dentry); 3870 dput(dentry); 3871 3872 spin_lock(&dentry_attach_lock); 3873 } 3874 spin_unlock(&dentry_attach_lock); 3875 3876 /* 3877 * If we are the last holder of this dentry lock, there is no 3878 * reason to downconvert so skip straight to the unlock. 3879 */ 3880 if (dl->dl_count == 1) 3881 return UNBLOCK_STOP_POST; 3882 3883 return UNBLOCK_CONTINUE_POST; 3884 } 3885 3886 static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres, 3887 int new_level) 3888 { 3889 struct ocfs2_refcount_tree *tree = 3890 ocfs2_lock_res_refcount_tree(lockres); 3891 3892 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level); 3893 } 3894 3895 static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres, 3896 int blocking) 3897 { 3898 struct ocfs2_refcount_tree *tree = 3899 ocfs2_lock_res_refcount_tree(lockres); 3900 3901 ocfs2_metadata_cache_purge(&tree->rf_ci); 3902 3903 return UNBLOCK_CONTINUE; 3904 } 3905 3906 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres) 3907 { 3908 struct ocfs2_qinfo_lvb *lvb; 3909 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres); 3910 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb, 3911 oinfo->dqi_gi.dqi_type); 3912 3913 lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 3914 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION; 3915 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace); 3916 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace); 3917 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms); 3918 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks); 3919 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk); 3920 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry); 3921 } 3922 3923 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex) 3924 { 3925 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock; 3926 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb); 3927 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 3928 3929 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) 3930 ocfs2_cluster_unlock(osb, lockres, level); 3931 } 3932 3933 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo) 3934 { 3935 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb, 3936 oinfo->dqi_gi.dqi_type); 3937 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock; 3938 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb); 3939 struct buffer_head *bh = NULL; 3940 struct ocfs2_global_disk_dqinfo *gdinfo; 3941 int status = 0; 3942 3943 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) && 3944 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) { 3945 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace); 3946 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace); 3947 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms); 3948 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks); 3949 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk); 3950 oinfo->dqi_gi.dqi_free_entry = 3951 be32_to_cpu(lvb->lvb_free_entry); 3952 } else { 3953 status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode, 3954 oinfo->dqi_giblk, &bh); 3955 if (status) { 3956 mlog_errno(status); 3957 goto bail; 3958 } 3959 gdinfo = (struct ocfs2_global_disk_dqinfo *) 3960 (bh->b_data + OCFS2_GLOBAL_INFO_OFF); 3961 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace); 3962 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace); 3963 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms); 3964 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks); 3965 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk); 3966 oinfo->dqi_gi.dqi_free_entry = 3967 le32_to_cpu(gdinfo->dqi_free_entry); 3968 brelse(bh); 3969 ocfs2_track_lock_refresh(lockres); 3970 } 3971 3972 bail: 3973 return status; 3974 } 3975 3976 /* Lock quota info, this function expects at least shared lock on the quota file 3977 * so that we can safely refresh quota info from disk. */ 3978 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex) 3979 { 3980 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock; 3981 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb); 3982 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 3983 int status = 0; 3984 3985 /* On RO devices, locking really isn't needed... */ 3986 if (ocfs2_is_hard_readonly(osb)) { 3987 if (ex) 3988 status = -EROFS; 3989 goto bail; 3990 } 3991 if (ocfs2_mount_local(osb)) 3992 goto bail; 3993 3994 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0); 3995 if (status < 0) { 3996 mlog_errno(status); 3997 goto bail; 3998 } 3999 if (!ocfs2_should_refresh_lock_res(lockres)) 4000 goto bail; 4001 /* OK, we have the lock but we need to refresh the quota info */ 4002 status = ocfs2_refresh_qinfo(oinfo); 4003 if (status) 4004 ocfs2_qinfo_unlock(oinfo, ex); 4005 ocfs2_complete_lock_res_refresh(lockres, status); 4006 bail: 4007 return status; 4008 } 4009 4010 int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex) 4011 { 4012 int status; 4013 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 4014 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres; 4015 struct ocfs2_super *osb = lockres->l_priv; 4016 4017 4018 if (ocfs2_is_hard_readonly(osb)) 4019 return -EROFS; 4020 4021 if (ocfs2_mount_local(osb)) 4022 return 0; 4023 4024 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0); 4025 if (status < 0) 4026 mlog_errno(status); 4027 4028 return status; 4029 } 4030 4031 void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex) 4032 { 4033 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR; 4034 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres; 4035 struct ocfs2_super *osb = lockres->l_priv; 4036 4037 if (!ocfs2_mount_local(osb)) 4038 ocfs2_cluster_unlock(osb, lockres, level); 4039 } 4040 4041 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb, 4042 struct ocfs2_lock_res *lockres) 4043 { 4044 int status; 4045 struct ocfs2_unblock_ctl ctl = {0, 0,}; 4046 unsigned long flags; 4047 4048 /* Our reference to the lockres in this function can be 4049 * considered valid until we remove the OCFS2_LOCK_QUEUED 4050 * flag. */ 4051 4052 BUG_ON(!lockres); 4053 BUG_ON(!lockres->l_ops); 4054 4055 mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name); 4056 4057 /* Detect whether a lock has been marked as going away while 4058 * the downconvert thread was processing other things. A lock can 4059 * still be marked with OCFS2_LOCK_FREEING after this check, 4060 * but short circuiting here will still save us some 4061 * performance. */ 4062 spin_lock_irqsave(&lockres->l_lock, flags); 4063 if (lockres->l_flags & OCFS2_LOCK_FREEING) 4064 goto unqueue; 4065 spin_unlock_irqrestore(&lockres->l_lock, flags); 4066 4067 status = ocfs2_unblock_lock(osb, lockres, &ctl); 4068 if (status < 0) 4069 mlog_errno(status); 4070 4071 spin_lock_irqsave(&lockres->l_lock, flags); 4072 unqueue: 4073 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) { 4074 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED); 4075 } else 4076 ocfs2_schedule_blocked_lock(osb, lockres); 4077 4078 mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name, 4079 ctl.requeue ? "yes" : "no"); 4080 spin_unlock_irqrestore(&lockres->l_lock, flags); 4081 4082 if (ctl.unblock_action != UNBLOCK_CONTINUE 4083 && lockres->l_ops->post_unlock) 4084 lockres->l_ops->post_unlock(osb, lockres); 4085 } 4086 4087 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb, 4088 struct ocfs2_lock_res *lockres) 4089 { 4090 unsigned long flags; 4091 4092 assert_spin_locked(&lockres->l_lock); 4093 4094 if (lockres->l_flags & OCFS2_LOCK_FREEING) { 4095 /* Do not schedule a lock for downconvert when it's on 4096 * the way to destruction - any nodes wanting access 4097 * to the resource will get it soon. */ 4098 mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n", 4099 lockres->l_name, lockres->l_flags); 4100 return; 4101 } 4102 4103 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED); 4104 4105 spin_lock_irqsave(&osb->dc_task_lock, flags); 4106 if (list_empty(&lockres->l_blocked_list)) { 4107 list_add_tail(&lockres->l_blocked_list, 4108 &osb->blocked_lock_list); 4109 osb->blocked_lock_count++; 4110 } 4111 spin_unlock_irqrestore(&osb->dc_task_lock, flags); 4112 } 4113 4114 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb) 4115 { 4116 unsigned long processed; 4117 unsigned long flags; 4118 struct ocfs2_lock_res *lockres; 4119 4120 spin_lock_irqsave(&osb->dc_task_lock, flags); 4121 /* grab this early so we know to try again if a state change and 4122 * wake happens part-way through our work */ 4123 osb->dc_work_sequence = osb->dc_wake_sequence; 4124 4125 processed = osb->blocked_lock_count; 4126 /* 4127 * blocked lock processing in this loop might call iput which can 4128 * remove items off osb->blocked_lock_list. Downconvert up to 4129 * 'processed' number of locks, but stop short if we had some 4130 * removed in ocfs2_mark_lockres_freeing when downconverting. 4131 */ 4132 while (processed && !list_empty(&osb->blocked_lock_list)) { 4133 lockres = list_entry(osb->blocked_lock_list.next, 4134 struct ocfs2_lock_res, l_blocked_list); 4135 list_del_init(&lockres->l_blocked_list); 4136 osb->blocked_lock_count--; 4137 spin_unlock_irqrestore(&osb->dc_task_lock, flags); 4138 4139 BUG_ON(!processed); 4140 processed--; 4141 4142 ocfs2_process_blocked_lock(osb, lockres); 4143 4144 spin_lock_irqsave(&osb->dc_task_lock, flags); 4145 } 4146 spin_unlock_irqrestore(&osb->dc_task_lock, flags); 4147 } 4148 4149 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb) 4150 { 4151 int empty = 0; 4152 unsigned long flags; 4153 4154 spin_lock_irqsave(&osb->dc_task_lock, flags); 4155 if (list_empty(&osb->blocked_lock_list)) 4156 empty = 1; 4157 4158 spin_unlock_irqrestore(&osb->dc_task_lock, flags); 4159 return empty; 4160 } 4161 4162 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb) 4163 { 4164 int should_wake = 0; 4165 unsigned long flags; 4166 4167 spin_lock_irqsave(&osb->dc_task_lock, flags); 4168 if (osb->dc_work_sequence != osb->dc_wake_sequence) 4169 should_wake = 1; 4170 spin_unlock_irqrestore(&osb->dc_task_lock, flags); 4171 4172 return should_wake; 4173 } 4174 4175 static int ocfs2_downconvert_thread(void *arg) 4176 { 4177 int status = 0; 4178 struct ocfs2_super *osb = arg; 4179 4180 /* only quit once we've been asked to stop and there is no more 4181 * work available */ 4182 while (!(kthread_should_stop() && 4183 ocfs2_downconvert_thread_lists_empty(osb))) { 4184 4185 wait_event_interruptible(osb->dc_event, 4186 ocfs2_downconvert_thread_should_wake(osb) || 4187 kthread_should_stop()); 4188 4189 mlog(0, "downconvert_thread: awoken\n"); 4190 4191 ocfs2_downconvert_thread_do_work(osb); 4192 } 4193 4194 osb->dc_task = NULL; 4195 return status; 4196 } 4197 4198 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb) 4199 { 4200 unsigned long flags; 4201 4202 spin_lock_irqsave(&osb->dc_task_lock, flags); 4203 /* make sure the voting thread gets a swipe at whatever changes 4204 * the caller may have made to the voting state */ 4205 osb->dc_wake_sequence++; 4206 spin_unlock_irqrestore(&osb->dc_task_lock, flags); 4207 wake_up(&osb->dc_event); 4208 } 4209