Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * dcache.c |
| 5 | * |
| 6 | * dentry cache handling code |
| 7 | * |
| 8 | * Copyright (C) 2002, 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/fs.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/namei.h> |
| 30 | |
| 31 | #define MLOG_MASK_PREFIX ML_DCACHE |
| 32 | #include <cluster/masklog.h> |
| 33 | |
| 34 | #include "ocfs2.h" |
| 35 | |
| 36 | #include "alloc.h" |
| 37 | #include "dcache.h" |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 38 | #include "dlmglue.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 39 | #include "file.h" |
| 40 | #include "inode.h" |
| 41 | |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 42 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 43 | static int ocfs2_dentry_revalidate(struct dentry *dentry, |
| 44 | struct nameidata *nd) |
| 45 | { |
| 46 | struct inode *inode = dentry->d_inode; |
| 47 | int ret = 0; /* if all else fails, just return false */ |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 48 | struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 49 | |
| 50 | mlog_entry("(0x%p, '%.*s')\n", dentry, |
| 51 | dentry->d_name.len, dentry->d_name.name); |
| 52 | |
| 53 | /* Never trust a negative dentry - force a new lookup. */ |
| 54 | if (inode == NULL) { |
| 55 | mlog(0, "negative dentry: %.*s\n", dentry->d_name.len, |
| 56 | dentry->d_name.name); |
| 57 | goto bail; |
| 58 | } |
| 59 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 60 | BUG_ON(!osb); |
| 61 | |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 62 | if (inode == osb->root_inode || is_bad_inode(inode)) |
| 63 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 64 | |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 65 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 66 | /* did we or someone else delete this inode? */ |
| 67 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { |
| 68 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 69 | mlog(0, "inode (%llu) deleted, returning false\n", |
| 70 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 71 | goto bail; |
| 72 | } |
| 73 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 74 | |
| 75 | /* |
| 76 | * We don't need a cluster lock to test this because once an |
| 77 | * inode nlink hits zero, it never goes back. |
| 78 | */ |
| 79 | if (inode->i_nlink == 0) { |
| 80 | mlog(0, "Inode %llu orphaned, returning false " |
| 81 | "dir = %d\n", |
| 82 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 83 | S_ISDIR(inode->i_mode)); |
| 84 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | ret = 1; |
| 88 | |
| 89 | bail: |
| 90 | mlog_exit(ret); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 95 | static int ocfs2_match_dentry(struct dentry *dentry, |
| 96 | u64 parent_blkno, |
| 97 | int skip_unhashed) |
| 98 | { |
| 99 | struct inode *parent; |
| 100 | |
| 101 | /* |
| 102 | * ocfs2_lookup() does a d_splice_alias() _before_ attaching |
| 103 | * to the lock data, so we skip those here, otherwise |
| 104 | * ocfs2_dentry_attach_lock() will get its original dentry |
| 105 | * back. |
| 106 | */ |
| 107 | if (!dentry->d_fsdata) |
| 108 | return 0; |
| 109 | |
| 110 | if (!dentry->d_parent) |
| 111 | return 0; |
| 112 | |
| 113 | if (skip_unhashed && d_unhashed(dentry)) |
| 114 | return 0; |
| 115 | |
| 116 | parent = dentry->d_parent->d_inode; |
| 117 | /* Negative parent dentry? */ |
| 118 | if (!parent) |
| 119 | return 0; |
| 120 | |
| 121 | /* Name is in a different directory. */ |
| 122 | if (OCFS2_I(parent)->ip_blkno != parent_blkno) |
| 123 | return 0; |
| 124 | |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Walk the inode alias list, and find a dentry which has a given |
| 130 | * parent. ocfs2_dentry_attach_lock() wants to find _any_ alias as it |
| 131 | * is looking for a dentry_lock reference. The vote thread is looking |
| 132 | * to unhash aliases, so we allow it to skip any that already have |
| 133 | * that property. |
| 134 | */ |
| 135 | struct dentry *ocfs2_find_local_alias(struct inode *inode, |
| 136 | u64 parent_blkno, |
| 137 | int skip_unhashed) |
| 138 | { |
| 139 | struct list_head *p; |
| 140 | struct dentry *dentry = NULL; |
| 141 | |
| 142 | spin_lock(&dcache_lock); |
| 143 | |
| 144 | list_for_each(p, &inode->i_dentry) { |
| 145 | dentry = list_entry(p, struct dentry, d_alias); |
| 146 | |
| 147 | if (ocfs2_match_dentry(dentry, parent_blkno, skip_unhashed)) { |
| 148 | mlog(0, "dentry found: %.*s\n", |
| 149 | dentry->d_name.len, dentry->d_name.name); |
| 150 | |
| 151 | dget_locked(dentry); |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | dentry = NULL; |
| 156 | } |
| 157 | |
| 158 | spin_unlock(&dcache_lock); |
| 159 | |
| 160 | return dentry; |
| 161 | } |
| 162 | |
Mark Fasheh | d680efe | 2006-09-08 14:14:34 -0700 | [diff] [blame] | 163 | DEFINE_SPINLOCK(dentry_attach_lock); |
| 164 | |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 165 | /* |
| 166 | * Attach this dentry to a cluster lock. |
| 167 | * |
| 168 | * Dentry locks cover all links in a given directory to a particular |
| 169 | * inode. We do this so that ocfs2 can build a lock name which all |
| 170 | * nodes in the cluster can agree on at all times. Shoving full names |
| 171 | * in the cluster lock won't work due to size restrictions. Covering |
| 172 | * links inside of a directory is a good compromise because it still |
| 173 | * allows us to use the parent directory lock to synchronize |
| 174 | * operations. |
| 175 | * |
| 176 | * Call this function with the parent dir semaphore and the parent dir |
| 177 | * cluster lock held. |
| 178 | * |
| 179 | * The dir semaphore will protect us from having to worry about |
| 180 | * concurrent processes on our node trying to attach a lock at the |
| 181 | * same time. |
| 182 | * |
| 183 | * The dir cluster lock (held at either PR or EX mode) protects us |
| 184 | * from unlink and rename on other nodes. |
| 185 | * |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 186 | * A dput() can happen asynchronously due to pruning, so we cover |
| 187 | * attaching and detaching the dentry lock with a |
| 188 | * dentry_attach_lock. |
| 189 | * |
| 190 | * A node which has done lookup on a name retains a protected read |
| 191 | * lock until final dput. If the user requests and unlink or rename, |
| 192 | * the protected read is upgraded to an exclusive lock. Other nodes |
| 193 | * who have seen the dentry will then be informed that they need to |
| 194 | * downgrade their lock, which will involve d_delete on the |
| 195 | * dentry. This happens in ocfs2_dentry_convert_worker(). |
| 196 | */ |
| 197 | int ocfs2_dentry_attach_lock(struct dentry *dentry, |
| 198 | struct inode *inode, |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 199 | u64 parent_blkno) |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 200 | { |
| 201 | int ret; |
| 202 | struct dentry *alias; |
| 203 | struct ocfs2_dentry_lock *dl = dentry->d_fsdata; |
| 204 | |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 205 | mlog(0, "Attach \"%.*s\", parent %llu, fsdata: %p\n", |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 206 | dentry->d_name.len, dentry->d_name.name, |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 207 | (unsigned long long)parent_blkno, dl); |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * Negative dentry. We ignore these for now. |
| 211 | * |
| 212 | * XXX: Could we can improve ocfs2_dentry_revalidate() by |
| 213 | * tracking these? |
| 214 | */ |
| 215 | if (!inode) |
| 216 | return 0; |
| 217 | |
| 218 | if (dl) { |
| 219 | mlog_bug_on_msg(dl->dl_parent_blkno != parent_blkno, |
| 220 | " \"%.*s\": old parent: %llu, new: %llu\n", |
| 221 | dentry->d_name.len, dentry->d_name.name, |
| 222 | (unsigned long long)parent_blkno, |
| 223 | (unsigned long long)dl->dl_parent_blkno); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | alias = ocfs2_find_local_alias(inode, parent_blkno, 0); |
| 228 | if (alias) { |
| 229 | /* |
| 230 | * Great, an alias exists, which means we must have a |
| 231 | * dentry lock already. We can just grab the lock off |
| 232 | * the alias and add it to the list. |
| 233 | * |
| 234 | * We're depending here on the fact that this dentry |
| 235 | * was found and exists in the dcache and so must have |
| 236 | * a reference to the dentry_lock because we can't |
| 237 | * race creates. Final dput() cannot happen on it |
| 238 | * since we have it pinned, so our reference is safe. |
| 239 | */ |
| 240 | dl = alias->d_fsdata; |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 241 | mlog_bug_on_msg(!dl, "parent %llu, ino %llu\n", |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 242 | (unsigned long long)parent_blkno, |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 243 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 244 | |
| 245 | mlog_bug_on_msg(dl->dl_parent_blkno != parent_blkno, |
| 246 | " \"%.*s\": old parent: %llu, new: %llu\n", |
| 247 | dentry->d_name.len, dentry->d_name.name, |
| 248 | (unsigned long long)parent_blkno, |
| 249 | (unsigned long long)dl->dl_parent_blkno); |
| 250 | |
| 251 | mlog(0, "Found: %s\n", dl->dl_lockres.l_name); |
| 252 | |
| 253 | goto out_attach; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * There are no other aliases |
| 258 | */ |
| 259 | dl = kmalloc(sizeof(*dl), GFP_NOFS); |
| 260 | if (!dl) { |
| 261 | ret = -ENOMEM; |
| 262 | mlog_errno(ret); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | dl->dl_count = 0; |
| 267 | /* |
| 268 | * Does this have to happen below, for all attaches, in case |
| 269 | * the struct inode gets blown away by votes? |
| 270 | */ |
| 271 | dl->dl_inode = igrab(inode); |
| 272 | dl->dl_parent_blkno = parent_blkno; |
| 273 | ocfs2_dentry_lock_res_init(dl, parent_blkno, inode); |
| 274 | |
| 275 | out_attach: |
| 276 | spin_lock(&dentry_attach_lock); |
| 277 | dentry->d_fsdata = dl; |
| 278 | dl->dl_count++; |
| 279 | spin_unlock(&dentry_attach_lock); |
| 280 | |
| 281 | /* |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 282 | * This actually gets us our PRMODE level lock. From now on, |
| 283 | * we'll have a notification if one of these names is |
| 284 | * destroyed on another node. |
| 285 | */ |
| 286 | ret = ocfs2_dentry_lock(dentry, 0); |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 287 | if (!ret) |
| 288 | ocfs2_dentry_unlock(dentry, 0); |
| 289 | else |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 290 | mlog_errno(ret); |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 291 | |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 292 | dput(alias); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * ocfs2_dentry_iput() and friends. |
| 299 | * |
| 300 | * At this point, our particular dentry is detached from the inodes |
| 301 | * alias list, so there's no way that the locking code can find it. |
| 302 | * |
| 303 | * The interesting stuff happens when we determine that our lock needs |
| 304 | * to go away because this is the last subdir alias in the |
| 305 | * system. This function needs to handle a couple things: |
| 306 | * |
| 307 | * 1) Synchronizing lock shutdown with the downconvert threads. This |
| 308 | * is already handled for us via the lockres release drop function |
| 309 | * called in ocfs2_release_dentry_lock() |
| 310 | * |
| 311 | * 2) A race may occur when we're doing our lock shutdown and |
| 312 | * another process wants to create a new dentry lock. Right now we |
| 313 | * let them race, which means that for a very short while, this |
| 314 | * node might have two locks on a lock resource. This should be a |
| 315 | * problem though because one of them is in the process of being |
| 316 | * thrown out. |
| 317 | */ |
| 318 | static void ocfs2_drop_dentry_lock(struct ocfs2_super *osb, |
| 319 | struct ocfs2_dentry_lock *dl) |
| 320 | { |
Mark Fasheh | 9f70968 | 2007-10-18 12:36:10 -0700 | [diff] [blame^] | 321 | iput(dl->dl_inode); |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 322 | ocfs2_simple_drop_lockres(osb, &dl->dl_lockres); |
| 323 | ocfs2_lock_res_free(&dl->dl_lockres); |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 324 | kfree(dl); |
| 325 | } |
| 326 | |
| 327 | void ocfs2_dentry_lock_put(struct ocfs2_super *osb, |
| 328 | struct ocfs2_dentry_lock *dl) |
| 329 | { |
| 330 | int unlock = 0; |
| 331 | |
| 332 | BUG_ON(dl->dl_count == 0); |
| 333 | |
| 334 | spin_lock(&dentry_attach_lock); |
| 335 | dl->dl_count--; |
| 336 | unlock = !dl->dl_count; |
| 337 | spin_unlock(&dentry_attach_lock); |
| 338 | |
| 339 | if (unlock) |
| 340 | ocfs2_drop_dentry_lock(osb, dl); |
| 341 | } |
| 342 | |
| 343 | static void ocfs2_dentry_iput(struct dentry *dentry, struct inode *inode) |
| 344 | { |
| 345 | struct ocfs2_dentry_lock *dl = dentry->d_fsdata; |
| 346 | |
| 347 | mlog_bug_on_msg(!dl && !(dentry->d_flags & DCACHE_DISCONNECTED), |
| 348 | "dentry: %.*s\n", dentry->d_name.len, |
| 349 | dentry->d_name.name); |
| 350 | |
| 351 | if (!dl) |
| 352 | goto out; |
| 353 | |
| 354 | mlog_bug_on_msg(dl->dl_count == 0, "dentry: %.*s, count: %u\n", |
| 355 | dentry->d_name.len, dentry->d_name.name, |
| 356 | dl->dl_count); |
| 357 | |
| 358 | ocfs2_dentry_lock_put(OCFS2_SB(dentry->d_sb), dl); |
| 359 | |
| 360 | out: |
| 361 | iput(inode); |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * d_move(), but keep the locks in sync. |
| 366 | * |
| 367 | * When we are done, "dentry" will have the parent dir and name of |
| 368 | * "target", which will be thrown away. |
| 369 | * |
| 370 | * We manually update the lock of "dentry" if need be. |
| 371 | * |
| 372 | * "target" doesn't have it's dentry lock touched - we allow the later |
| 373 | * dput() to handle this for us. |
| 374 | * |
| 375 | * This is called during ocfs2_rename(), while holding parent |
| 376 | * directory locks. The dentries have already been deleted on other |
| 377 | * nodes via ocfs2_remote_dentry_delete(). |
| 378 | * |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 379 | * Normally, the VFS handles the d_move() for the file system, after |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 380 | * the ->rename() callback. OCFS2 wants to handle this internally, so |
| 381 | * the new lock can be created atomically with respect to the cluster. |
| 382 | */ |
| 383 | void ocfs2_dentry_move(struct dentry *dentry, struct dentry *target, |
| 384 | struct inode *old_dir, struct inode *new_dir) |
| 385 | { |
| 386 | int ret; |
| 387 | struct ocfs2_super *osb = OCFS2_SB(old_dir->i_sb); |
| 388 | struct inode *inode = dentry->d_inode; |
| 389 | |
| 390 | /* |
| 391 | * Move within the same directory, so the actual lock info won't |
| 392 | * change. |
| 393 | * |
| 394 | * XXX: Is there any advantage to dropping the lock here? |
| 395 | */ |
| 396 | if (old_dir == new_dir) |
Mark Fasheh | 1ba9da2 | 2006-09-08 14:22:54 -0700 | [diff] [blame] | 397 | goto out_move; |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 398 | |
| 399 | ocfs2_dentry_lock_put(osb, dentry->d_fsdata); |
| 400 | |
| 401 | dentry->d_fsdata = NULL; |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 402 | ret = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(new_dir)->ip_blkno); |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 403 | if (ret) |
| 404 | mlog_errno(ret); |
Mark Fasheh | 1ba9da2 | 2006-09-08 14:22:54 -0700 | [diff] [blame] | 405 | |
| 406 | out_move: |
| 407 | d_move(dentry, target); |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 410 | struct dentry_operations ocfs2_dentry_ops = { |
| 411 | .d_revalidate = ocfs2_dentry_revalidate, |
Mark Fasheh | 80c0584 | 2006-09-08 14:43:18 -0700 | [diff] [blame] | 412 | .d_iput = ocfs2_dentry_iput, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 413 | }; |