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 | * inode.c |
| 5 | * |
| 6 | * vfs' aops, fops, dops and iops |
| 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/highmem.h> |
| 30 | #include <linux/pagemap.h> |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame^] | 31 | #include <linux/quotaops.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 32 | |
| 33 | #include <asm/byteorder.h> |
| 34 | |
| 35 | #define MLOG_MASK_PREFIX ML_INODE |
| 36 | #include <cluster/masklog.h> |
| 37 | |
| 38 | #include "ocfs2.h" |
| 39 | |
| 40 | #include "alloc.h" |
| 41 | #include "dlmglue.h" |
| 42 | #include "extent_map.h" |
| 43 | #include "file.h" |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 44 | #include "heartbeat.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 45 | #include "inode.h" |
| 46 | #include "journal.h" |
| 47 | #include "namei.h" |
| 48 | #include "suballoc.h" |
| 49 | #include "super.h" |
| 50 | #include "symlink.h" |
| 51 | #include "sysfile.h" |
| 52 | #include "uptodate.h" |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 53 | #include "xattr.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 54 | |
| 55 | #include "buffer_head_io.h" |
| 56 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 57 | struct ocfs2_find_inode_args |
| 58 | { |
| 59 | u64 fi_blkno; |
| 60 | unsigned long fi_ino; |
| 61 | unsigned int fi_flags; |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 62 | unsigned int fi_sysfile_type; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 65 | static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES]; |
| 66 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 67 | static int ocfs2_read_locked_inode(struct inode *inode, |
| 68 | struct ocfs2_find_inode_args *args); |
| 69 | static int ocfs2_init_locked_inode(struct inode *inode, void *opaque); |
| 70 | static int ocfs2_find_actor(struct inode *inode, void *opaque); |
| 71 | static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, |
| 72 | struct inode *inode, |
| 73 | struct buffer_head *fe_bh); |
| 74 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 75 | void ocfs2_set_inode_flags(struct inode *inode) |
| 76 | { |
| 77 | unsigned int flags = OCFS2_I(inode)->ip_attr; |
| 78 | |
| 79 | inode->i_flags &= ~(S_IMMUTABLE | |
| 80 | S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC); |
| 81 | |
| 82 | if (flags & OCFS2_IMMUTABLE_FL) |
| 83 | inode->i_flags |= S_IMMUTABLE; |
| 84 | |
| 85 | if (flags & OCFS2_SYNC_FL) |
| 86 | inode->i_flags |= S_SYNC; |
| 87 | if (flags & OCFS2_APPEND_FL) |
| 88 | inode->i_flags |= S_APPEND; |
| 89 | if (flags & OCFS2_NOATIME_FL) |
| 90 | inode->i_flags |= S_NOATIME; |
| 91 | if (flags & OCFS2_DIRSYNC_FL) |
| 92 | inode->i_flags |= S_DIRSYNC; |
| 93 | } |
| 94 | |
Jan Kara | 6e4b0d5 | 2007-04-27 11:08:01 -0700 | [diff] [blame] | 95 | /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */ |
| 96 | void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi) |
| 97 | { |
| 98 | unsigned int flags = oi->vfs_inode.i_flags; |
| 99 | |
| 100 | oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL| |
| 101 | OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL); |
| 102 | if (flags & S_SYNC) |
| 103 | oi->ip_attr |= OCFS2_SYNC_FL; |
| 104 | if (flags & S_APPEND) |
| 105 | oi->ip_attr |= OCFS2_APPEND_FL; |
| 106 | if (flags & S_IMMUTABLE) |
| 107 | oi->ip_attr |= OCFS2_IMMUTABLE_FL; |
| 108 | if (flags & S_NOATIME) |
| 109 | oi->ip_attr |= OCFS2_NOATIME_FL; |
| 110 | if (flags & S_DIRSYNC) |
| 111 | oi->ip_attr |= OCFS2_DIRSYNC_FL; |
| 112 | } |
| 113 | |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 114 | struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags, |
| 115 | int sysfile_type) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 116 | { |
| 117 | struct inode *inode = NULL; |
| 118 | struct super_block *sb = osb->sb; |
| 119 | struct ocfs2_find_inode_args args; |
| 120 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 121 | mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 122 | |
| 123 | /* Ok. By now we've either got the offsets passed to us by the |
| 124 | * caller, or we just pulled them off the bh. Lets do some |
| 125 | * sanity checks to make sure they're OK. */ |
| 126 | if (blkno == 0) { |
| 127 | inode = ERR_PTR(-EINVAL); |
| 128 | mlog_errno(PTR_ERR(inode)); |
| 129 | goto bail; |
| 130 | } |
| 131 | |
| 132 | args.fi_blkno = blkno; |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 133 | args.fi_flags = flags; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 134 | args.fi_ino = ino_from_blkno(sb, blkno); |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 135 | args.fi_sysfile_type = sysfile_type; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 136 | |
| 137 | inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor, |
| 138 | ocfs2_init_locked_inode, &args); |
| 139 | /* inode was *not* in the inode cache. 2.6.x requires |
| 140 | * us to do our own read_inode call and unlock it |
| 141 | * afterwards. */ |
| 142 | if (inode && inode->i_state & I_NEW) { |
| 143 | mlog(0, "Inode was not in inode cache, reading it.\n"); |
| 144 | ocfs2_read_locked_inode(inode, &args); |
| 145 | unlock_new_inode(inode); |
| 146 | } |
| 147 | if (inode == NULL) { |
| 148 | inode = ERR_PTR(-ENOMEM); |
| 149 | mlog_errno(PTR_ERR(inode)); |
| 150 | goto bail; |
| 151 | } |
| 152 | if (is_bad_inode(inode)) { |
| 153 | iput(inode); |
| 154 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 155 | goto bail; |
| 156 | } |
| 157 | |
| 158 | bail: |
| 159 | if (!IS_ERR(inode)) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 160 | mlog(0, "returning inode with number %llu\n", |
| 161 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 162 | mlog_exit_ptr(inode); |
Mark Fasheh | 6a1bd4a | 2007-01-03 17:06:59 -0800 | [diff] [blame] | 163 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 164 | |
| 165 | return inode; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /* |
| 170 | * here's how inodes get read from disk: |
| 171 | * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR |
| 172 | * found? : return the in-memory inode |
| 173 | * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE |
| 174 | */ |
| 175 | |
| 176 | static int ocfs2_find_actor(struct inode *inode, void *opaque) |
| 177 | { |
| 178 | struct ocfs2_find_inode_args *args = NULL; |
| 179 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 180 | int ret = 0; |
| 181 | |
| 182 | mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque); |
| 183 | |
| 184 | args = opaque; |
| 185 | |
| 186 | mlog_bug_on_msg(!inode, "No inode in find actor!\n"); |
| 187 | |
| 188 | if (oi->ip_blkno != args->fi_blkno) |
| 189 | goto bail; |
| 190 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 191 | ret = 1; |
| 192 | bail: |
| 193 | mlog_exit(ret); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * initialize the new inode, but don't do anything that would cause |
| 199 | * us to sleep. |
| 200 | * return 0 on success, 1 on failure |
| 201 | */ |
| 202 | static int ocfs2_init_locked_inode(struct inode *inode, void *opaque) |
| 203 | { |
| 204 | struct ocfs2_find_inode_args *args = opaque; |
| 205 | |
| 206 | mlog_entry("inode = %p, opaque = %p\n", inode, opaque); |
| 207 | |
| 208 | inode->i_ino = args->fi_ino; |
| 209 | OCFS2_I(inode)->ip_blkno = args->fi_blkno; |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 210 | if (args->fi_sysfile_type != 0) |
| 211 | lockdep_set_class(&inode->i_mutex, |
| 212 | &ocfs2_sysfile_lock_key[args->fi_sysfile_type]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 213 | |
| 214 | mlog_exit(0); |
| 215 | return 0; |
| 216 | } |
| 217 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 218 | void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe, |
| 219 | int create_ino) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 220 | { |
| 221 | struct super_block *sb; |
| 222 | struct ocfs2_super *osb; |
Mark Fasheh | 53da493 | 2008-07-21 14:29:16 -0700 | [diff] [blame] | 223 | int use_plocks = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 224 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 225 | mlog_entry("(0x%p, size:%llu)\n", inode, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 226 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 227 | |
| 228 | sb = inode->i_sb; |
| 229 | osb = OCFS2_SB(sb); |
| 230 | |
Mark Fasheh | 53da493 | 2008-07-21 14:29:16 -0700 | [diff] [blame] | 231 | if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) || |
| 232 | ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks()) |
| 233 | use_plocks = 0; |
| 234 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 235 | /* |
| 236 | * These have all been checked by ocfs2_read_inode_block() or set |
| 237 | * by ocfs2_mknod_locked(), so a failure is a code bug. |
| 238 | */ |
| 239 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode |
| 240 | cannot create a superblock |
| 241 | inode today. change if |
| 242 | that is needed. */ |
| 243 | BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))); |
| 244 | BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 245 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 246 | |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 247 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
| 248 | OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); |
Mark Fasheh | 15b1e36 | 2007-09-07 13:58:15 -0700 | [diff] [blame] | 249 | OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features); |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 250 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 251 | inode->i_version = 1; |
| 252 | inode->i_generation = le32_to_cpu(fe->i_generation); |
| 253 | inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); |
| 254 | inode->i_mode = le16_to_cpu(fe->i_mode); |
| 255 | inode->i_uid = le32_to_cpu(fe->i_uid); |
| 256 | inode->i_gid = le32_to_cpu(fe->i_gid); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 257 | |
| 258 | /* Fast symlinks will have i_size but no allocated clusters. */ |
| 259 | if (S_ISLNK(inode->i_mode) && !fe->i_clusters) |
| 260 | inode->i_blocks = 0; |
| 261 | else |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 262 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 263 | inode->i_mapping->a_ops = &ocfs2_aops; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 264 | inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime); |
| 265 | inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec); |
| 266 | inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime); |
| 267 | inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec); |
| 268 | inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime); |
| 269 | inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec); |
| 270 | |
| 271 | if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno)) |
| 272 | mlog(ML_ERROR, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 273 | "ip_blkno %llu != i_blkno %llu!\n", |
| 274 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 275 | (unsigned long long)le64_to_cpu(fe->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 276 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 277 | inode->i_nlink = le16_to_cpu(fe->i_links_count); |
| 278 | |
Jan Kara | bbbd0eb | 2008-08-21 18:22:30 +0200 | [diff] [blame] | 279 | if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) { |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 280 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE; |
Jan Kara | bbbd0eb | 2008-08-21 18:22:30 +0200 | [diff] [blame] | 281 | inode->i_flags |= S_NOQUOTA; |
| 282 | } |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 283 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 284 | if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) { |
| 285 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; |
| 286 | mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino); |
| 287 | } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) { |
| 288 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; |
Jan Kara | 1a224ad | 2008-08-20 15:43:36 +0200 | [diff] [blame] | 289 | } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) { |
| 290 | inode->i_flags |= S_NOQUOTA; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 291 | } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) { |
| 292 | mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino); |
| 293 | /* we can't actually hit this as read_inode can't |
| 294 | * handle superblocks today ;-) */ |
| 295 | BUG(); |
| 296 | } |
| 297 | |
| 298 | switch (inode->i_mode & S_IFMT) { |
| 299 | case S_IFREG: |
Mark Fasheh | 53da493 | 2008-07-21 14:29:16 -0700 | [diff] [blame] | 300 | if (use_plocks) |
| 301 | inode->i_fop = &ocfs2_fops; |
| 302 | else |
| 303 | inode->i_fop = &ocfs2_fops_no_plocks; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 304 | inode->i_op = &ocfs2_file_iops; |
| 305 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
| 306 | break; |
| 307 | case S_IFDIR: |
| 308 | inode->i_op = &ocfs2_dir_iops; |
Mark Fasheh | 53da493 | 2008-07-21 14:29:16 -0700 | [diff] [blame] | 309 | if (use_plocks) |
| 310 | inode->i_fop = &ocfs2_dops; |
| 311 | else |
| 312 | inode->i_fop = &ocfs2_dops_no_plocks; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 313 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
| 314 | break; |
| 315 | case S_IFLNK: |
| 316 | if (ocfs2_inode_is_fast_symlink(inode)) |
| 317 | inode->i_op = &ocfs2_fast_symlink_inode_operations; |
| 318 | else |
| 319 | inode->i_op = &ocfs2_symlink_inode_operations; |
| 320 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
| 321 | break; |
| 322 | default: |
| 323 | inode->i_op = &ocfs2_special_file_iops; |
| 324 | init_special_inode(inode, inode->i_mode, |
| 325 | inode->i_rdev); |
| 326 | break; |
| 327 | } |
| 328 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 329 | if (create_ino) { |
| 330 | inode->i_ino = ino_from_blkno(inode->i_sb, |
| 331 | le64_to_cpu(fe->i_blkno)); |
| 332 | |
| 333 | /* |
| 334 | * If we ever want to create system files from kernel, |
| 335 | * the generation argument to |
| 336 | * ocfs2_inode_lock_res_init() will have to change. |
| 337 | */ |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 338 | BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 339 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 340 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 341 | OCFS2_LOCK_TYPE_META, 0, inode); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 342 | |
| 343 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, |
| 344 | OCFS2_LOCK_TYPE_OPEN, 0, inode); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 347 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres, |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 348 | OCFS2_LOCK_TYPE_RW, inode->i_generation, |
| 349 | inode); |
| 350 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 351 | ocfs2_set_inode_flags(inode); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 352 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 353 | mlog_exit_void(); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static int ocfs2_read_locked_inode(struct inode *inode, |
| 357 | struct ocfs2_find_inode_args *args) |
| 358 | { |
| 359 | struct super_block *sb; |
| 360 | struct ocfs2_super *osb; |
| 361 | struct ocfs2_dinode *fe; |
| 362 | struct buffer_head *bh = NULL; |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 363 | int status, can_lock; |
| 364 | u32 generation = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 365 | |
| 366 | mlog_entry("(0x%p, 0x%p)\n", inode, args); |
| 367 | |
| 368 | status = -EINVAL; |
| 369 | if (inode == NULL || inode->i_sb == NULL) { |
| 370 | mlog(ML_ERROR, "bad inode\n"); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 371 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 372 | } |
| 373 | sb = inode->i_sb; |
| 374 | osb = OCFS2_SB(sb); |
| 375 | |
| 376 | if (!args) { |
| 377 | mlog(ML_ERROR, "bad inode args\n"); |
| 378 | make_bad_inode(inode); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 379 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 382 | /* |
| 383 | * To improve performance of cold-cache inode stats, we take |
| 384 | * the cluster lock here if possible. |
| 385 | * |
| 386 | * Generally, OCFS2 never trusts the contents of an inode |
| 387 | * unless it's holding a cluster lock, so taking it here isn't |
| 388 | * a correctness issue as much as it is a performance |
| 389 | * improvement. |
| 390 | * |
| 391 | * There are three times when taking the lock is not a good idea: |
| 392 | * |
| 393 | * 1) During startup, before we have initialized the DLM. |
| 394 | * |
| 395 | * 2) If we are reading certain system files which never get |
| 396 | * cluster locks (local alloc, truncate log). |
| 397 | * |
| 398 | * 3) If the process doing the iget() is responsible for |
| 399 | * orphan dir recovery. We're holding the orphan dir lock and |
| 400 | * can get into a deadlock with another process on another |
| 401 | * node in ->delete_inode(). |
| 402 | * |
| 403 | * #1 and #2 can be simply solved by never taking the lock |
| 404 | * here for system files (which are the only type we read |
| 405 | * during mount). It's a heavier approach, but our main |
| 406 | * concern is user-accesible files anyway. |
| 407 | * |
| 408 | * #3 works itself out because we'll eventually take the |
| 409 | * cluster lock before trusting anything anyway. |
| 410 | */ |
| 411 | can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE) |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 412 | && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) |
Sunil Mushran | c271c5c | 2006-12-05 17:56:35 -0800 | [diff] [blame] | 413 | && !ocfs2_mount_local(osb); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * To maintain backwards compatibility with older versions of |
| 417 | * ocfs2-tools, we still store the generation value for system |
| 418 | * files. The only ones that actually matter to userspace are |
| 419 | * the journals, but it's easier and inexpensive to just flag |
| 420 | * all system files similarly. |
| 421 | */ |
| 422 | if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE) |
| 423 | generation = osb->fs_generation; |
| 424 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 425 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 426 | OCFS2_LOCK_TYPE_META, |
| 427 | generation, inode); |
| 428 | |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 429 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, |
| 430 | OCFS2_LOCK_TYPE_OPEN, |
| 431 | 0, inode); |
| 432 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 433 | if (can_lock) { |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 434 | status = ocfs2_open_lock(inode); |
| 435 | if (status) { |
| 436 | make_bad_inode(inode); |
| 437 | mlog_errno(status); |
| 438 | return status; |
| 439 | } |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 440 | status = ocfs2_inode_lock(inode, NULL, 0); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 441 | if (status) { |
| 442 | make_bad_inode(inode); |
| 443 | mlog_errno(status); |
| 444 | return status; |
| 445 | } |
| 446 | } |
| 447 | |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 448 | if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) { |
| 449 | status = ocfs2_try_open_lock(inode, 0); |
| 450 | if (status) { |
| 451 | make_bad_inode(inode); |
| 452 | return status; |
| 453 | } |
| 454 | } |
| 455 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 456 | if (can_lock) { |
| 457 | status = ocfs2_read_inode_block_full(inode, &bh, |
| 458 | OCFS2_BH_IGNORE_CACHE); |
| 459 | } else { |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 460 | status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh); |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 461 | if (!status) |
| 462 | status = ocfs2_validate_inode_block(osb->sb, bh); |
| 463 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 464 | if (status < 0) { |
| 465 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 466 | goto bail; |
| 467 | } |
| 468 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 469 | status = -EINVAL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 470 | fe = (struct ocfs2_dinode *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 471 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 472 | /* |
| 473 | * This is a code bug. Right now the caller needs to |
| 474 | * understand whether it is asking for a system file inode or |
| 475 | * not so the proper lock names can be built. |
| 476 | */ |
| 477 | mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) != |
| 478 | !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE), |
| 479 | "Inode %llu: system file state is ambigous\n", |
| 480 | (unsigned long long)args->fi_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 481 | |
| 482 | if (S_ISCHR(le16_to_cpu(fe->i_mode)) || |
| 483 | S_ISBLK(le16_to_cpu(fe->i_mode))) |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 484 | inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 485 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 486 | ocfs2_populate_inode(inode, fe, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 487 | |
| 488 | BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno)); |
| 489 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 490 | status = 0; |
| 491 | |
| 492 | bail: |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 493 | if (can_lock) |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 494 | ocfs2_inode_unlock(inode, 0); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 495 | |
| 496 | if (status < 0) |
| 497 | make_bad_inode(inode); |
| 498 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 499 | if (args && bh) |
| 500 | brelse(bh); |
| 501 | |
| 502 | mlog_exit(status); |
| 503 | return status; |
| 504 | } |
| 505 | |
| 506 | void ocfs2_sync_blockdev(struct super_block *sb) |
| 507 | { |
| 508 | sync_blockdev(sb->s_bdev); |
| 509 | } |
| 510 | |
| 511 | static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, |
| 512 | struct inode *inode, |
| 513 | struct buffer_head *fe_bh) |
| 514 | { |
| 515 | int status = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 516 | struct ocfs2_truncate_context *tc = NULL; |
| 517 | struct ocfs2_dinode *fe; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 518 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 519 | |
| 520 | mlog_entry_void(); |
| 521 | |
| 522 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 523 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 524 | /* |
| 525 | * This check will also skip truncate of inodes with inline |
| 526 | * data and fast symlinks. |
| 527 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 528 | if (fe->i_clusters) { |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 529 | if (ocfs2_should_order_data(inode)) |
| 530 | ocfs2_begin_ordered_truncate(inode, 0); |
| 531 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 532 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 533 | if (IS_ERR(handle)) { |
| 534 | status = PTR_ERR(handle); |
| 535 | mlog_errno(status); |
| 536 | goto out; |
| 537 | } |
| 538 | |
| 539 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 540 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 541 | if (status < 0) { |
| 542 | mlog_errno(status); |
| 543 | goto out; |
| 544 | } |
| 545 | |
| 546 | i_size_write(inode, 0); |
| 547 | |
| 548 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); |
| 549 | if (status < 0) { |
| 550 | mlog_errno(status); |
| 551 | goto out; |
| 552 | } |
| 553 | |
| 554 | ocfs2_commit_trans(osb, handle); |
| 555 | handle = NULL; |
| 556 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 557 | status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc); |
| 558 | if (status < 0) { |
| 559 | mlog_errno(status); |
| 560 | goto out; |
| 561 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 562 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 563 | status = ocfs2_commit_truncate(osb, inode, fe_bh, tc); |
| 564 | if (status < 0) { |
| 565 | mlog_errno(status); |
| 566 | goto out; |
| 567 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 568 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 569 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 570 | out: |
| 571 | if (handle) |
| 572 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 573 | mlog_exit(status); |
| 574 | return status; |
| 575 | } |
| 576 | |
| 577 | static int ocfs2_remove_inode(struct inode *inode, |
| 578 | struct buffer_head *di_bh, |
| 579 | struct inode *orphan_dir_inode, |
| 580 | struct buffer_head *orphan_dir_bh) |
| 581 | { |
| 582 | int status; |
| 583 | struct inode *inode_alloc_inode = NULL; |
| 584 | struct buffer_head *inode_alloc_bh = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 585 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 586 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 587 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; |
| 588 | |
| 589 | inode_alloc_inode = |
| 590 | ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, |
| 591 | le16_to_cpu(di->i_suballoc_slot)); |
| 592 | if (!inode_alloc_inode) { |
| 593 | status = -EEXIST; |
| 594 | mlog_errno(status); |
| 595 | goto bail; |
| 596 | } |
| 597 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 598 | mutex_lock(&inode_alloc_inode->i_mutex); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 599 | status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 600 | if (status < 0) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 601 | mutex_unlock(&inode_alloc_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 602 | |
| 603 | mlog_errno(status); |
| 604 | goto bail; |
| 605 | } |
| 606 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame^] | 607 | handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS + |
| 608 | ocfs2_quota_trans_credits(inode->i_sb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 609 | if (IS_ERR(handle)) { |
| 610 | status = PTR_ERR(handle); |
| 611 | mlog_errno(status); |
| 612 | goto bail_unlock; |
| 613 | } |
| 614 | |
| 615 | status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode, |
| 616 | orphan_dir_bh); |
| 617 | if (status < 0) { |
| 618 | mlog_errno(status); |
| 619 | goto bail_commit; |
| 620 | } |
| 621 | |
| 622 | /* set the inodes dtime */ |
| 623 | status = ocfs2_journal_access(handle, inode, di_bh, |
| 624 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 625 | if (status < 0) { |
| 626 | mlog_errno(status); |
| 627 | goto bail_commit; |
| 628 | } |
| 629 | |
| 630 | di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec); |
Marcin Slusarz | 4092d49 | 2007-12-25 15:52:59 +0100 | [diff] [blame] | 631 | di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 632 | |
| 633 | status = ocfs2_journal_dirty(handle, di_bh); |
| 634 | if (status < 0) { |
| 635 | mlog_errno(status); |
| 636 | goto bail_commit; |
| 637 | } |
| 638 | |
| 639 | ocfs2_remove_from_cache(inode, di_bh); |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame^] | 640 | vfs_dq_free_inode(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 641 | |
| 642 | status = ocfs2_free_dinode(handle, inode_alloc_inode, |
| 643 | inode_alloc_bh, di); |
| 644 | if (status < 0) |
| 645 | mlog_errno(status); |
| 646 | |
| 647 | bail_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 648 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 649 | bail_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 650 | ocfs2_inode_unlock(inode_alloc_inode, 1); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 651 | mutex_unlock(&inode_alloc_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 652 | brelse(inode_alloc_bh); |
| 653 | bail: |
| 654 | iput(inode_alloc_inode); |
| 655 | |
| 656 | return status; |
| 657 | } |
| 658 | |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 659 | /* |
| 660 | * Serialize with orphan dir recovery. If the process doing |
| 661 | * recovery on this orphan dir does an iget() with the dir |
| 662 | * i_mutex held, we'll deadlock here. Instead we detect this |
| 663 | * and exit early - recovery will wipe this inode for us. |
| 664 | */ |
| 665 | static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb, |
| 666 | int slot) |
| 667 | { |
| 668 | int ret = 0; |
| 669 | |
| 670 | spin_lock(&osb->osb_lock); |
| 671 | if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) { |
| 672 | mlog(0, "Recovery is happening on orphan dir %d, will skip " |
| 673 | "this inode\n", slot); |
| 674 | ret = -EDEADLK; |
| 675 | goto out; |
| 676 | } |
| 677 | /* This signals to the orphan recovery process that it should |
| 678 | * wait for us to handle the wipe. */ |
| 679 | osb->osb_orphan_wipes[slot]++; |
| 680 | out: |
| 681 | spin_unlock(&osb->osb_lock); |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb, |
| 686 | int slot) |
| 687 | { |
| 688 | spin_lock(&osb->osb_lock); |
| 689 | osb->osb_orphan_wipes[slot]--; |
| 690 | spin_unlock(&osb->osb_lock); |
| 691 | |
| 692 | wake_up(&osb->osb_wipe_event); |
| 693 | } |
| 694 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 695 | static int ocfs2_wipe_inode(struct inode *inode, |
| 696 | struct buffer_head *di_bh) |
| 697 | { |
| 698 | int status, orphaned_slot; |
| 699 | struct inode *orphan_dir_inode = NULL; |
| 700 | struct buffer_head *orphan_dir_bh = NULL; |
| 701 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 702 | struct ocfs2_dinode *di; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 703 | |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 704 | di = (struct ocfs2_dinode *) di_bh->b_data; |
| 705 | orphaned_slot = le16_to_cpu(di->i_orphaned_slot); |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 706 | |
| 707 | status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot); |
| 708 | if (status) |
| 709 | return status; |
| 710 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 711 | orphan_dir_inode = ocfs2_get_system_file_inode(osb, |
| 712 | ORPHAN_DIR_SYSTEM_INODE, |
| 713 | orphaned_slot); |
| 714 | if (!orphan_dir_inode) { |
| 715 | status = -EEXIST; |
| 716 | mlog_errno(status); |
| 717 | goto bail; |
| 718 | } |
| 719 | |
| 720 | /* Lock the orphan dir. The lock will be held for the entire |
| 721 | * delete_inode operation. We do this now to avoid races with |
| 722 | * recovery completion on other nodes. */ |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 723 | mutex_lock(&orphan_dir_inode->i_mutex); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 724 | status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 725 | if (status < 0) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 726 | mutex_unlock(&orphan_dir_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 727 | |
| 728 | mlog_errno(status); |
| 729 | goto bail; |
| 730 | } |
| 731 | |
| 732 | /* we do this while holding the orphan dir lock because we |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 733 | * don't want recovery being run from another node to try an |
| 734 | * inode delete underneath us -- this will result in two nodes |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 735 | * truncating the same file! */ |
| 736 | status = ocfs2_truncate_for_delete(osb, inode, di_bh); |
| 737 | if (status < 0) { |
| 738 | mlog_errno(status); |
| 739 | goto bail_unlock_dir; |
| 740 | } |
| 741 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 742 | /*Free extended attribute resources associated with this inode.*/ |
| 743 | status = ocfs2_xattr_remove(inode, di_bh); |
| 744 | if (status < 0) { |
| 745 | mlog_errno(status); |
| 746 | goto bail_unlock_dir; |
| 747 | } |
| 748 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 749 | status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode, |
| 750 | orphan_dir_bh); |
| 751 | if (status < 0) |
| 752 | mlog_errno(status); |
| 753 | |
| 754 | bail_unlock_dir: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 755 | ocfs2_inode_unlock(orphan_dir_inode, 1); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 756 | mutex_unlock(&orphan_dir_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 757 | brelse(orphan_dir_bh); |
| 758 | bail: |
| 759 | iput(orphan_dir_inode); |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 760 | ocfs2_signal_wipe_completion(osb, orphaned_slot); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 761 | |
| 762 | return status; |
| 763 | } |
| 764 | |
| 765 | /* There is a series of simple checks that should be done before a |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 766 | * trylock is even considered. Encapsulate those in this function. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 767 | static int ocfs2_inode_is_valid_to_delete(struct inode *inode) |
| 768 | { |
| 769 | int ret = 0; |
| 770 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 771 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 772 | |
| 773 | /* We shouldn't be getting here for the root directory |
| 774 | * inode.. */ |
| 775 | if (inode == osb->root_inode) { |
| 776 | mlog(ML_ERROR, "Skipping delete of root inode.\n"); |
| 777 | goto bail; |
| 778 | } |
| 779 | |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 780 | /* If we're coming from downconvert_thread we can't go into our own |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 781 | * voting [hello, deadlock city!], so unforuntately we just |
| 782 | * have to skip deleting this guy. That's OK though because |
| 783 | * the node who's doing the actual deleting should handle it |
| 784 | * anyway. */ |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 785 | if (current == osb->dc_task) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 786 | mlog(0, "Skipping delete of %lu because we're currently " |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 787 | "in downconvert\n", inode->i_ino); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 788 | goto bail; |
| 789 | } |
| 790 | |
| 791 | spin_lock(&oi->ip_lock); |
| 792 | /* OCFS2 *never* deletes system files. This should technically |
| 793 | * never get here as system file inodes should always have a |
| 794 | * positive link count. */ |
| 795 | if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 796 | mlog(ML_ERROR, "Skipping delete of system file %llu\n", |
| 797 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 798 | goto bail_unlock; |
| 799 | } |
| 800 | |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 801 | /* If we have allowd wipe of this inode for another node, it |
| 802 | * will be marked here so we can safely skip it. Recovery will |
| 803 | * cleanup any inodes we might inadvertantly skip here. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 804 | if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) { |
| 805 | mlog(0, "Skipping delete of %lu because another node " |
| 806 | "has done this for us.\n", inode->i_ino); |
| 807 | goto bail_unlock; |
| 808 | } |
| 809 | |
| 810 | ret = 1; |
| 811 | bail_unlock: |
| 812 | spin_unlock(&oi->ip_lock); |
| 813 | bail: |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | /* Query the cluster to determine whether we should wipe an inode from |
| 818 | * disk or not. |
| 819 | * |
| 820 | * Requires the inode to have the cluster lock. */ |
| 821 | static int ocfs2_query_inode_wipe(struct inode *inode, |
| 822 | struct buffer_head *di_bh, |
| 823 | int *wipe) |
| 824 | { |
| 825 | int status = 0; |
| 826 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 827 | struct ocfs2_dinode *di; |
| 828 | |
| 829 | *wipe = 0; |
| 830 | |
| 831 | /* While we were waiting for the cluster lock in |
| 832 | * ocfs2_delete_inode, another node might have asked to delete |
| 833 | * the inode. Recheck our flags to catch this. */ |
| 834 | if (!ocfs2_inode_is_valid_to_delete(inode)) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 835 | mlog(0, "Skipping delete of %llu because flags changed\n", |
| 836 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 837 | goto bail; |
| 838 | } |
| 839 | |
| 840 | /* Now that we have an up to date inode, we can double check |
| 841 | * the link count. */ |
| 842 | if (inode->i_nlink) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 843 | mlog(0, "Skipping delete of %llu because nlink = %u\n", |
| 844 | (unsigned long long)oi->ip_blkno, inode->i_nlink); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 845 | goto bail; |
| 846 | } |
| 847 | |
| 848 | /* Do some basic inode verification... */ |
| 849 | di = (struct ocfs2_dinode *) di_bh->b_data; |
| 850 | if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) { |
| 851 | /* for lack of a better error? */ |
| 852 | status = -EEXIST; |
| 853 | mlog(ML_ERROR, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 854 | "Inode %llu (on-disk %llu) not orphaned! " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 855 | "Disk flags 0x%x, inode flags 0x%x\n", |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 856 | (unsigned long long)oi->ip_blkno, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 857 | (unsigned long long)le64_to_cpu(di->i_blkno), |
| 858 | le32_to_cpu(di->i_flags), oi->ip_flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 859 | goto bail; |
| 860 | } |
| 861 | |
| 862 | /* has someone already deleted us?! baaad... */ |
| 863 | if (di->i_dtime) { |
| 864 | status = -EEXIST; |
| 865 | mlog_errno(status); |
| 866 | goto bail; |
| 867 | } |
| 868 | |
Mark Fasheh | 6f16bf6 | 2007-03-20 17:17:54 -0700 | [diff] [blame] | 869 | /* |
| 870 | * This is how ocfs2 determines whether an inode is still live |
| 871 | * within the cluster. Every node takes a shared read lock on |
| 872 | * the inode open lock in ocfs2_read_locked_inode(). When we |
| 873 | * get to ->delete_inode(), each node tries to convert it's |
| 874 | * lock to an exclusive. Trylocks are serialized by the inode |
| 875 | * meta data lock. If the upconvert suceeds, we know the inode |
| 876 | * is no longer live and can be deleted. |
| 877 | * |
| 878 | * Though we call this with the meta data lock held, the |
| 879 | * trylock keeps us from ABBA deadlock. |
| 880 | */ |
| 881 | status = ocfs2_try_open_lock(inode, 1); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 882 | if (status == -EAGAIN) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 883 | status = 0; |
Joe Perches | 2759236 | 2007-11-19 17:53:34 -0800 | [diff] [blame] | 884 | mlog(0, "Skipping delete of %llu because it is in use on " |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 885 | "other nodes\n", (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 886 | goto bail; |
| 887 | } |
| 888 | if (status < 0) { |
| 889 | mlog_errno(status); |
| 890 | goto bail; |
| 891 | } |
| 892 | |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 893 | *wipe = 1; |
| 894 | mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n", |
| 895 | (unsigned long long)oi->ip_blkno, |
| 896 | le16_to_cpu(di->i_orphaned_slot)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 897 | |
| 898 | bail: |
| 899 | return status; |
| 900 | } |
| 901 | |
| 902 | /* Support function for ocfs2_delete_inode. Will help us keep the |
| 903 | * inode data in a consistent state for clear_inode. Always truncates |
| 904 | * pages, optionally sync's them first. */ |
| 905 | static void ocfs2_cleanup_delete_inode(struct inode *inode, |
| 906 | int sync_data) |
| 907 | { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 908 | mlog(0, "Cleanup inode %llu, sync = %d\n", |
| 909 | (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 910 | if (sync_data) |
| 911 | write_inode_now(inode, 1); |
| 912 | truncate_inode_pages(&inode->i_data, 0); |
| 913 | } |
| 914 | |
| 915 | void ocfs2_delete_inode(struct inode *inode) |
| 916 | { |
| 917 | int wipe, status; |
| 918 | sigset_t blocked, oldset; |
| 919 | struct buffer_head *di_bh = NULL; |
| 920 | |
| 921 | mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino); |
| 922 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame^] | 923 | /* When we fail in read_inode() we mark inode as bad. The second test |
| 924 | * catches the case when inode allocation fails before allocating |
| 925 | * a block for inode. */ |
| 926 | if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 927 | mlog(0, "Skipping delete of bad inode\n"); |
| 928 | goto bail; |
| 929 | } |
| 930 | |
| 931 | if (!ocfs2_inode_is_valid_to_delete(inode)) { |
| 932 | /* It's probably not necessary to truncate_inode_pages |
| 933 | * here but we do it for safety anyway (it will most |
| 934 | * likely be a no-op anyway) */ |
| 935 | ocfs2_cleanup_delete_inode(inode, 0); |
| 936 | goto bail; |
| 937 | } |
| 938 | |
| 939 | /* We want to block signals in delete_inode as the lock and |
| 940 | * messaging paths may return us -ERESTARTSYS. Which would |
| 941 | * cause us to exit early, resulting in inodes being orphaned |
| 942 | * forever. */ |
| 943 | sigfillset(&blocked); |
| 944 | status = sigprocmask(SIG_BLOCK, &blocked, &oldset); |
| 945 | if (status < 0) { |
| 946 | mlog_errno(status); |
| 947 | ocfs2_cleanup_delete_inode(inode, 1); |
| 948 | goto bail; |
| 949 | } |
| 950 | |
| 951 | /* Lock down the inode. This gives us an up to date view of |
| 952 | * it's metadata (for verification), and allows us to |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 953 | * serialize delete_inode on multiple nodes. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 954 | * |
| 955 | * Even though we might be doing a truncate, we don't take the |
| 956 | * allocation lock here as it won't be needed - nobody will |
| 957 | * have the file open. |
| 958 | */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 959 | status = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 960 | if (status < 0) { |
| 961 | if (status != -ENOENT) |
| 962 | mlog_errno(status); |
| 963 | ocfs2_cleanup_delete_inode(inode, 0); |
| 964 | goto bail_unblock; |
| 965 | } |
| 966 | |
| 967 | /* Query the cluster. This will be the final decision made |
| 968 | * before we go ahead and wipe the inode. */ |
| 969 | status = ocfs2_query_inode_wipe(inode, di_bh, &wipe); |
| 970 | if (!wipe || status < 0) { |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 971 | /* Error and remote inode busy both mean we won't be |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 972 | * removing the inode, so they take almost the same |
| 973 | * path. */ |
| 974 | if (status < 0) |
| 975 | mlog_errno(status); |
| 976 | |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 977 | /* Someone in the cluster has disallowed a wipe of |
| 978 | * this inode, or it was never completely |
| 979 | * orphaned. Write out the pages and exit now. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 980 | ocfs2_cleanup_delete_inode(inode, 1); |
| 981 | goto bail_unlock_inode; |
| 982 | } |
| 983 | |
| 984 | ocfs2_cleanup_delete_inode(inode, 0); |
| 985 | |
| 986 | status = ocfs2_wipe_inode(inode, di_bh); |
| 987 | if (status < 0) { |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 988 | if (status != -EDEADLK) |
| 989 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 990 | goto bail_unlock_inode; |
| 991 | } |
| 992 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 993 | /* |
| 994 | * Mark the inode as successfully deleted. |
| 995 | * |
| 996 | * This is important for ocfs2_clear_inode() as it will check |
| 997 | * this flag and skip any checkpointing work |
| 998 | * |
| 999 | * ocfs2_stuff_meta_lvb() also uses this flag to invalidate |
| 1000 | * the LVB for other nodes. |
| 1001 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1002 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED; |
| 1003 | |
| 1004 | bail_unlock_inode: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1005 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1006 | brelse(di_bh); |
| 1007 | bail_unblock: |
| 1008 | status = sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 1009 | if (status < 0) |
| 1010 | mlog_errno(status); |
| 1011 | bail: |
| 1012 | clear_inode(inode); |
| 1013 | mlog_exit_void(); |
| 1014 | } |
| 1015 | |
| 1016 | void ocfs2_clear_inode(struct inode *inode) |
| 1017 | { |
| 1018 | int status; |
| 1019 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1020 | |
| 1021 | mlog_entry_void(); |
| 1022 | |
| 1023 | if (!inode) |
| 1024 | goto bail; |
| 1025 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1026 | mlog(0, "Clearing inode: %llu, nlink = %u\n", |
| 1027 | (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1028 | |
| 1029 | mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL, |
| 1030 | "Inode=%lu\n", inode->i_ino); |
| 1031 | |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 1032 | /* To preven remote deletes we hold open lock before, now it |
| 1033 | * is time to unlock PR and EX open locks. */ |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 1034 | ocfs2_open_unlock(inode); |
| 1035 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1036 | /* Do these before all the other work so that we don't bounce |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 1037 | * the downconvert thread while waiting to destroy the locks. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1038 | ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1039 | ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 1040 | ocfs2_mark_lockres_freeing(&oi->ip_open_lockres); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1041 | |
| 1042 | /* We very well may get a clear_inode before all an inodes |
| 1043 | * metadata has hit disk. Of course, we can't drop any cluster |
| 1044 | * locks until the journal has finished with it. The only |
| 1045 | * exception here are successfully wiped inodes - their |
| 1046 | * metadata can now be considered to be part of the system |
| 1047 | * inodes from which it came. */ |
| 1048 | if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED)) |
| 1049 | ocfs2_checkpoint_inode(inode); |
| 1050 | |
| 1051 | mlog_bug_on_msg(!list_empty(&oi->ip_io_markers), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1052 | "Clear inode of %llu, inode has io markers\n", |
| 1053 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1054 | |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 1055 | ocfs2_extent_map_trunc(inode, 0); |
| 1056 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1057 | status = ocfs2_drop_inode_locks(inode); |
| 1058 | if (status < 0) |
| 1059 | mlog_errno(status); |
| 1060 | |
| 1061 | ocfs2_lock_res_free(&oi->ip_rw_lockres); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1062 | ocfs2_lock_res_free(&oi->ip_inode_lockres); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 1063 | ocfs2_lock_res_free(&oi->ip_open_lockres); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1064 | |
| 1065 | ocfs2_metadata_cache_purge(inode); |
| 1066 | |
| 1067 | mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1068 | "Clear inode of %llu, inode has %u cache items\n", |
| 1069 | (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1070 | |
| 1071 | mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1072 | "Clear inode of %llu, inode has a bad flag\n", |
| 1073 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1074 | |
| 1075 | mlog_bug_on_msg(spin_is_locked(&oi->ip_lock), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1076 | "Clear inode of %llu, inode is locked\n", |
| 1077 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1078 | |
Mark Fasheh | 251b6ec | 2006-01-10 15:41:43 -0800 | [diff] [blame] | 1079 | mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1080 | "Clear inode of %llu, io_mutex is locked\n", |
| 1081 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | 251b6ec | 2006-01-10 15:41:43 -0800 | [diff] [blame] | 1082 | mutex_unlock(&oi->ip_io_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1083 | |
| 1084 | /* |
| 1085 | * down_trylock() returns 0, down_write_trylock() returns 1 |
| 1086 | * kernel 1, world 0 |
| 1087 | */ |
| 1088 | mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1089 | "Clear inode of %llu, alloc_sem is locked\n", |
| 1090 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1091 | up_write(&oi->ip_alloc_sem); |
| 1092 | |
| 1093 | mlog_bug_on_msg(oi->ip_open_count, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1094 | "Clear inode of %llu has open count %d\n", |
| 1095 | (unsigned long long)oi->ip_blkno, oi->ip_open_count); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1096 | |
| 1097 | /* Clear all other flags. */ |
| 1098 | oi->ip_flags = OCFS2_INODE_CACHE_INLINE; |
| 1099 | oi->ip_created_trans = 0; |
| 1100 | oi->ip_last_trans = 0; |
| 1101 | oi->ip_dir_start_lookup = 0; |
| 1102 | oi->ip_blkno = 0ULL; |
Sunil Mushran | ae0dff6 | 2008-10-22 13:24:29 -0700 | [diff] [blame] | 1103 | |
| 1104 | /* |
| 1105 | * ip_jinode is used to track txns against this inode. We ensure that |
| 1106 | * the journal is flushed before journal shutdown. Thus it is safe to |
| 1107 | * have inodes get cleaned up after journal shutdown. |
| 1108 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 1109 | jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal, |
| 1110 | &oi->ip_jinode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1111 | |
| 1112 | bail: |
| 1113 | mlog_exit_void(); |
| 1114 | } |
| 1115 | |
| 1116 | /* Called under inode_lock, with no more references on the |
| 1117 | * struct inode, so it's safe here to check the flags field |
| 1118 | * and to manipulate i_nlink without any other locks. */ |
| 1119 | void ocfs2_drop_inode(struct inode *inode) |
| 1120 | { |
| 1121 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1122 | |
| 1123 | mlog_entry_void(); |
| 1124 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1125 | mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n", |
| 1126 | (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1127 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1128 | if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED) |
| 1129 | generic_delete_inode(inode); |
| 1130 | else |
| 1131 | generic_drop_inode(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1132 | |
| 1133 | mlog_exit_void(); |
| 1134 | } |
| 1135 | |
| 1136 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1137 | * This is called from our getattr. |
| 1138 | */ |
| 1139 | int ocfs2_inode_revalidate(struct dentry *dentry) |
| 1140 | { |
| 1141 | struct inode *inode = dentry->d_inode; |
| 1142 | int status = 0; |
| 1143 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1144 | mlog_entry("(inode = 0x%p, ino = %llu)\n", inode, |
| 1145 | inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1146 | |
| 1147 | if (!inode) { |
| 1148 | mlog(0, "eep, no inode!\n"); |
| 1149 | status = -ENOENT; |
| 1150 | goto bail; |
| 1151 | } |
| 1152 | |
| 1153 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1154 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { |
| 1155 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1156 | mlog(0, "inode deleted!\n"); |
| 1157 | status = -ENOENT; |
| 1158 | goto bail; |
| 1159 | } |
| 1160 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1161 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1162 | /* Let ocfs2_inode_lock do the work of updating our struct |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1163 | * inode for us. */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1164 | status = ocfs2_inode_lock(inode, NULL, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1165 | if (status < 0) { |
| 1166 | if (status != -ENOENT) |
| 1167 | mlog_errno(status); |
| 1168 | goto bail; |
| 1169 | } |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1170 | ocfs2_inode_unlock(inode, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1171 | bail: |
| 1172 | mlog_exit(status); |
| 1173 | |
| 1174 | return status; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Updates a disk inode from a |
| 1179 | * struct inode. |
| 1180 | * Only takes ip_lock. |
| 1181 | */ |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1182 | int ocfs2_mark_inode_dirty(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1183 | struct inode *inode, |
| 1184 | struct buffer_head *bh) |
| 1185 | { |
| 1186 | int status; |
| 1187 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; |
| 1188 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1189 | mlog_entry("(inode %llu)\n", |
| 1190 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1191 | |
| 1192 | status = ocfs2_journal_access(handle, inode, bh, |
| 1193 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1194 | if (status < 0) { |
| 1195 | mlog_errno(status); |
| 1196 | goto leave; |
| 1197 | } |
| 1198 | |
| 1199 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1200 | fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters); |
Jan Kara | 6e4b0d5 | 2007-04-27 11:08:01 -0700 | [diff] [blame] | 1201 | ocfs2_get_inode_flags(OCFS2_I(inode)); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1202 | fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr); |
Mark Fasheh | 15b1e36 | 2007-09-07 13:58:15 -0700 | [diff] [blame] | 1203 | fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1204 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1205 | |
| 1206 | fe->i_size = cpu_to_le64(i_size_read(inode)); |
| 1207 | fe->i_links_count = cpu_to_le16(inode->i_nlink); |
| 1208 | fe->i_uid = cpu_to_le32(inode->i_uid); |
| 1209 | fe->i_gid = cpu_to_le32(inode->i_gid); |
| 1210 | fe->i_mode = cpu_to_le16(inode->i_mode); |
| 1211 | fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec); |
| 1212 | fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); |
| 1213 | fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 1214 | fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 1215 | fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); |
| 1216 | fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); |
| 1217 | |
| 1218 | status = ocfs2_journal_dirty(handle, bh); |
| 1219 | if (status < 0) |
| 1220 | mlog_errno(status); |
| 1221 | |
| 1222 | status = 0; |
| 1223 | leave: |
| 1224 | |
| 1225 | mlog_exit(status); |
| 1226 | return status; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * |
| 1231 | * Updates a struct inode from a disk inode. |
| 1232 | * does no i/o, only takes ip_lock. |
| 1233 | */ |
| 1234 | void ocfs2_refresh_inode(struct inode *inode, |
| 1235 | struct ocfs2_dinode *fe) |
| 1236 | { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1237 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1238 | |
| 1239 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1240 | OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); |
Mark Fasheh | 15b1e36 | 2007-09-07 13:58:15 -0700 | [diff] [blame] | 1241 | OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1242 | ocfs2_set_inode_flags(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1243 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
| 1244 | inode->i_nlink = le16_to_cpu(fe->i_links_count); |
| 1245 | inode->i_uid = le32_to_cpu(fe->i_uid); |
| 1246 | inode->i_gid = le32_to_cpu(fe->i_gid); |
| 1247 | inode->i_mode = le16_to_cpu(fe->i_mode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1248 | if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0) |
| 1249 | inode->i_blocks = 0; |
| 1250 | else |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1251 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1252 | inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime); |
| 1253 | inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec); |
| 1254 | inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime); |
| 1255 | inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec); |
| 1256 | inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime); |
| 1257 | inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec); |
| 1258 | |
| 1259 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1260 | } |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1261 | |
| 1262 | int ocfs2_validate_inode_block(struct super_block *sb, |
| 1263 | struct buffer_head *bh) |
| 1264 | { |
| 1265 | int rc = -EINVAL; |
| 1266 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; |
| 1267 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 1268 | mlog(0, "Validating dinode %llu\n", |
| 1269 | (unsigned long long)bh->b_blocknr); |
| 1270 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1271 | BUG_ON(!buffer_uptodate(bh)); |
| 1272 | |
| 1273 | if (!OCFS2_IS_VALID_DINODE(di)) { |
| 1274 | ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n", |
| 1275 | (unsigned long long)bh->b_blocknr, 7, |
| 1276 | di->i_signature); |
| 1277 | goto bail; |
| 1278 | } |
| 1279 | |
| 1280 | if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { |
| 1281 | ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n", |
| 1282 | (unsigned long long)bh->b_blocknr, |
| 1283 | (unsigned long long)le64_to_cpu(di->i_blkno)); |
| 1284 | goto bail; |
| 1285 | } |
| 1286 | |
| 1287 | if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { |
| 1288 | ocfs2_error(sb, |
| 1289 | "Invalid dinode #%llu: OCFS2_VALID_FL not set\n", |
| 1290 | (unsigned long long)bh->b_blocknr); |
| 1291 | goto bail; |
| 1292 | } |
| 1293 | |
| 1294 | if (le32_to_cpu(di->i_fs_generation) != |
| 1295 | OCFS2_SB(sb)->fs_generation) { |
| 1296 | ocfs2_error(sb, |
| 1297 | "Invalid dinode #%llu: fs_generation is %u\n", |
| 1298 | (unsigned long long)bh->b_blocknr, |
| 1299 | le32_to_cpu(di->i_fs_generation)); |
| 1300 | goto bail; |
| 1301 | } |
| 1302 | |
| 1303 | rc = 0; |
| 1304 | |
| 1305 | bail: |
| 1306 | return rc; |
| 1307 | } |
| 1308 | |
| 1309 | int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh, |
| 1310 | int flags) |
| 1311 | { |
| 1312 | int rc; |
| 1313 | struct buffer_head *tmp = *bh; |
| 1314 | |
| 1315 | rc = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, &tmp, |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 1316 | flags, ocfs2_validate_inode_block); |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1317 | |
| 1318 | /* If ocfs2_read_blocks() got us a new bh, pass it up. */ |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 1319 | if (!rc && !*bh) |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1320 | *bh = tmp; |
| 1321 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1322 | return rc; |
| 1323 | } |
| 1324 | |
| 1325 | int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh) |
| 1326 | { |
| 1327 | return ocfs2_read_inode_block_full(inode, bh, 0); |
| 1328 | } |