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> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 28 | #include <linux/highmem.h> |
| 29 | #include <linux/pagemap.h> |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 30 | #include <linux/quotaops.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 31 | |
| 32 | #include <asm/byteorder.h> |
| 33 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 34 | #include <cluster/masklog.h> |
| 35 | |
| 36 | #include "ocfs2.h" |
| 37 | |
| 38 | #include "alloc.h" |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 39 | #include "dir.h" |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 40 | #include "blockcheck.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 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" |
Tao Ma | 8b2c0db | 2009-08-18 11:43:49 +0800 | [diff] [blame] | 54 | #include "refcounttree.h" |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 55 | #include "ocfs2_trace.h" |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 56 | #include "filecheck.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 57 | |
| 58 | #include "buffer_head_io.h" |
| 59 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 60 | struct ocfs2_find_inode_args |
| 61 | { |
| 62 | u64 fi_blkno; |
| 63 | unsigned long fi_ino; |
| 64 | unsigned int fi_flags; |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 65 | unsigned int fi_sysfile_type; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 68 | static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES]; |
| 69 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 70 | static int ocfs2_read_locked_inode(struct inode *inode, |
| 71 | struct ocfs2_find_inode_args *args); |
| 72 | static int ocfs2_init_locked_inode(struct inode *inode, void *opaque); |
| 73 | static int ocfs2_find_actor(struct inode *inode, void *opaque); |
| 74 | static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, |
| 75 | struct inode *inode, |
| 76 | struct buffer_head *fe_bh); |
| 77 | |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 78 | static int ocfs2_filecheck_read_inode_block_full(struct inode *inode, |
| 79 | struct buffer_head **bh, |
| 80 | int flags, int type); |
| 81 | static int ocfs2_filecheck_validate_inode_block(struct super_block *sb, |
| 82 | struct buffer_head *bh); |
| 83 | static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, |
| 84 | struct buffer_head *bh); |
| 85 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 86 | void ocfs2_set_inode_flags(struct inode *inode) |
| 87 | { |
| 88 | unsigned int flags = OCFS2_I(inode)->ip_attr; |
| 89 | |
| 90 | inode->i_flags &= ~(S_IMMUTABLE | |
| 91 | S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC); |
| 92 | |
| 93 | if (flags & OCFS2_IMMUTABLE_FL) |
| 94 | inode->i_flags |= S_IMMUTABLE; |
| 95 | |
| 96 | if (flags & OCFS2_SYNC_FL) |
| 97 | inode->i_flags |= S_SYNC; |
| 98 | if (flags & OCFS2_APPEND_FL) |
| 99 | inode->i_flags |= S_APPEND; |
| 100 | if (flags & OCFS2_NOATIME_FL) |
| 101 | inode->i_flags |= S_NOATIME; |
| 102 | if (flags & OCFS2_DIRSYNC_FL) |
| 103 | inode->i_flags |= S_DIRSYNC; |
| 104 | } |
| 105 | |
Jan Kara | 6e4b0d5 | 2007-04-27 11:08:01 -0700 | [diff] [blame] | 106 | /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */ |
| 107 | void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi) |
| 108 | { |
| 109 | unsigned int flags = oi->vfs_inode.i_flags; |
| 110 | |
| 111 | oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL| |
| 112 | OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL); |
| 113 | if (flags & S_SYNC) |
| 114 | oi->ip_attr |= OCFS2_SYNC_FL; |
| 115 | if (flags & S_APPEND) |
| 116 | oi->ip_attr |= OCFS2_APPEND_FL; |
| 117 | if (flags & S_IMMUTABLE) |
| 118 | oi->ip_attr |= OCFS2_IMMUTABLE_FL; |
| 119 | if (flags & S_NOATIME) |
| 120 | oi->ip_attr |= OCFS2_NOATIME_FL; |
| 121 | if (flags & S_DIRSYNC) |
| 122 | oi->ip_attr |= OCFS2_DIRSYNC_FL; |
| 123 | } |
| 124 | |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 125 | struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno) |
| 126 | { |
| 127 | struct ocfs2_find_inode_args args; |
| 128 | |
| 129 | args.fi_blkno = blkno; |
| 130 | args.fi_flags = 0; |
| 131 | args.fi_ino = ino_from_blkno(sb, blkno); |
| 132 | args.fi_sysfile_type = 0; |
| 133 | |
| 134 | return ilookup5(sb, blkno, ocfs2_find_actor, &args); |
| 135 | } |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 136 | struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags, |
| 137 | int sysfile_type) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 138 | { |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 139 | int rc = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 140 | struct inode *inode = NULL; |
| 141 | struct super_block *sb = osb->sb; |
| 142 | struct ocfs2_find_inode_args args; |
Darrick J. Wong | 2931cdc | 2014-04-03 14:46:48 -0700 | [diff] [blame] | 143 | journal_t *journal = OCFS2_SB(sb)->journal->j_journal; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 144 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 145 | trace_ocfs2_iget_begin((unsigned long long)blkno, flags, |
| 146 | sysfile_type); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 147 | |
| 148 | /* Ok. By now we've either got the offsets passed to us by the |
| 149 | * caller, or we just pulled them off the bh. Lets do some |
| 150 | * sanity checks to make sure they're OK. */ |
| 151 | if (blkno == 0) { |
| 152 | inode = ERR_PTR(-EINVAL); |
| 153 | mlog_errno(PTR_ERR(inode)); |
| 154 | goto bail; |
| 155 | } |
| 156 | |
| 157 | args.fi_blkno = blkno; |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 158 | args.fi_flags = flags; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 159 | args.fi_ino = ino_from_blkno(sb, blkno); |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 160 | args.fi_sysfile_type = sysfile_type; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 161 | |
| 162 | inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor, |
| 163 | ocfs2_init_locked_inode, &args); |
| 164 | /* inode was *not* in the inode cache. 2.6.x requires |
| 165 | * us to do our own read_inode call and unlock it |
| 166 | * afterwards. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 167 | if (inode == NULL) { |
| 168 | inode = ERR_PTR(-ENOMEM); |
| 169 | mlog_errno(PTR_ERR(inode)); |
| 170 | goto bail; |
| 171 | } |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 172 | trace_ocfs2_iget5_locked(inode->i_state); |
Tao Ma | 6218b90 | 2011-02-21 11:18:30 +0800 | [diff] [blame] | 173 | if (inode->i_state & I_NEW) { |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 174 | rc = ocfs2_read_locked_inode(inode, &args); |
Tao Ma | 6218b90 | 2011-02-21 11:18:30 +0800 | [diff] [blame] | 175 | unlock_new_inode(inode); |
| 176 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 177 | if (is_bad_inode(inode)) { |
| 178 | iput(inode); |
Eric Ren | 1f3a437 | 2016-05-26 15:16:16 -0700 | [diff] [blame] | 179 | inode = ERR_PTR(rc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 180 | goto bail; |
| 181 | } |
| 182 | |
Darrick J. Wong | 2931cdc | 2014-04-03 14:46:48 -0700 | [diff] [blame] | 183 | /* |
| 184 | * Set transaction id's of transactions that have to be committed |
| 185 | * to finish f[data]sync. We set them to currently running transaction |
| 186 | * as we cannot be sure that the inode or some of its metadata isn't |
| 187 | * part of the transaction - the inode could have been reclaimed and |
| 188 | * now it is reread from disk. |
| 189 | */ |
| 190 | if (journal) { |
| 191 | transaction_t *transaction; |
| 192 | tid_t tid; |
| 193 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 194 | |
| 195 | read_lock(&journal->j_state_lock); |
| 196 | if (journal->j_running_transaction) |
| 197 | transaction = journal->j_running_transaction; |
| 198 | else |
| 199 | transaction = journal->j_committing_transaction; |
| 200 | if (transaction) |
| 201 | tid = transaction->t_tid; |
| 202 | else |
| 203 | tid = journal->j_commit_sequence; |
| 204 | read_unlock(&journal->j_state_lock); |
| 205 | oi->i_sync_tid = tid; |
| 206 | oi->i_datasync_tid = tid; |
| 207 | } |
| 208 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 209 | bail: |
| 210 | if (!IS_ERR(inode)) { |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 211 | trace_ocfs2_iget_end(inode, |
| 212 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | 6a1bd4a | 2007-01-03 17:06:59 -0800 | [diff] [blame] | 213 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 214 | |
| 215 | return inode; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /* |
| 220 | * here's how inodes get read from disk: |
| 221 | * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR |
| 222 | * found? : return the in-memory inode |
| 223 | * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE |
| 224 | */ |
| 225 | |
| 226 | static int ocfs2_find_actor(struct inode *inode, void *opaque) |
| 227 | { |
| 228 | struct ocfs2_find_inode_args *args = NULL; |
| 229 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 230 | int ret = 0; |
| 231 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 232 | args = opaque; |
| 233 | |
| 234 | mlog_bug_on_msg(!inode, "No inode in find actor!\n"); |
| 235 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 236 | trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno); |
| 237 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 238 | if (oi->ip_blkno != args->fi_blkno) |
| 239 | goto bail; |
| 240 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 241 | ret = 1; |
| 242 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * initialize the new inode, but don't do anything that would cause |
| 248 | * us to sleep. |
| 249 | * return 0 on success, 1 on failure |
| 250 | */ |
| 251 | static int ocfs2_init_locked_inode(struct inode *inode, void *opaque) |
| 252 | { |
| 253 | struct ocfs2_find_inode_args *args = opaque; |
Jan Kara | cb25797 | 2009-06-04 15:26:50 +0200 | [diff] [blame] | 254 | static struct lock_class_key ocfs2_quota_ip_alloc_sem_key, |
| 255 | ocfs2_file_ip_alloc_sem_key; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 256 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 257 | inode->i_ino = args->fi_ino; |
| 258 | OCFS2_I(inode)->ip_blkno = args->fi_blkno; |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 259 | if (args->fi_sysfile_type != 0) |
Al Viro | 9902af7 | 2016-04-15 15:08:36 -0400 | [diff] [blame] | 260 | lockdep_set_class(&inode->i_rwsem, |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 261 | &ocfs2_sysfile_lock_key[args->fi_sysfile_type]); |
Jan Kara | cb25797 | 2009-06-04 15:26:50 +0200 | [diff] [blame] | 262 | if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE || |
| 263 | args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE || |
| 264 | args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE || |
| 265 | args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE) |
| 266 | lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem, |
| 267 | &ocfs2_quota_ip_alloc_sem_key); |
| 268 | else |
| 269 | lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem, |
| 270 | &ocfs2_file_ip_alloc_sem_key); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 271 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 275 | void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe, |
| 276 | int create_ino) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 277 | { |
| 278 | struct super_block *sb; |
| 279 | struct ocfs2_super *osb; |
Mark Fasheh | 53da493 | 2008-07-21 14:29:16 -0700 | [diff] [blame] | 280 | int use_plocks = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 281 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 282 | sb = inode->i_sb; |
| 283 | osb = OCFS2_SB(sb); |
| 284 | |
Mark Fasheh | 53da493 | 2008-07-21 14:29:16 -0700 | [diff] [blame] | 285 | if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) || |
| 286 | ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks()) |
| 287 | use_plocks = 0; |
| 288 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 289 | /* |
| 290 | * These have all been checked by ocfs2_read_inode_block() or set |
| 291 | * by ocfs2_mknod_locked(), so a failure is a code bug. |
| 292 | */ |
| 293 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode |
| 294 | cannot create a superblock |
| 295 | inode today. change if |
| 296 | that is needed. */ |
| 297 | BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))); |
| 298 | BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 299 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 300 | |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 301 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
| 302 | OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); |
Mark Fasheh | 15b1e36 | 2007-09-07 13:58:15 -0700 | [diff] [blame] | 303 | 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] | 304 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 305 | inode->i_version = 1; |
| 306 | inode->i_generation = le32_to_cpu(fe->i_generation); |
| 307 | inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); |
| 308 | inode->i_mode = le16_to_cpu(fe->i_mode); |
Eric W. Biederman | 2c03417 | 2013-01-31 17:27:11 -0800 | [diff] [blame] | 309 | i_uid_write(inode, le32_to_cpu(fe->i_uid)); |
| 310 | i_gid_write(inode, le32_to_cpu(fe->i_gid)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 311 | |
| 312 | /* Fast symlinks will have i_size but no allocated clusters. */ |
Al Viro | ea022dfb | 2012-05-03 10:14:29 -0400 | [diff] [blame] | 313 | if (S_ISLNK(inode->i_mode) && !fe->i_clusters) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 314 | inode->i_blocks = 0; |
Al Viro | ea022dfb | 2012-05-03 10:14:29 -0400 | [diff] [blame] | 315 | inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops; |
| 316 | } else { |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 317 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Al Viro | ea022dfb | 2012-05-03 10:14:29 -0400 | [diff] [blame] | 318 | inode->i_mapping->a_ops = &ocfs2_aops; |
| 319 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 320 | inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime); |
| 321 | inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec); |
| 322 | inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime); |
| 323 | inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec); |
| 324 | inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime); |
| 325 | inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec); |
| 326 | |
| 327 | if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno)) |
| 328 | mlog(ML_ERROR, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 329 | "ip_blkno %llu != i_blkno %llu!\n", |
| 330 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 331 | (unsigned long long)le64_to_cpu(fe->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 332 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 333 | set_nlink(inode, ocfs2_read_links_count(fe)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 334 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 335 | trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno, |
| 336 | le32_to_cpu(fe->i_flags)); |
Jan Kara | bbbd0eb | 2008-08-21 18:22:30 +0200 | [diff] [blame] | 337 | if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) { |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 338 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE; |
Jan Kara | bbbd0eb | 2008-08-21 18:22:30 +0200 | [diff] [blame] | 339 | inode->i_flags |= S_NOQUOTA; |
| 340 | } |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 341 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 342 | if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) { |
| 343 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 344 | } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) { |
| 345 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; |
Jan Kara | 1a224ad | 2008-08-20 15:43:36 +0200 | [diff] [blame] | 346 | } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) { |
| 347 | inode->i_flags |= S_NOQUOTA; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 348 | } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 349 | /* we can't actually hit this as read_inode can't |
| 350 | * handle superblocks today ;-) */ |
| 351 | BUG(); |
| 352 | } |
| 353 | |
| 354 | switch (inode->i_mode & S_IFMT) { |
| 355 | case S_IFREG: |
Mark Fasheh | 53da493 | 2008-07-21 14:29:16 -0700 | [diff] [blame] | 356 | if (use_plocks) |
| 357 | inode->i_fop = &ocfs2_fops; |
| 358 | else |
| 359 | inode->i_fop = &ocfs2_fops_no_plocks; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 360 | inode->i_op = &ocfs2_file_iops; |
| 361 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
| 362 | break; |
| 363 | case S_IFDIR: |
| 364 | inode->i_op = &ocfs2_dir_iops; |
Mark Fasheh | 53da493 | 2008-07-21 14:29:16 -0700 | [diff] [blame] | 365 | if (use_plocks) |
| 366 | inode->i_fop = &ocfs2_dops; |
| 367 | else |
| 368 | inode->i_fop = &ocfs2_dops_no_plocks; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 369 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
Goldwyn Rodrigues | 5e98d49 | 2010-06-28 10:04:32 -0500 | [diff] [blame] | 370 | OCFS2_I(inode)->ip_dir_lock_gen = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 371 | break; |
| 372 | case S_IFLNK: |
Al Viro | ea022dfb | 2012-05-03 10:14:29 -0400 | [diff] [blame] | 373 | inode->i_op = &ocfs2_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 374 | inode_nohighmem(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 375 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
| 376 | break; |
| 377 | default: |
| 378 | inode->i_op = &ocfs2_special_file_iops; |
| 379 | init_special_inode(inode, inode->i_mode, |
| 380 | inode->i_rdev); |
| 381 | break; |
| 382 | } |
| 383 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 384 | if (create_ino) { |
| 385 | inode->i_ino = ino_from_blkno(inode->i_sb, |
| 386 | le64_to_cpu(fe->i_blkno)); |
| 387 | |
| 388 | /* |
| 389 | * If we ever want to create system files from kernel, |
| 390 | * the generation argument to |
| 391 | * ocfs2_inode_lock_res_init() will have to change. |
| 392 | */ |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 393 | BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 394 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 395 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 396 | OCFS2_LOCK_TYPE_META, 0, inode); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 397 | |
| 398 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, |
| 399 | OCFS2_LOCK_TYPE_OPEN, 0, inode); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 402 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres, |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 403 | OCFS2_LOCK_TYPE_RW, inode->i_generation, |
| 404 | inode); |
| 405 | |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 406 | ocfs2_set_inode_flags(inode); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 407 | |
Tao Ma | 1382115 | 2009-02-25 00:53:23 +0800 | [diff] [blame] | 408 | OCFS2_I(inode)->ip_last_used_slot = 0; |
| 409 | OCFS2_I(inode)->ip_last_used_group = 0; |
Mark Fasheh | e3b4a97 | 2009-12-07 13:16:07 -0800 | [diff] [blame] | 410 | |
| 411 | if (S_ISDIR(inode->i_mode)) |
| 412 | ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv, |
| 413 | OCFS2_RESV_FLAG_DIR); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int ocfs2_read_locked_inode(struct inode *inode, |
| 417 | struct ocfs2_find_inode_args *args) |
| 418 | { |
| 419 | struct super_block *sb; |
| 420 | struct ocfs2_super *osb; |
| 421 | struct ocfs2_dinode *fe; |
| 422 | struct buffer_head *bh = NULL; |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 423 | int status, can_lock, lock_level = 0; |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 424 | u32 generation = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 425 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 426 | status = -EINVAL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 427 | sb = inode->i_sb; |
| 428 | osb = OCFS2_SB(sb); |
| 429 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 430 | /* |
| 431 | * To improve performance of cold-cache inode stats, we take |
| 432 | * the cluster lock here if possible. |
| 433 | * |
| 434 | * Generally, OCFS2 never trusts the contents of an inode |
| 435 | * unless it's holding a cluster lock, so taking it here isn't |
| 436 | * a correctness issue as much as it is a performance |
| 437 | * improvement. |
| 438 | * |
| 439 | * There are three times when taking the lock is not a good idea: |
| 440 | * |
| 441 | * 1) During startup, before we have initialized the DLM. |
| 442 | * |
| 443 | * 2) If we are reading certain system files which never get |
| 444 | * cluster locks (local alloc, truncate log). |
| 445 | * |
| 446 | * 3) If the process doing the iget() is responsible for |
| 447 | * orphan dir recovery. We're holding the orphan dir lock and |
| 448 | * can get into a deadlock with another process on another |
| 449 | * node in ->delete_inode(). |
| 450 | * |
| 451 | * #1 and #2 can be simply solved by never taking the lock |
| 452 | * here for system files (which are the only type we read |
| 453 | * during mount). It's a heavier approach, but our main |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 454 | * concern is user-accessible files anyway. |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 455 | * |
| 456 | * #3 works itself out because we'll eventually take the |
| 457 | * cluster lock before trusting anything anyway. |
| 458 | */ |
| 459 | can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE) |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 460 | && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) |
Sunil Mushran | c271c5c | 2006-12-05 17:56:35 -0800 | [diff] [blame] | 461 | && !ocfs2_mount_local(osb); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 462 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 463 | trace_ocfs2_read_locked_inode( |
| 464 | (unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock); |
| 465 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 466 | /* |
| 467 | * To maintain backwards compatibility with older versions of |
| 468 | * ocfs2-tools, we still store the generation value for system |
| 469 | * files. The only ones that actually matter to userspace are |
| 470 | * the journals, but it's easier and inexpensive to just flag |
| 471 | * all system files similarly. |
| 472 | */ |
| 473 | if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE) |
| 474 | generation = osb->fs_generation; |
| 475 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 476 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 477 | OCFS2_LOCK_TYPE_META, |
| 478 | generation, inode); |
| 479 | |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 480 | ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, |
| 481 | OCFS2_LOCK_TYPE_OPEN, |
| 482 | 0, inode); |
| 483 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 484 | if (can_lock) { |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 485 | status = ocfs2_open_lock(inode); |
| 486 | if (status) { |
| 487 | make_bad_inode(inode); |
| 488 | mlog_errno(status); |
| 489 | return status; |
| 490 | } |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 491 | status = ocfs2_inode_lock(inode, NULL, lock_level); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 492 | if (status) { |
| 493 | make_bad_inode(inode); |
| 494 | mlog_errno(status); |
| 495 | return status; |
| 496 | } |
| 497 | } |
| 498 | |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 499 | if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) { |
| 500 | status = ocfs2_try_open_lock(inode, 0); |
| 501 | if (status) { |
Sunil Mushran | 2bd6321 | 2010-01-25 16:57:38 -0800 | [diff] [blame] | 502 | make_bad_inode(inode); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 503 | return status; |
| 504 | } |
| 505 | } |
| 506 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 507 | if (can_lock) { |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 508 | if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK) |
| 509 | status = ocfs2_filecheck_read_inode_block_full(inode, |
| 510 | &bh, OCFS2_BH_IGNORE_CACHE, 0); |
| 511 | else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX) |
| 512 | status = ocfs2_filecheck_read_inode_block_full(inode, |
| 513 | &bh, OCFS2_BH_IGNORE_CACHE, 1); |
| 514 | else |
| 515 | status = ocfs2_read_inode_block_full(inode, |
| 516 | &bh, OCFS2_BH_IGNORE_CACHE); |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 517 | } else { |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 518 | status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh); |
Sunil Mushran | f5ce5a0 | 2010-08-12 16:24:26 -0700 | [diff] [blame] | 519 | /* |
| 520 | * If buffer is in jbd, then its checksum may not have been |
| 521 | * computed as yet. |
| 522 | */ |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 523 | if (!status && !buffer_jbd(bh)) { |
| 524 | if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK) |
| 525 | status = ocfs2_filecheck_validate_inode_block( |
| 526 | osb->sb, bh); |
| 527 | else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX) |
| 528 | status = ocfs2_filecheck_repair_inode_block( |
| 529 | osb->sb, bh); |
| 530 | else |
| 531 | status = ocfs2_validate_inode_block( |
| 532 | osb->sb, bh); |
| 533 | } |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 534 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 535 | if (status < 0) { |
| 536 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 537 | goto bail; |
| 538 | } |
| 539 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 540 | status = -EINVAL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 541 | fe = (struct ocfs2_dinode *) bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 542 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 543 | /* |
| 544 | * This is a code bug. Right now the caller needs to |
| 545 | * understand whether it is asking for a system file inode or |
| 546 | * not so the proper lock names can be built. |
| 547 | */ |
| 548 | mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) != |
| 549 | !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE), |
| 550 | "Inode %llu: system file state is ambigous\n", |
| 551 | (unsigned long long)args->fi_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 552 | |
| 553 | if (S_ISCHR(le16_to_cpu(fe->i_mode)) || |
| 554 | S_ISBLK(le16_to_cpu(fe->i_mode))) |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 555 | 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] | 556 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 557 | ocfs2_populate_inode(inode, fe, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 558 | |
| 559 | BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno)); |
| 560 | |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 561 | if (buffer_dirty(bh) && !buffer_jbd(bh)) { |
| 562 | if (can_lock) { |
| 563 | ocfs2_inode_unlock(inode, lock_level); |
| 564 | lock_level = 1; |
| 565 | ocfs2_inode_lock(inode, NULL, lock_level); |
| 566 | } |
| 567 | status = ocfs2_write_block(osb, bh, INODE_CACHE(inode)); |
| 568 | if (status < 0) { |
| 569 | mlog_errno(status); |
| 570 | goto bail; |
| 571 | } |
| 572 | } |
| 573 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 574 | status = 0; |
| 575 | |
| 576 | bail: |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 577 | if (can_lock) |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 578 | ocfs2_inode_unlock(inode, lock_level); |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 579 | |
| 580 | if (status < 0) |
| 581 | make_bad_inode(inode); |
| 582 | |
Jan Kara | 4a635a1 | 2014-12-10 15:41:42 -0800 | [diff] [blame] | 583 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 584 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 585 | return status; |
| 586 | } |
| 587 | |
| 588 | void ocfs2_sync_blockdev(struct super_block *sb) |
| 589 | { |
| 590 | sync_blockdev(sb->s_bdev); |
| 591 | } |
| 592 | |
| 593 | static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, |
| 594 | struct inode *inode, |
| 595 | struct buffer_head *fe_bh) |
| 596 | { |
| 597 | int status = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 598 | struct ocfs2_dinode *fe; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 599 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 600 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 601 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 602 | |
Mark Fasheh | 1afc32b | 2007-09-07 14:46:51 -0700 | [diff] [blame] | 603 | /* |
| 604 | * This check will also skip truncate of inodes with inline |
| 605 | * data and fast symlinks. |
| 606 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 607 | if (fe->i_clusters) { |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 608 | if (ocfs2_should_order_data(inode)) |
| 609 | ocfs2_begin_ordered_truncate(inode, 0); |
| 610 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 611 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 612 | if (IS_ERR(handle)) { |
| 613 | status = PTR_ERR(handle); |
Dan Carpenter | 0350cb0 | 2010-04-22 11:39:29 +0200 | [diff] [blame] | 614 | handle = NULL; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 615 | mlog_errno(status); |
| 616 | goto out; |
| 617 | } |
| 618 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 619 | status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), |
| 620 | fe_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 621 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 622 | if (status < 0) { |
| 623 | mlog_errno(status); |
| 624 | goto out; |
| 625 | } |
| 626 | |
| 627 | i_size_write(inode, 0); |
| 628 | |
| 629 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); |
| 630 | if (status < 0) { |
| 631 | mlog_errno(status); |
| 632 | goto out; |
| 633 | } |
| 634 | |
| 635 | ocfs2_commit_trans(osb, handle); |
| 636 | handle = NULL; |
| 637 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 638 | status = ocfs2_commit_truncate(osb, inode, fe_bh); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 639 | if (status < 0) { |
| 640 | mlog_errno(status); |
| 641 | goto out; |
| 642 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 643 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 644 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 645 | out: |
| 646 | if (handle) |
| 647 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 648 | return status; |
| 649 | } |
| 650 | |
| 651 | static int ocfs2_remove_inode(struct inode *inode, |
| 652 | struct buffer_head *di_bh, |
| 653 | struct inode *orphan_dir_inode, |
| 654 | struct buffer_head *orphan_dir_bh) |
| 655 | { |
| 656 | int status; |
| 657 | struct inode *inode_alloc_inode = NULL; |
| 658 | struct buffer_head *inode_alloc_bh = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 659 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 660 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 661 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; |
| 662 | |
| 663 | inode_alloc_inode = |
| 664 | ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, |
| 665 | le16_to_cpu(di->i_suballoc_slot)); |
| 666 | if (!inode_alloc_inode) { |
Joseph Qi | 62f8b1f | 2015-04-14 15:43:24 -0700 | [diff] [blame] | 667 | status = -ENOENT; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 668 | mlog_errno(status); |
| 669 | goto bail; |
| 670 | } |
| 671 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 672 | inode_lock(inode_alloc_inode); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 673 | status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 674 | if (status < 0) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 675 | inode_unlock(inode_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 676 | |
| 677 | mlog_errno(status); |
| 678 | goto bail; |
| 679 | } |
| 680 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 681 | handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS + |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 682 | ocfs2_quota_trans_credits(inode->i_sb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 683 | if (IS_ERR(handle)) { |
| 684 | status = PTR_ERR(handle); |
| 685 | mlog_errno(status); |
| 686 | goto bail_unlock; |
| 687 | } |
| 688 | |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 689 | if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { |
| 690 | status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode, |
Joseph Qi | 06ee5c7 | 2015-02-16 15:59:54 -0800 | [diff] [blame] | 691 | orphan_dir_bh, false); |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 692 | if (status < 0) { |
| 693 | mlog_errno(status); |
| 694 | goto bail_commit; |
| 695 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* set the inodes dtime */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 699 | status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 700 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 701 | if (status < 0) { |
| 702 | mlog_errno(status); |
| 703 | goto bail_commit; |
| 704 | } |
| 705 | |
| 706 | di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec); |
Marcin Slusarz | 4092d49 | 2007-12-25 15:52:59 +0100 | [diff] [blame] | 707 | di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL)); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 708 | ocfs2_journal_dirty(handle, di_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 709 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 710 | ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh); |
Christoph Hellwig | 63936dd | 2010-03-03 09:05:01 -0500 | [diff] [blame] | 711 | dquot_free_inode(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 712 | |
| 713 | status = ocfs2_free_dinode(handle, inode_alloc_inode, |
| 714 | inode_alloc_bh, di); |
| 715 | if (status < 0) |
| 716 | mlog_errno(status); |
| 717 | |
| 718 | bail_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 719 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 720 | bail_unlock: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 721 | ocfs2_inode_unlock(inode_alloc_inode, 1); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 722 | inode_unlock(inode_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 723 | brelse(inode_alloc_bh); |
| 724 | bail: |
| 725 | iput(inode_alloc_inode); |
| 726 | |
| 727 | return status; |
| 728 | } |
| 729 | |
Sunil Mushran | 2bd6321 | 2010-01-25 16:57:38 -0800 | [diff] [blame] | 730 | /* |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 731 | * Serialize with orphan dir recovery. If the process doing |
| 732 | * recovery on this orphan dir does an iget() with the dir |
| 733 | * i_mutex held, we'll deadlock here. Instead we detect this |
| 734 | * and exit early - recovery will wipe this inode for us. |
| 735 | */ |
| 736 | static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb, |
| 737 | int slot) |
| 738 | { |
| 739 | int ret = 0; |
| 740 | |
| 741 | spin_lock(&osb->osb_lock); |
| 742 | if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) { |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 743 | ret = -EDEADLK; |
| 744 | goto out; |
| 745 | } |
| 746 | /* This signals to the orphan recovery process that it should |
| 747 | * wait for us to handle the wipe. */ |
| 748 | osb->osb_orphan_wipes[slot]++; |
| 749 | out: |
| 750 | spin_unlock(&osb->osb_lock); |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 751 | trace_ocfs2_check_orphan_recovery_state(slot, ret); |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb, |
| 756 | int slot) |
| 757 | { |
| 758 | spin_lock(&osb->osb_lock); |
| 759 | osb->osb_orphan_wipes[slot]--; |
| 760 | spin_unlock(&osb->osb_lock); |
| 761 | |
| 762 | wake_up(&osb->osb_wipe_event); |
| 763 | } |
| 764 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 765 | static int ocfs2_wipe_inode(struct inode *inode, |
| 766 | struct buffer_head *di_bh) |
| 767 | { |
Joel Becker | d577632 | 2010-05-03 19:15:49 -0700 | [diff] [blame] | 768 | int status, orphaned_slot = -1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 769 | struct inode *orphan_dir_inode = NULL; |
| 770 | struct buffer_head *orphan_dir_bh = NULL; |
| 771 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 772 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 773 | |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 774 | if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { |
| 775 | orphaned_slot = le16_to_cpu(di->i_orphaned_slot); |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 776 | |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 777 | status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot); |
| 778 | if (status) |
| 779 | return status; |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 780 | |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 781 | orphan_dir_inode = ocfs2_get_system_file_inode(osb, |
| 782 | ORPHAN_DIR_SYSTEM_INODE, |
| 783 | orphaned_slot); |
| 784 | if (!orphan_dir_inode) { |
Joseph Qi | 62f8b1f | 2015-04-14 15:43:24 -0700 | [diff] [blame] | 785 | status = -ENOENT; |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 786 | mlog_errno(status); |
| 787 | goto bail; |
| 788 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 789 | |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 790 | /* Lock the orphan dir. The lock will be held for the entire |
| 791 | * delete_inode operation. We do this now to avoid races with |
| 792 | * recovery completion on other nodes. */ |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 793 | inode_lock(orphan_dir_inode); |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 794 | status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); |
| 795 | if (status < 0) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 796 | inode_unlock(orphan_dir_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 797 | |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 798 | mlog_errno(status); |
| 799 | goto bail; |
| 800 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* we do this while holding the orphan dir lock because we |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 804 | * don't want recovery being run from another node to try an |
| 805 | * inode delete underneath us -- this will result in two nodes |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 806 | * truncating the same file! */ |
| 807 | status = ocfs2_truncate_for_delete(osb, inode, di_bh); |
| 808 | if (status < 0) { |
| 809 | mlog_errno(status); |
| 810 | goto bail_unlock_dir; |
| 811 | } |
| 812 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 813 | /* Remove any dir index tree */ |
| 814 | if (S_ISDIR(inode->i_mode)) { |
| 815 | status = ocfs2_dx_dir_truncate(inode, di_bh); |
| 816 | if (status) { |
| 817 | mlog_errno(status); |
| 818 | goto bail_unlock_dir; |
| 819 | } |
| 820 | } |
| 821 | |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 822 | /*Free extended attribute resources associated with this inode.*/ |
| 823 | status = ocfs2_xattr_remove(inode, di_bh); |
| 824 | if (status < 0) { |
| 825 | mlog_errno(status); |
| 826 | goto bail_unlock_dir; |
| 827 | } |
| 828 | |
Tao Ma | 8b2c0db | 2009-08-18 11:43:49 +0800 | [diff] [blame] | 829 | status = ocfs2_remove_refcount_tree(inode, di_bh); |
| 830 | if (status < 0) { |
| 831 | mlog_errno(status); |
| 832 | goto bail_unlock_dir; |
| 833 | } |
| 834 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 835 | status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode, |
| 836 | orphan_dir_bh); |
| 837 | if (status < 0) |
| 838 | mlog_errno(status); |
| 839 | |
| 840 | bail_unlock_dir: |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 841 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR) |
| 842 | return status; |
| 843 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 844 | ocfs2_inode_unlock(orphan_dir_inode, 1); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 845 | inode_unlock(orphan_dir_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 846 | brelse(orphan_dir_bh); |
| 847 | bail: |
| 848 | iput(orphan_dir_inode); |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 849 | ocfs2_signal_wipe_completion(osb, orphaned_slot); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 850 | |
| 851 | return status; |
| 852 | } |
| 853 | |
| 854 | /* 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] | 855 | * trylock is even considered. Encapsulate those in this function. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 856 | static int ocfs2_inode_is_valid_to_delete(struct inode *inode) |
| 857 | { |
| 858 | int ret = 0; |
| 859 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 860 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 861 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 862 | trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task, |
| 863 | (unsigned long long)oi->ip_blkno, |
| 864 | oi->ip_flags); |
| 865 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 866 | /* We shouldn't be getting here for the root directory |
| 867 | * inode.. */ |
| 868 | if (inode == osb->root_inode) { |
| 869 | mlog(ML_ERROR, "Skipping delete of root inode.\n"); |
| 870 | goto bail; |
| 871 | } |
| 872 | |
Jan Kara | bd62ad7 | 2014-04-03 14:46:54 -0700 | [diff] [blame] | 873 | /* |
| 874 | * If we're coming from downconvert_thread we can't go into our own |
| 875 | * voting [hello, deadlock city!] so we cannot delete the inode. But |
| 876 | * since we dropped last inode ref when downconverting dentry lock, |
| 877 | * we cannot have the file open and thus the node doing unlink will |
| 878 | * take care of deleting the inode. |
| 879 | */ |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 880 | if (current == osb->dc_task) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 881 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 882 | |
| 883 | spin_lock(&oi->ip_lock); |
| 884 | /* OCFS2 *never* deletes system files. This should technically |
| 885 | * never get here as system file inodes should always have a |
| 886 | * positive link count. */ |
| 887 | if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 888 | mlog(ML_ERROR, "Skipping delete of system file %llu\n", |
| 889 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 890 | goto bail_unlock; |
| 891 | } |
| 892 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 893 | ret = 1; |
| 894 | bail_unlock: |
| 895 | spin_unlock(&oi->ip_lock); |
| 896 | bail: |
| 897 | return ret; |
| 898 | } |
| 899 | |
| 900 | /* Query the cluster to determine whether we should wipe an inode from |
| 901 | * disk or not. |
| 902 | * |
| 903 | * Requires the inode to have the cluster lock. */ |
| 904 | static int ocfs2_query_inode_wipe(struct inode *inode, |
| 905 | struct buffer_head *di_bh, |
| 906 | int *wipe) |
| 907 | { |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 908 | int status = 0, reason = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 909 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 910 | struct ocfs2_dinode *di; |
| 911 | |
| 912 | *wipe = 0; |
| 913 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 914 | trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno, |
| 915 | inode->i_nlink); |
| 916 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 917 | /* While we were waiting for the cluster lock in |
| 918 | * ocfs2_delete_inode, another node might have asked to delete |
| 919 | * the inode. Recheck our flags to catch this. */ |
| 920 | if (!ocfs2_inode_is_valid_to_delete(inode)) { |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 921 | reason = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 922 | goto bail; |
| 923 | } |
| 924 | |
| 925 | /* Now that we have an up to date inode, we can double check |
| 926 | * the link count. */ |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 927 | if (inode->i_nlink) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 928 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 929 | |
| 930 | /* Do some basic inode verification... */ |
| 931 | di = (struct ocfs2_dinode *) di_bh->b_data; |
Li Dongyang | d4cd187 | 2010-04-22 16:11:19 +0800 | [diff] [blame] | 932 | if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) && |
| 933 | !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { |
Tristan Ye | b54c2ca | 2010-03-19 09:21:10 +0800 | [diff] [blame] | 934 | /* |
| 935 | * Inodes in the orphan dir must have ORPHANED_FL. The only |
| 936 | * inodes that come back out of the orphan dir are reflink |
| 937 | * targets. A reflink target may be moved out of the orphan |
| 938 | * dir between the time we scan the directory and the time we |
| 939 | * process it. This would lead to HAS_REFCOUNT_FL being set but |
| 940 | * ORPHANED_FL not. |
| 941 | */ |
| 942 | if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) { |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 943 | reason = 2; |
Tristan Ye | b54c2ca | 2010-03-19 09:21:10 +0800 | [diff] [blame] | 944 | goto bail; |
| 945 | } |
| 946 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 947 | /* for lack of a better error? */ |
| 948 | status = -EEXIST; |
| 949 | mlog(ML_ERROR, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 950 | "Inode %llu (on-disk %llu) not orphaned! " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 951 | "Disk flags 0x%x, inode flags 0x%x\n", |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 952 | (unsigned long long)oi->ip_blkno, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 953 | (unsigned long long)le64_to_cpu(di->i_blkno), |
| 954 | le32_to_cpu(di->i_flags), oi->ip_flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 955 | goto bail; |
| 956 | } |
| 957 | |
| 958 | /* has someone already deleted us?! baaad... */ |
| 959 | if (di->i_dtime) { |
| 960 | status = -EEXIST; |
| 961 | mlog_errno(status); |
| 962 | goto bail; |
| 963 | } |
| 964 | |
Mark Fasheh | 6f16bf6 | 2007-03-20 17:17:54 -0700 | [diff] [blame] | 965 | /* |
| 966 | * This is how ocfs2 determines whether an inode is still live |
| 967 | * within the cluster. Every node takes a shared read lock on |
| 968 | * the inode open lock in ocfs2_read_locked_inode(). When we |
| 969 | * get to ->delete_inode(), each node tries to convert it's |
| 970 | * lock to an exclusive. Trylocks are serialized by the inode |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 971 | * meta data lock. If the upconvert succeeds, we know the inode |
Mark Fasheh | 6f16bf6 | 2007-03-20 17:17:54 -0700 | [diff] [blame] | 972 | * is no longer live and can be deleted. |
| 973 | * |
| 974 | * Though we call this with the meta data lock held, the |
| 975 | * trylock keeps us from ABBA deadlock. |
| 976 | */ |
| 977 | status = ocfs2_try_open_lock(inode, 1); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 978 | if (status == -EAGAIN) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 979 | status = 0; |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 980 | reason = 3; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 981 | goto bail; |
| 982 | } |
| 983 | if (status < 0) { |
| 984 | mlog_errno(status); |
| 985 | goto bail; |
| 986 | } |
| 987 | |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 988 | *wipe = 1; |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 989 | trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 990 | |
| 991 | bail: |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 992 | trace_ocfs2_query_inode_wipe_end(status, reason); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 993 | return status; |
| 994 | } |
| 995 | |
| 996 | /* Support function for ocfs2_delete_inode. Will help us keep the |
| 997 | * inode data in a consistent state for clear_inode. Always truncates |
| 998 | * pages, optionally sync's them first. */ |
| 999 | static void ocfs2_cleanup_delete_inode(struct inode *inode, |
| 1000 | int sync_data) |
| 1001 | { |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 1002 | trace_ocfs2_cleanup_delete_inode( |
| 1003 | (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1004 | if (sync_data) |
Jan Kara | 249ec93 | 2011-11-08 00:20:39 +0100 | [diff] [blame] | 1005 | filemap_write_and_wait(inode->i_mapping); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 1006 | truncate_inode_pages_final(&inode->i_data); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Al Viro | 066d92d | 2010-06-08 21:28:10 -0400 | [diff] [blame] | 1009 | static void ocfs2_delete_inode(struct inode *inode) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1010 | { |
| 1011 | int wipe, status; |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 1012 | sigset_t oldset; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1013 | struct buffer_head *di_bh = NULL; |
Joseph Qi | ad69482 | 2015-09-04 15:44:40 -0700 | [diff] [blame] | 1014 | struct ocfs2_dinode *di = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1015 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 1016 | trace_ocfs2_delete_inode(inode->i_ino, |
| 1017 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 1018 | is_bad_inode(inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1019 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1020 | /* When we fail in read_inode() we mark inode as bad. The second test |
| 1021 | * catches the case when inode allocation fails before allocating |
| 1022 | * a block for inode. */ |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 1023 | if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1024 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1025 | |
| 1026 | if (!ocfs2_inode_is_valid_to_delete(inode)) { |
| 1027 | /* It's probably not necessary to truncate_inode_pages |
| 1028 | * here but we do it for safety anyway (it will most |
| 1029 | * likely be a no-op anyway) */ |
| 1030 | ocfs2_cleanup_delete_inode(inode, 0); |
| 1031 | goto bail; |
| 1032 | } |
| 1033 | |
Jan Kara | bd62ad7 | 2014-04-03 14:46:54 -0700 | [diff] [blame] | 1034 | dquot_initialize(inode); |
| 1035 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1036 | /* We want to block signals in delete_inode as the lock and |
| 1037 | * messaging paths may return us -ERESTARTSYS. Which would |
| 1038 | * cause us to exit early, resulting in inodes being orphaned |
| 1039 | * forever. */ |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 1040 | ocfs2_block_signals(&oldset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1041 | |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 1042 | /* |
| 1043 | * Synchronize us against ocfs2_get_dentry. We take this in |
| 1044 | * shared mode so that all nodes can still concurrently |
| 1045 | * process deletes. |
| 1046 | */ |
| 1047 | status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0); |
| 1048 | if (status < 0) { |
| 1049 | mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status); |
| 1050 | ocfs2_cleanup_delete_inode(inode, 0); |
| 1051 | goto bail_unblock; |
| 1052 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1053 | /* Lock down the inode. This gives us an up to date view of |
| 1054 | * it's metadata (for verification), and allows us to |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 1055 | * serialize delete_inode on multiple nodes. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1056 | * |
| 1057 | * Even though we might be doing a truncate, we don't take the |
| 1058 | * allocation lock here as it won't be needed - nobody will |
| 1059 | * have the file open. |
| 1060 | */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1061 | status = ocfs2_inode_lock(inode, &di_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1062 | if (status < 0) { |
| 1063 | if (status != -ENOENT) |
| 1064 | mlog_errno(status); |
| 1065 | ocfs2_cleanup_delete_inode(inode, 0); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 1066 | goto bail_unlock_nfs_sync; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Joseph Qi | ad69482 | 2015-09-04 15:44:40 -0700 | [diff] [blame] | 1069 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1070 | /* Skip inode deletion and wait for dio orphan entry recovered |
| 1071 | * first */ |
| 1072 | if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) { |
| 1073 | ocfs2_cleanup_delete_inode(inode, 0); |
| 1074 | goto bail_unlock_inode; |
| 1075 | } |
| 1076 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1077 | /* Query the cluster. This will be the final decision made |
| 1078 | * before we go ahead and wipe the inode. */ |
| 1079 | status = ocfs2_query_inode_wipe(inode, di_bh, &wipe); |
| 1080 | if (!wipe || status < 0) { |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 1081 | /* Error and remote inode busy both mean we won't be |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1082 | * removing the inode, so they take almost the same |
| 1083 | * path. */ |
| 1084 | if (status < 0) |
| 1085 | mlog_errno(status); |
| 1086 | |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 1087 | /* Someone in the cluster has disallowed a wipe of |
| 1088 | * this inode, or it was never completely |
| 1089 | * orphaned. Write out the pages and exit now. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1090 | ocfs2_cleanup_delete_inode(inode, 1); |
| 1091 | goto bail_unlock_inode; |
| 1092 | } |
| 1093 | |
| 1094 | ocfs2_cleanup_delete_inode(inode, 0); |
| 1095 | |
| 1096 | status = ocfs2_wipe_inode(inode, di_bh); |
| 1097 | if (status < 0) { |
Mark Fasheh | b4df6ed | 2006-02-22 17:35:08 -0800 | [diff] [blame] | 1098 | if (status != -EDEADLK) |
| 1099 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1100 | goto bail_unlock_inode; |
| 1101 | } |
| 1102 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | * Mark the inode as successfully deleted. |
| 1105 | * |
| 1106 | * This is important for ocfs2_clear_inode() as it will check |
| 1107 | * this flag and skip any checkpointing work |
| 1108 | * |
| 1109 | * ocfs2_stuff_meta_lvb() also uses this flag to invalidate |
| 1110 | * the LVB for other nodes. |
| 1111 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1112 | OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED; |
| 1113 | |
| 1114 | bail_unlock_inode: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1115 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1116 | brelse(di_bh); |
wengang wang | 6ca497a | 2009-03-06 21:29:10 +0800 | [diff] [blame] | 1117 | |
| 1118 | bail_unlock_nfs_sync: |
| 1119 | ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0); |
| 1120 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1121 | bail_unblock: |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 1122 | ocfs2_unblock_signals(&oldset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1123 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1124 | return; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
Al Viro | 066d92d | 2010-06-08 21:28:10 -0400 | [diff] [blame] | 1127 | static void ocfs2_clear_inode(struct inode *inode) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1128 | { |
| 1129 | int status; |
| 1130 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
Jan Kara | 84d86f8 | 2014-04-03 14:46:57 -0700 | [diff] [blame] | 1131 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1132 | |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 1133 | clear_inode(inode); |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 1134 | trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno, |
| 1135 | inode->i_nlink); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1136 | |
| 1137 | mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL, |
| 1138 | "Inode=%lu\n", inode->i_ino); |
| 1139 | |
Christoph Hellwig | 9f75475 | 2010-03-03 09:05:05 -0500 | [diff] [blame] | 1140 | dquot_drop(inode); |
Christoph Hellwig | 257ba15 | 2010-03-03 09:05:04 -0500 | [diff] [blame] | 1141 | |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 1142 | /* To preven remote deletes we hold open lock before, now it |
| 1143 | * is time to unlock PR and EX open locks. */ |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 1144 | ocfs2_open_unlock(inode); |
| 1145 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1146 | /* 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] | 1147 | * the downconvert thread while waiting to destroy the locks. */ |
Jan Kara | 84d86f8 | 2014-04-03 14:46:57 -0700 | [diff] [blame] | 1148 | ocfs2_mark_lockres_freeing(osb, &oi->ip_rw_lockres); |
| 1149 | ocfs2_mark_lockres_freeing(osb, &oi->ip_inode_lockres); |
| 1150 | ocfs2_mark_lockres_freeing(osb, &oi->ip_open_lockres); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1151 | |
Mark Fasheh | 4fe370a | 2009-12-07 13:15:40 -0800 | [diff] [blame] | 1152 | ocfs2_resv_discard(&OCFS2_SB(inode->i_sb)->osb_la_resmap, |
| 1153 | &oi->ip_la_data_resv); |
| 1154 | ocfs2_resv_init_once(&oi->ip_la_data_resv); |
| 1155 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1156 | /* We very well may get a clear_inode before all an inodes |
| 1157 | * metadata has hit disk. Of course, we can't drop any cluster |
| 1158 | * locks until the journal has finished with it. The only |
| 1159 | * exception here are successfully wiped inodes - their |
| 1160 | * metadata can now be considered to be part of the system |
| 1161 | * inodes from which it came. */ |
| 1162 | if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED)) |
| 1163 | ocfs2_checkpoint_inode(inode); |
| 1164 | |
| 1165 | mlog_bug_on_msg(!list_empty(&oi->ip_io_markers), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1166 | "Clear inode of %llu, inode has io markers\n", |
| 1167 | (unsigned long long)oi->ip_blkno); |
Ryan Ding | 4506cfb | 2016-03-25 14:21:06 -0700 | [diff] [blame] | 1168 | mlog_bug_on_msg(!list_empty(&oi->ip_unwritten_list), |
| 1169 | "Clear inode of %llu, inode has unwritten extents\n", |
| 1170 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1171 | |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 1172 | ocfs2_extent_map_trunc(inode, 0); |
| 1173 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1174 | status = ocfs2_drop_inode_locks(inode); |
| 1175 | if (status < 0) |
| 1176 | mlog_errno(status); |
| 1177 | |
| 1178 | ocfs2_lock_res_free(&oi->ip_rw_lockres); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1179 | ocfs2_lock_res_free(&oi->ip_inode_lockres); |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 1180 | ocfs2_lock_res_free(&oi->ip_open_lockres); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1181 | |
Joel Becker | 66fb345 | 2009-02-12 15:24:40 -0800 | [diff] [blame] | 1182 | ocfs2_metadata_cache_exit(INODE_CACHE(inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1183 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1184 | mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1185 | "Clear inode of %llu, inode has %u cache items\n", |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1186 | (unsigned long long)oi->ip_blkno, |
| 1187 | INODE_CACHE(inode)->ci_num_cached); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1188 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1189 | mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1190 | "Clear inode of %llu, inode has a bad flag\n", |
| 1191 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1192 | |
| 1193 | mlog_bug_on_msg(spin_is_locked(&oi->ip_lock), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1194 | "Clear inode of %llu, inode is locked\n", |
| 1195 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1196 | |
Mark Fasheh | 251b6ec | 2006-01-10 15:41:43 -0800 | [diff] [blame] | 1197 | mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1198 | "Clear inode of %llu, io_mutex is locked\n", |
| 1199 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | 251b6ec | 2006-01-10 15:41:43 -0800 | [diff] [blame] | 1200 | mutex_unlock(&oi->ip_io_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1201 | |
| 1202 | /* |
| 1203 | * down_trylock() returns 0, down_write_trylock() returns 1 |
| 1204 | * kernel 1, world 0 |
| 1205 | */ |
| 1206 | mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem), |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1207 | "Clear inode of %llu, alloc_sem is locked\n", |
| 1208 | (unsigned long long)oi->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1209 | up_write(&oi->ip_alloc_sem); |
| 1210 | |
| 1211 | mlog_bug_on_msg(oi->ip_open_count, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1212 | "Clear inode of %llu has open count %d\n", |
| 1213 | (unsigned long long)oi->ip_blkno, oi->ip_open_count); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1214 | |
| 1215 | /* Clear all other flags. */ |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 1216 | oi->ip_flags = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1217 | oi->ip_dir_start_lookup = 0; |
| 1218 | oi->ip_blkno = 0ULL; |
Sunil Mushran | ae0dff6 | 2008-10-22 13:24:29 -0700 | [diff] [blame] | 1219 | |
| 1220 | /* |
| 1221 | * ip_jinode is used to track txns against this inode. We ensure that |
| 1222 | * the journal is flushed before journal shutdown. Thus it is safe to |
| 1223 | * have inodes get cleaned up after journal shutdown. |
| 1224 | */ |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 1225 | jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal, |
| 1226 | &oi->ip_jinode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
Al Viro | 066d92d | 2010-06-08 21:28:10 -0400 | [diff] [blame] | 1229 | void ocfs2_evict_inode(struct inode *inode) |
| 1230 | { |
| 1231 | if (!inode->i_nlink || |
| 1232 | (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) { |
| 1233 | ocfs2_delete_inode(inode); |
| 1234 | } else { |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 1235 | truncate_inode_pages_final(&inode->i_data); |
Al Viro | 066d92d | 2010-06-08 21:28:10 -0400 | [diff] [blame] | 1236 | } |
| 1237 | ocfs2_clear_inode(inode); |
| 1238 | } |
| 1239 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1240 | /* Called under inode_lock, with no more references on the |
| 1241 | * struct inode, so it's safe here to check the flags field |
| 1242 | * and to manipulate i_nlink without any other locks. */ |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 1243 | int ocfs2_drop_inode(struct inode *inode) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1244 | { |
| 1245 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1246 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 1247 | trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno, |
| 1248 | inode->i_nlink, oi->ip_flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1249 | |
Xue jiufei | 513e2da | 2015-09-04 15:44:11 -0700 | [diff] [blame] | 1250 | assert_spin_locked(&inode->i_lock); |
| 1251 | inode->i_state |= I_WILL_FREE; |
| 1252 | spin_unlock(&inode->i_lock); |
| 1253 | write_inode_now(inode, 1); |
| 1254 | spin_lock(&inode->i_lock); |
| 1255 | WARN_ON(inode->i_state & I_NEW); |
| 1256 | inode->i_state &= ~I_WILL_FREE; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1257 | |
Xue jiufei | 513e2da | 2015-09-04 15:44:11 -0700 | [diff] [blame] | 1258 | return 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1262 | * This is called from our getattr. |
| 1263 | */ |
| 1264 | int ocfs2_inode_revalidate(struct dentry *dentry) |
| 1265 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1266 | struct inode *inode = d_inode(dentry); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1267 | int status = 0; |
| 1268 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 1269 | trace_ocfs2_inode_revalidate(inode, |
| 1270 | inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL, |
| 1271 | inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1272 | |
| 1273 | if (!inode) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1274 | status = -ENOENT; |
| 1275 | goto bail; |
| 1276 | } |
| 1277 | |
| 1278 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1279 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { |
| 1280 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1281 | status = -ENOENT; |
| 1282 | goto bail; |
| 1283 | } |
| 1284 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1285 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1286 | /* Let ocfs2_inode_lock do the work of updating our struct |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1287 | * inode for us. */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1288 | status = ocfs2_inode_lock(inode, NULL, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1289 | if (status < 0) { |
| 1290 | if (status != -ENOENT) |
| 1291 | mlog_errno(status); |
| 1292 | goto bail; |
| 1293 | } |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1294 | ocfs2_inode_unlock(inode, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1295 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1296 | return status; |
| 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * Updates a disk inode from a |
| 1301 | * struct inode. |
| 1302 | * Only takes ip_lock. |
| 1303 | */ |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1304 | int ocfs2_mark_inode_dirty(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1305 | struct inode *inode, |
| 1306 | struct buffer_head *bh) |
| 1307 | { |
| 1308 | int status; |
| 1309 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; |
| 1310 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 1311 | trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1312 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1313 | status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1314 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1315 | if (status < 0) { |
| 1316 | mlog_errno(status); |
| 1317 | goto leave; |
| 1318 | } |
| 1319 | |
| 1320 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1321 | fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters); |
Jan Kara | 6e4b0d5 | 2007-04-27 11:08:01 -0700 | [diff] [blame] | 1322 | ocfs2_get_inode_flags(OCFS2_I(inode)); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1323 | fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr); |
Mark Fasheh | 15b1e36 | 2007-09-07 13:58:15 -0700 | [diff] [blame] | 1324 | 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] | 1325 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1326 | |
| 1327 | fe->i_size = cpu_to_le64(i_size_read(inode)); |
Mark Fasheh | 198a1ca | 2008-11-20 17:54:57 -0800 | [diff] [blame] | 1328 | ocfs2_set_links_count(fe, inode->i_nlink); |
Eric W. Biederman | 2c03417 | 2013-01-31 17:27:11 -0800 | [diff] [blame] | 1329 | fe->i_uid = cpu_to_le32(i_uid_read(inode)); |
| 1330 | fe->i_gid = cpu_to_le32(i_gid_read(inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1331 | fe->i_mode = cpu_to_le16(inode->i_mode); |
| 1332 | fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec); |
| 1333 | fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec); |
| 1334 | fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 1335 | fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 1336 | fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); |
| 1337 | fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); |
| 1338 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1339 | ocfs2_journal_dirty(handle, bh); |
Darrick J. Wong | 2931cdc | 2014-04-03 14:46:48 -0700 | [diff] [blame] | 1340 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1341 | leave: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1342 | return status; |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * |
| 1347 | * Updates a struct inode from a disk inode. |
| 1348 | * does no i/o, only takes ip_lock. |
| 1349 | */ |
| 1350 | void ocfs2_refresh_inode(struct inode *inode, |
| 1351 | struct ocfs2_dinode *fe) |
| 1352 | { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1353 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1354 | |
| 1355 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1356 | OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); |
Mark Fasheh | 15b1e36 | 2007-09-07 13:58:15 -0700 | [diff] [blame] | 1357 | 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] | 1358 | ocfs2_set_inode_flags(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1359 | i_size_write(inode, le64_to_cpu(fe->i_size)); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1360 | set_nlink(inode, ocfs2_read_links_count(fe)); |
Eric W. Biederman | 2c03417 | 2013-01-31 17:27:11 -0800 | [diff] [blame] | 1361 | i_uid_write(inode, le32_to_cpu(fe->i_uid)); |
| 1362 | i_gid_write(inode, le32_to_cpu(fe->i_gid)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1363 | inode->i_mode = le16_to_cpu(fe->i_mode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1364 | if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0) |
| 1365 | inode->i_blocks = 0; |
| 1366 | else |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1367 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1368 | inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime); |
| 1369 | inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec); |
| 1370 | inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime); |
| 1371 | inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec); |
| 1372 | inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime); |
| 1373 | inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec); |
| 1374 | |
| 1375 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1376 | } |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1377 | |
| 1378 | int ocfs2_validate_inode_block(struct super_block *sb, |
| 1379 | struct buffer_head *bh) |
| 1380 | { |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 1381 | int rc; |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1382 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; |
| 1383 | |
Tao Ma | 64f3b26 | 2011-02-22 22:24:57 +0800 | [diff] [blame] | 1384 | trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr); |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 1385 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1386 | BUG_ON(!buffer_uptodate(bh)); |
| 1387 | |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 1388 | /* |
| 1389 | * If the ecc fails, we return the error but otherwise |
| 1390 | * leave the filesystem running. We know any error is |
| 1391 | * local to this block. |
| 1392 | */ |
| 1393 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check); |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1394 | if (rc) { |
| 1395 | mlog(ML_ERROR, "Checksum failed for dinode %llu\n", |
| 1396 | (unsigned long long)bh->b_blocknr); |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 1397 | goto bail; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1398 | } |
Joel Becker | d6b32bb | 2008-10-17 14:55:01 -0700 | [diff] [blame] | 1399 | |
| 1400 | /* |
| 1401 | * Errors after here are fatal. |
| 1402 | */ |
| 1403 | |
| 1404 | rc = -EINVAL; |
| 1405 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1406 | if (!OCFS2_IS_VALID_DINODE(di)) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 1407 | rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n", |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1408 | (unsigned long long)bh->b_blocknr, 7, |
| 1409 | di->i_signature); |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1410 | goto bail; |
| 1411 | } |
| 1412 | |
| 1413 | if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 1414 | rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n", |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1415 | (unsigned long long)bh->b_blocknr, |
| 1416 | (unsigned long long)le64_to_cpu(di->i_blkno)); |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1417 | goto bail; |
| 1418 | } |
| 1419 | |
| 1420 | if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 1421 | rc = ocfs2_error(sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1422 | "Invalid dinode #%llu: OCFS2_VALID_FL not set\n", |
| 1423 | (unsigned long long)bh->b_blocknr); |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1424 | goto bail; |
| 1425 | } |
| 1426 | |
| 1427 | if (le32_to_cpu(di->i_fs_generation) != |
| 1428 | OCFS2_SB(sb)->fs_generation) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame] | 1429 | rc = ocfs2_error(sb, |
Joe Perches | 7ecef14 | 2015-09-04 15:44:51 -0700 | [diff] [blame] | 1430 | "Invalid dinode #%llu: fs_generation is %u\n", |
| 1431 | (unsigned long long)bh->b_blocknr, |
| 1432 | le32_to_cpu(di->i_fs_generation)); |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1433 | goto bail; |
| 1434 | } |
| 1435 | |
| 1436 | rc = 0; |
| 1437 | |
| 1438 | bail: |
| 1439 | return rc; |
| 1440 | } |
| 1441 | |
Gang He | d56a8f3 | 2016-03-22 14:24:30 -0700 | [diff] [blame] | 1442 | static int ocfs2_filecheck_validate_inode_block(struct super_block *sb, |
| 1443 | struct buffer_head *bh) |
| 1444 | { |
| 1445 | int rc = 0; |
| 1446 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; |
| 1447 | |
| 1448 | trace_ocfs2_filecheck_validate_inode_block( |
| 1449 | (unsigned long long)bh->b_blocknr); |
| 1450 | |
| 1451 | BUG_ON(!buffer_uptodate(bh)); |
| 1452 | |
| 1453 | /* |
| 1454 | * Call ocfs2_validate_meta_ecc() first since it has ecc repair |
| 1455 | * function, but we should not return error immediately when ecc |
| 1456 | * validation fails, because the reason is quite likely the invalid |
| 1457 | * inode number inputed. |
| 1458 | */ |
| 1459 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check); |
| 1460 | if (rc) { |
| 1461 | mlog(ML_ERROR, |
| 1462 | "Filecheck: checksum failed for dinode %llu\n", |
| 1463 | (unsigned long long)bh->b_blocknr); |
| 1464 | rc = -OCFS2_FILECHECK_ERR_BLOCKECC; |
| 1465 | } |
| 1466 | |
| 1467 | if (!OCFS2_IS_VALID_DINODE(di)) { |
| 1468 | mlog(ML_ERROR, |
| 1469 | "Filecheck: invalid dinode #%llu: signature = %.*s\n", |
| 1470 | (unsigned long long)bh->b_blocknr, 7, di->i_signature); |
| 1471 | rc = -OCFS2_FILECHECK_ERR_INVALIDINO; |
| 1472 | goto bail; |
| 1473 | } else if (rc) |
| 1474 | goto bail; |
| 1475 | |
| 1476 | if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { |
| 1477 | mlog(ML_ERROR, |
| 1478 | "Filecheck: invalid dinode #%llu: i_blkno is %llu\n", |
| 1479 | (unsigned long long)bh->b_blocknr, |
| 1480 | (unsigned long long)le64_to_cpu(di->i_blkno)); |
| 1481 | rc = -OCFS2_FILECHECK_ERR_BLOCKNO; |
| 1482 | goto bail; |
| 1483 | } |
| 1484 | |
| 1485 | if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { |
| 1486 | mlog(ML_ERROR, |
| 1487 | "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL " |
| 1488 | "not set\n", |
| 1489 | (unsigned long long)bh->b_blocknr); |
| 1490 | rc = -OCFS2_FILECHECK_ERR_VALIDFLAG; |
| 1491 | goto bail; |
| 1492 | } |
| 1493 | |
| 1494 | if (le32_to_cpu(di->i_fs_generation) != |
| 1495 | OCFS2_SB(sb)->fs_generation) { |
| 1496 | mlog(ML_ERROR, |
| 1497 | "Filecheck: invalid dinode #%llu: fs_generation is %u\n", |
| 1498 | (unsigned long long)bh->b_blocknr, |
| 1499 | le32_to_cpu(di->i_fs_generation)); |
| 1500 | rc = -OCFS2_FILECHECK_ERR_GENERATION; |
| 1501 | goto bail; |
| 1502 | } |
| 1503 | |
| 1504 | bail: |
| 1505 | return rc; |
| 1506 | } |
| 1507 | |
| 1508 | static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, |
| 1509 | struct buffer_head *bh) |
| 1510 | { |
| 1511 | int changed = 0; |
| 1512 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; |
| 1513 | |
| 1514 | if (!ocfs2_filecheck_validate_inode_block(sb, bh)) |
| 1515 | return 0; |
| 1516 | |
| 1517 | trace_ocfs2_filecheck_repair_inode_block( |
| 1518 | (unsigned long long)bh->b_blocknr); |
| 1519 | |
| 1520 | if (ocfs2_is_hard_readonly(OCFS2_SB(sb)) || |
| 1521 | ocfs2_is_soft_readonly(OCFS2_SB(sb))) { |
| 1522 | mlog(ML_ERROR, |
| 1523 | "Filecheck: cannot repair dinode #%llu " |
| 1524 | "on readonly filesystem\n", |
| 1525 | (unsigned long long)bh->b_blocknr); |
| 1526 | return -OCFS2_FILECHECK_ERR_READONLY; |
| 1527 | } |
| 1528 | |
| 1529 | if (buffer_jbd(bh)) { |
| 1530 | mlog(ML_ERROR, |
| 1531 | "Filecheck: cannot repair dinode #%llu, " |
| 1532 | "its buffer is in jbd\n", |
| 1533 | (unsigned long long)bh->b_blocknr); |
| 1534 | return -OCFS2_FILECHECK_ERR_INJBD; |
| 1535 | } |
| 1536 | |
| 1537 | if (!OCFS2_IS_VALID_DINODE(di)) { |
| 1538 | /* Cannot fix invalid inode block */ |
| 1539 | return -OCFS2_FILECHECK_ERR_INVALIDINO; |
| 1540 | } |
| 1541 | |
| 1542 | if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { |
| 1543 | /* Cannot just add VALID_FL flag back as a fix, |
| 1544 | * need more things to check here. |
| 1545 | */ |
| 1546 | return -OCFS2_FILECHECK_ERR_VALIDFLAG; |
| 1547 | } |
| 1548 | |
| 1549 | if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { |
| 1550 | di->i_blkno = cpu_to_le64(bh->b_blocknr); |
| 1551 | changed = 1; |
| 1552 | mlog(ML_ERROR, |
| 1553 | "Filecheck: reset dinode #%llu: i_blkno to %llu\n", |
| 1554 | (unsigned long long)bh->b_blocknr, |
| 1555 | (unsigned long long)le64_to_cpu(di->i_blkno)); |
| 1556 | } |
| 1557 | |
| 1558 | if (le32_to_cpu(di->i_fs_generation) != |
| 1559 | OCFS2_SB(sb)->fs_generation) { |
| 1560 | di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); |
| 1561 | changed = 1; |
| 1562 | mlog(ML_ERROR, |
| 1563 | "Filecheck: reset dinode #%llu: fs_generation to %u\n", |
| 1564 | (unsigned long long)bh->b_blocknr, |
| 1565 | le32_to_cpu(di->i_fs_generation)); |
| 1566 | } |
| 1567 | |
| 1568 | if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) { |
| 1569 | ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check); |
| 1570 | mark_buffer_dirty(bh); |
| 1571 | mlog(ML_ERROR, |
| 1572 | "Filecheck: reset dinode #%llu: compute meta ecc\n", |
| 1573 | (unsigned long long)bh->b_blocknr); |
| 1574 | } |
| 1575 | |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | static int |
| 1580 | ocfs2_filecheck_read_inode_block_full(struct inode *inode, |
| 1581 | struct buffer_head **bh, |
| 1582 | int flags, int type) |
| 1583 | { |
| 1584 | int rc; |
| 1585 | struct buffer_head *tmp = *bh; |
| 1586 | |
| 1587 | if (!type) /* Check inode block */ |
| 1588 | rc = ocfs2_read_blocks(INODE_CACHE(inode), |
| 1589 | OCFS2_I(inode)->ip_blkno, |
| 1590 | 1, &tmp, flags, |
| 1591 | ocfs2_filecheck_validate_inode_block); |
| 1592 | else /* Repair inode block */ |
| 1593 | rc = ocfs2_read_blocks(INODE_CACHE(inode), |
| 1594 | OCFS2_I(inode)->ip_blkno, |
| 1595 | 1, &tmp, flags, |
| 1596 | ocfs2_filecheck_repair_inode_block); |
| 1597 | |
| 1598 | /* If ocfs2_read_blocks() got us a new bh, pass it up. */ |
| 1599 | if (!rc && !*bh) |
| 1600 | *bh = tmp; |
| 1601 | |
| 1602 | return rc; |
| 1603 | } |
| 1604 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1605 | int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh, |
| 1606 | int flags) |
| 1607 | { |
| 1608 | int rc; |
| 1609 | struct buffer_head *tmp = *bh; |
| 1610 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1611 | rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno, |
| 1612 | 1, &tmp, flags, ocfs2_validate_inode_block); |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1613 | |
| 1614 | /* If ocfs2_read_blocks() got us a new bh, pass it up. */ |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 1615 | if (!rc && !*bh) |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1616 | *bh = tmp; |
| 1617 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1618 | return rc; |
| 1619 | } |
| 1620 | |
| 1621 | int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh) |
| 1622 | { |
| 1623 | return ocfs2_read_inode_block_full(inode, bh, 0); |
| 1624 | } |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 1625 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 1626 | |
| 1627 | static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci) |
| 1628 | { |
| 1629 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); |
| 1630 | |
| 1631 | return oi->ip_blkno; |
| 1632 | } |
| 1633 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1634 | static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci) |
| 1635 | { |
| 1636 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); |
| 1637 | |
| 1638 | return oi->vfs_inode.i_sb; |
| 1639 | } |
| 1640 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 1641 | static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci) |
| 1642 | { |
| 1643 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); |
| 1644 | |
| 1645 | spin_lock(&oi->ip_lock); |
| 1646 | } |
| 1647 | |
| 1648 | static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci) |
| 1649 | { |
| 1650 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); |
| 1651 | |
| 1652 | spin_unlock(&oi->ip_lock); |
| 1653 | } |
| 1654 | |
| 1655 | static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci) |
| 1656 | { |
| 1657 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); |
| 1658 | |
| 1659 | mutex_lock(&oi->ip_io_mutex); |
| 1660 | } |
| 1661 | |
| 1662 | static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci) |
| 1663 | { |
| 1664 | struct ocfs2_inode_info *oi = cache_info_to_inode(ci); |
| 1665 | |
| 1666 | mutex_unlock(&oi->ip_io_mutex); |
| 1667 | } |
| 1668 | |
| 1669 | const struct ocfs2_caching_operations ocfs2_inode_caching_ops = { |
| 1670 | .co_owner = ocfs2_inode_cache_owner, |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 1671 | .co_get_super = ocfs2_inode_cache_get_super, |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 1672 | .co_cache_lock = ocfs2_inode_cache_lock, |
| 1673 | .co_cache_unlock = ocfs2_inode_cache_unlock, |
| 1674 | .co_io_lock = ocfs2_inode_cache_io_lock, |
| 1675 | .co_io_unlock = ocfs2_inode_cache_io_unlock, |
| 1676 | }; |
| 1677 | |