blob: c56a7679df93a2bc3df147fe8f408b74221988cb [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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 Fashehccd979b2005-12-15 14:31:24 -080028#include <linux/highmem.h>
29#include <linux/pagemap.h>
Jan Karaa90714c2008-10-09 19:38:40 +020030#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080031
32#include <asm/byteorder.h>
33
Mark Fashehccd979b2005-12-15 14:31:24 -080034#include <cluster/masklog.h>
35
36#include "ocfs2.h"
37
38#include "alloc.h"
Mark Fasheh9b7895e2008-11-12 16:27:44 -080039#include "dir.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070040#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080041#include "dlmglue.h"
42#include "extent_map.h"
43#include "file.h"
Mark Fashehb4df6ed2006-02-22 17:35:08 -080044#include "heartbeat.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080045#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 Yangcf1d6c72008-08-18 17:11:00 +080053#include "xattr.h"
Tao Ma8b2c0db2009-08-18 11:43:49 +080054#include "refcounttree.h"
Tao Ma64f3b262011-02-22 22:24:57 +080055#include "ocfs2_trace.h"
Gang Hed56a8f32016-03-22 14:24:30 -070056#include "filecheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080057
58#include "buffer_head_io.h"
59
Mark Fashehccd979b2005-12-15 14:31:24 -080060struct ocfs2_find_inode_args
61{
62 u64 fi_blkno;
63 unsigned long fi_ino;
64 unsigned int fi_flags;
Jan Kara5fa06132008-01-11 00:11:45 +010065 unsigned int fi_sysfile_type;
Mark Fashehccd979b2005-12-15 14:31:24 -080066};
67
Jan Kara5fa06132008-01-11 00:11:45 +010068static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
69
Mark Fashehccd979b2005-12-15 14:31:24 -080070static int ocfs2_read_locked_inode(struct inode *inode,
71 struct ocfs2_find_inode_args *args);
72static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
73static int ocfs2_find_actor(struct inode *inode, void *opaque);
74static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
75 struct inode *inode,
76 struct buffer_head *fe_bh);
77
Gang Hed56a8f32016-03-22 14:24:30 -070078static int ocfs2_filecheck_read_inode_block_full(struct inode *inode,
79 struct buffer_head **bh,
80 int flags, int type);
81static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
82 struct buffer_head *bh);
83static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
84 struct buffer_head *bh);
85
Herbert Poetzlca4d1472006-07-03 17:27:12 -070086void 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 Kara6e4b0d52007-04-27 11:08:01 -0700106/* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
107void 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 wang6ca497a2009-03-06 21:29:10 +0800125struct 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 Kara5fa06132008-01-11 00:11:45 +0100136struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
137 int sysfile_type)
Mark Fashehccd979b2005-12-15 14:31:24 -0800138{
Gang Hed56a8f32016-03-22 14:24:30 -0700139 int rc = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800140 struct inode *inode = NULL;
141 struct super_block *sb = osb->sb;
142 struct ocfs2_find_inode_args args;
Darrick J. Wong2931cdc2014-04-03 14:46:48 -0700143 journal_t *journal = OCFS2_SB(sb)->journal->j_journal;
Mark Fashehccd979b2005-12-15 14:31:24 -0800144
Tao Ma64f3b262011-02-22 22:24:57 +0800145 trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
146 sysfile_type);
Mark Fashehccd979b2005-12-15 14:31:24 -0800147
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 Fasheh24c19ef2006-09-22 17:28:19 -0700158 args.fi_flags = flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800159 args.fi_ino = ino_from_blkno(sb, blkno);
Jan Kara5fa06132008-01-11 00:11:45 +0100160 args.fi_sysfile_type = sysfile_type;
Mark Fashehccd979b2005-12-15 14:31:24 -0800161
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 Fashehccd979b2005-12-15 14:31:24 -0800167 if (inode == NULL) {
168 inode = ERR_PTR(-ENOMEM);
169 mlog_errno(PTR_ERR(inode));
170 goto bail;
171 }
Tao Ma64f3b262011-02-22 22:24:57 +0800172 trace_ocfs2_iget5_locked(inode->i_state);
Tao Ma6218b902011-02-21 11:18:30 +0800173 if (inode->i_state & I_NEW) {
Gang Hed56a8f32016-03-22 14:24:30 -0700174 rc = ocfs2_read_locked_inode(inode, &args);
Tao Ma6218b902011-02-21 11:18:30 +0800175 unlock_new_inode(inode);
176 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800177 if (is_bad_inode(inode)) {
178 iput(inode);
Eric Ren1f3a4372016-05-26 15:16:16 -0700179 inode = ERR_PTR(rc);
Mark Fashehccd979b2005-12-15 14:31:24 -0800180 goto bail;
181 }
182
Darrick J. Wong2931cdc2014-04-03 14:46:48 -0700183 /*
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 Fashehccd979b2005-12-15 14:31:24 -0800209bail:
210 if (!IS_ERR(inode)) {
Tao Ma64f3b262011-02-22 22:24:57 +0800211 trace_ocfs2_iget_end(inode,
212 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh6a1bd4a2007-01-03 17:06:59 -0800213 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800214
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
226static 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 Fashehccd979b2005-12-15 14:31:24 -0800232 args = opaque;
233
234 mlog_bug_on_msg(!inode, "No inode in find actor!\n");
235
Tao Ma64f3b262011-02-22 22:24:57 +0800236 trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno);
237
Mark Fashehccd979b2005-12-15 14:31:24 -0800238 if (oi->ip_blkno != args->fi_blkno)
239 goto bail;
240
Mark Fashehccd979b2005-12-15 14:31:24 -0800241 ret = 1;
242bail:
Mark Fashehccd979b2005-12-15 14:31:24 -0800243 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 */
251static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
252{
253 struct ocfs2_find_inode_args *args = opaque;
Jan Karacb257972009-06-04 15:26:50 +0200254 static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
255 ocfs2_file_ip_alloc_sem_key;
Mark Fashehccd979b2005-12-15 14:31:24 -0800256
Mark Fashehccd979b2005-12-15 14:31:24 -0800257 inode->i_ino = args->fi_ino;
258 OCFS2_I(inode)->ip_blkno = args->fi_blkno;
Jan Kara5fa06132008-01-11 00:11:45 +0100259 if (args->fi_sysfile_type != 0)
Al Viro9902af72016-04-15 15:08:36 -0400260 lockdep_set_class(&inode->i_rwsem,
Jan Kara5fa06132008-01-11 00:11:45 +0100261 &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
Jan Karacb257972009-06-04 15:26:50 +0200262 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 Fashehccd979b2005-12-15 14:31:24 -0800271
Mark Fashehccd979b2005-12-15 14:31:24 -0800272 return 0;
273}
274
Joel Beckerb657c952008-11-13 14:49:11 -0800275void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
276 int create_ino)
Mark Fashehccd979b2005-12-15 14:31:24 -0800277{
278 struct super_block *sb;
279 struct ocfs2_super *osb;
Mark Fasheh53da4932008-07-21 14:29:16 -0700280 int use_plocks = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800281
Mark Fashehccd979b2005-12-15 14:31:24 -0800282 sb = inode->i_sb;
283 osb = OCFS2_SB(sb);
284
Mark Fasheh53da4932008-07-21 14:29:16 -0700285 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
286 ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
287 use_plocks = 0;
288
Joel Beckerb657c952008-11-13 14:49:11 -0800289 /*
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 Fashehccd979b2005-12-15 14:31:24 -0800299
Mark Fashehccd979b2005-12-15 14:31:24 -0800300
Mark Fasheh8110b072007-03-22 16:53:23 -0700301 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
302 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -0700303 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
Mark Fasheh8110b072007-03-22 16:53:23 -0700304
Mark Fashehccd979b2005-12-15 14:31:24 -0800305 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. Biederman2c034172013-01-31 17:27:11 -0800309 i_uid_write(inode, le32_to_cpu(fe->i_uid));
310 i_gid_write(inode, le32_to_cpu(fe->i_gid));
Mark Fashehccd979b2005-12-15 14:31:24 -0800311
312 /* Fast symlinks will have i_size but no allocated clusters. */
Al Viroea022dfb2012-05-03 10:14:29 -0400313 if (S_ISLNK(inode->i_mode) && !fe->i_clusters) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800314 inode->i_blocks = 0;
Al Viroea022dfb2012-05-03 10:14:29 -0400315 inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
316 } else {
Mark Fasheh8110b072007-03-22 16:53:23 -0700317 inode->i_blocks = ocfs2_inode_sector_count(inode);
Al Viroea022dfb2012-05-03 10:14:29 -0400318 inode->i_mapping->a_ops = &ocfs2_aops;
319 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800320 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 Fashehb06970532006-03-03 10:24:33 -0800329 "ip_blkno %llu != i_blkno %llu!\n",
330 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700331 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800332
Miklos Szeredibfe86842011-10-28 14:13:29 +0200333 set_nlink(inode, ocfs2_read_links_count(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -0800334
Tao Ma64f3b262011-02-22 22:24:57 +0800335 trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno,
336 le32_to_cpu(fe->i_flags));
Jan Karabbbd0eb2008-08-21 18:22:30 +0200337 if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700338 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
Jan Karabbbd0eb2008-08-21 18:22:30 +0200339 inode->i_flags |= S_NOQUOTA;
340 }
Tao Ma64f3b262011-02-22 22:24:57 +0800341
Mark Fashehccd979b2005-12-15 14:31:24 -0800342 if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
343 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
Mark Fashehccd979b2005-12-15 14:31:24 -0800344 } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
345 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
Jan Kara1a224ad2008-08-20 15:43:36 +0200346 } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
347 inode->i_flags |= S_NOQUOTA;
Mark Fashehccd979b2005-12-15 14:31:24 -0800348 } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800349 /* 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 Fasheh53da4932008-07-21 14:29:16 -0700356 if (use_plocks)
357 inode->i_fop = &ocfs2_fops;
358 else
359 inode->i_fop = &ocfs2_fops_no_plocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800360 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 Fasheh53da4932008-07-21 14:29:16 -0700365 if (use_plocks)
366 inode->i_fop = &ocfs2_dops;
367 else
368 inode->i_fop = &ocfs2_dops_no_plocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800369 i_size_write(inode, le64_to_cpu(fe->i_size));
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -0500370 OCFS2_I(inode)->ip_dir_lock_gen = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800371 break;
372 case S_IFLNK:
Al Viroea022dfb2012-05-03 10:14:29 -0400373 inode->i_op = &ocfs2_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500374 inode_nohighmem(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800375 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 Fasheh24c19ef2006-09-22 17:28:19 -0700384 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 Fasheh1ca1a112007-04-27 16:01:25 -0700393 BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700394
Mark Fashehe63aecb62007-10-18 15:30:42 -0700395 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700396 OCFS2_LOCK_TYPE_META, 0, inode);
Tiger Yang50008632007-03-20 16:01:38 -0700397
398 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
399 OCFS2_LOCK_TYPE_OPEN, 0, inode);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700400 }
401
Mark Fashehccd979b2005-12-15 14:31:24 -0800402 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700403 OCFS2_LOCK_TYPE_RW, inode->i_generation,
404 inode);
405
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700406 ocfs2_set_inode_flags(inode);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700407
Tao Ma13821152009-02-25 00:53:23 +0800408 OCFS2_I(inode)->ip_last_used_slot = 0;
409 OCFS2_I(inode)->ip_last_used_group = 0;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800410
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 Fashehccd979b2005-12-15 14:31:24 -0800414}
415
416static 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 Hed56a8f32016-03-22 14:24:30 -0700423 int status, can_lock, lock_level = 0;
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700424 u32 generation = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800425
Mark Fashehccd979b2005-12-15 14:31:24 -0800426 status = -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800427 sb = inode->i_sb;
428 osb = OCFS2_SB(sb);
429
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700430 /*
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önigb5950762010-11-01 15:38:34 -0400454 * concern is user-accessible files anyway.
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700455 *
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 Yang50008632007-03-20 16:01:38 -0700460 && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800461 && !ocfs2_mount_local(osb);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700462
Tao Ma64f3b262011-02-22 22:24:57 +0800463 trace_ocfs2_read_locked_inode(
464 (unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock);
465
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700466 /*
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 Fashehe63aecb62007-10-18 15:30:42 -0700476 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700477 OCFS2_LOCK_TYPE_META,
478 generation, inode);
479
Tiger Yang50008632007-03-20 16:01:38 -0700480 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
481 OCFS2_LOCK_TYPE_OPEN,
482 0, inode);
483
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700484 if (can_lock) {
Tiger Yang50008632007-03-20 16:01:38 -0700485 status = ocfs2_open_lock(inode);
486 if (status) {
487 make_bad_inode(inode);
488 mlog_errno(status);
489 return status;
490 }
Gang Hed56a8f32016-03-22 14:24:30 -0700491 status = ocfs2_inode_lock(inode, NULL, lock_level);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700492 if (status) {
493 make_bad_inode(inode);
494 mlog_errno(status);
495 return status;
496 }
497 }
498
Tiger Yang50008632007-03-20 16:01:38 -0700499 if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
500 status = ocfs2_try_open_lock(inode, 0);
501 if (status) {
Sunil Mushran2bd63212010-01-25 16:57:38 -0800502 make_bad_inode(inode);
Tiger Yang50008632007-03-20 16:01:38 -0700503 return status;
504 }
505 }
506
Joel Beckerb657c952008-11-13 14:49:11 -0800507 if (can_lock) {
Gang Hed56a8f32016-03-22 14:24:30 -0700508 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 Beckerb657c952008-11-13 14:49:11 -0800517 } else {
Joel Beckerda1e9092008-10-09 17:20:29 -0700518 status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
Sunil Mushranf5ce5a02010-08-12 16:24:26 -0700519 /*
520 * If buffer is in jbd, then its checksum may not have been
521 * computed as yet.
522 */
Gang Hed56a8f32016-03-22 14:24:30 -0700523 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 Beckerb657c952008-11-13 14:49:11 -0800534 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800535 if (status < 0) {
536 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 goto bail;
538 }
539
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700540 status = -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800541 fe = (struct ocfs2_dinode *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800542
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700543 /*
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 Fashehccd979b2005-12-15 14:31:24 -0800552
553 if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
554 S_ISBLK(le16_to_cpu(fe->i_mode)))
Joel Beckerb657c952008-11-13 14:49:11 -0800555 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
Mark Fashehccd979b2005-12-15 14:31:24 -0800556
Joel Beckerb657c952008-11-13 14:49:11 -0800557 ocfs2_populate_inode(inode, fe, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800558
559 BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
560
Gang Hed56a8f32016-03-22 14:24:30 -0700561 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 Fashehccd979b2005-12-15 14:31:24 -0800574 status = 0;
575
576bail:
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700577 if (can_lock)
Gang Hed56a8f32016-03-22 14:24:30 -0700578 ocfs2_inode_unlock(inode, lock_level);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700579
580 if (status < 0)
581 make_bad_inode(inode);
582
Jan Kara4a635a12014-12-10 15:41:42 -0800583 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800584
Mark Fashehccd979b2005-12-15 14:31:24 -0800585 return status;
586}
587
588void ocfs2_sync_blockdev(struct super_block *sb)
589{
590 sync_blockdev(sb->s_bdev);
591}
592
593static 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 Fashehccd979b2005-12-15 14:31:24 -0800598 struct ocfs2_dinode *fe;
Mark Fasheh60b11392007-02-16 11:46:50 -0800599 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800600
Mark Fashehccd979b2005-12-15 14:31:24 -0800601 fe = (struct ocfs2_dinode *) fe_bh->b_data;
602
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700603 /*
604 * This check will also skip truncate of inodes with inline
605 * data and fast symlinks.
606 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800607 if (fe->i_clusters) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700608 if (ocfs2_should_order_data(inode))
609 ocfs2_begin_ordered_truncate(inode, 0);
610
Mark Fasheh60b11392007-02-16 11:46:50 -0800611 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
612 if (IS_ERR(handle)) {
613 status = PTR_ERR(handle);
Dan Carpenter0350cb02010-04-22 11:39:29 +0200614 handle = NULL;
Mark Fasheh60b11392007-02-16 11:46:50 -0800615 mlog_errno(status);
616 goto out;
617 }
618
Joel Becker0cf2f762009-02-12 16:41:25 -0800619 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
620 fe_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700621 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -0800622 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 Ye78f94672010-05-11 17:54:42 +0800638 status = ocfs2_commit_truncate(osb, inode, fe_bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800639 if (status < 0) {
640 mlog_errno(status);
641 goto out;
642 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800643 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800644
Mark Fasheh60b11392007-02-16 11:46:50 -0800645out:
646 if (handle)
647 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800648 return status;
649}
650
651static 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 Fasheh1fabe142006-10-09 18:11:45 -0700659 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800660 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 Qi62f8b1f2015-04-14 15:43:24 -0700667 status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -0800668 mlog_errno(status);
669 goto bail;
670 }
671
Al Viro59551022016-01-22 15:40:57 -0500672 inode_lock(inode_alloc_inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700673 status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800674 if (status < 0) {
Al Viro59551022016-01-22 15:40:57 -0500675 inode_unlock(inode_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800676
677 mlog_errno(status);
678 goto bail;
679 }
680
Jan Karaa90714c2008-10-09 19:38:40 +0200681 handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800682 ocfs2_quota_trans_credits(inode->i_sb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800683 if (IS_ERR(handle)) {
684 status = PTR_ERR(handle);
685 mlog_errno(status);
686 goto bail_unlock;
687 }
688
Li Dongyangd4cd1872010-04-22 16:11:19 +0800689 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
690 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
Joseph Qi06ee5c72015-02-16 15:59:54 -0800691 orphan_dir_bh, false);
Li Dongyangd4cd1872010-04-22 16:11:19 +0800692 if (status < 0) {
693 mlog_errno(status);
694 goto bail_commit;
695 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800696 }
697
698 /* set the inodes dtime */
Joel Becker0cf2f762009-02-12 16:41:25 -0800699 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700700 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800701 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 Slusarz4092d492007-12-25 15:52:59 +0100707 di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
Joel Beckerec20cec2010-03-19 14:13:52 -0700708 ocfs2_journal_dirty(handle, di_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800709
Joel Becker8cb471e2009-02-10 20:00:41 -0800710 ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500711 dquot_free_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800712
713 status = ocfs2_free_dinode(handle, inode_alloc_inode,
714 inode_alloc_bh, di);
715 if (status < 0)
716 mlog_errno(status);
717
718bail_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700719 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800720bail_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700721 ocfs2_inode_unlock(inode_alloc_inode, 1);
Al Viro59551022016-01-22 15:40:57 -0500722 inode_unlock(inode_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800723 brelse(inode_alloc_bh);
724bail:
725 iput(inode_alloc_inode);
726
727 return status;
728}
729
Sunil Mushran2bd63212010-01-25 16:57:38 -0800730/*
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800731 * 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 */
736static 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 Fashehb4df6ed2006-02-22 17:35:08 -0800743 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]++;
749out:
750 spin_unlock(&osb->osb_lock);
Tao Ma64f3b262011-02-22 22:24:57 +0800751 trace_ocfs2_check_orphan_recovery_state(slot, ret);
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800752 return ret;
753}
754
755static 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 Fashehccd979b2005-12-15 14:31:24 -0800765static int ocfs2_wipe_inode(struct inode *inode,
766 struct buffer_head *di_bh)
767{
Joel Beckerd5776322010-05-03 19:15:49 -0700768 int status, orphaned_slot = -1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800769 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 Dongyangd4cd1872010-04-22 16:11:19 +0800772 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800773
Li Dongyangd4cd1872010-04-22 16:11:19 +0800774 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
775 orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800776
Li Dongyangd4cd1872010-04-22 16:11:19 +0800777 status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
778 if (status)
779 return status;
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800780
Li Dongyangd4cd1872010-04-22 16:11:19 +0800781 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
782 ORPHAN_DIR_SYSTEM_INODE,
783 orphaned_slot);
784 if (!orphan_dir_inode) {
Joseph Qi62f8b1f2015-04-14 15:43:24 -0700785 status = -ENOENT;
Li Dongyangd4cd1872010-04-22 16:11:19 +0800786 mlog_errno(status);
787 goto bail;
788 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800789
Li Dongyangd4cd1872010-04-22 16:11:19 +0800790 /* 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 Viro59551022016-01-22 15:40:57 -0500793 inode_lock(orphan_dir_inode);
Li Dongyangd4cd1872010-04-22 16:11:19 +0800794 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
795 if (status < 0) {
Al Viro59551022016-01-22 15:40:57 -0500796 inode_unlock(orphan_dir_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800797
Li Dongyangd4cd1872010-04-22 16:11:19 +0800798 mlog_errno(status);
799 goto bail;
800 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800801 }
802
803 /* we do this while holding the orphan dir lock because we
Mark Fasheh34d024f2007-09-24 15:56:19 -0700804 * don't want recovery being run from another node to try an
805 * inode delete underneath us -- this will result in two nodes
Mark Fashehccd979b2005-12-15 14:31:24 -0800806 * 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 Fasheh9b7895e2008-11-12 16:27:44 -0800813 /* 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 Yangcf1d6c72008-08-18 17:11:00 +0800822 /*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 Ma8b2c0db2009-08-18 11:43:49 +0800829 status = ocfs2_remove_refcount_tree(inode, di_bh);
830 if (status < 0) {
831 mlog_errno(status);
832 goto bail_unlock_dir;
833 }
834
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
836 orphan_dir_bh);
837 if (status < 0)
838 mlog_errno(status);
839
840bail_unlock_dir:
Li Dongyangd4cd1872010-04-22 16:11:19 +0800841 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
842 return status;
843
Mark Fashehe63aecb62007-10-18 15:30:42 -0700844 ocfs2_inode_unlock(orphan_dir_inode, 1);
Al Viro59551022016-01-22 15:40:57 -0500845 inode_unlock(orphan_dir_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800846 brelse(orphan_dir_bh);
847bail:
848 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800849 ocfs2_signal_wipe_completion(osb, orphaned_slot);
Mark Fashehccd979b2005-12-15 14:31:24 -0800850
851 return status;
852}
853
854/* There is a series of simple checks that should be done before a
Mark Fasheh34d024f2007-09-24 15:56:19 -0700855 * trylock is even considered. Encapsulate those in this function. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800856static 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 Ma64f3b262011-02-22 22:24:57 +0800862 trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
863 (unsigned long long)oi->ip_blkno,
864 oi->ip_flags);
865
Mark Fashehccd979b2005-12-15 14:31:24 -0800866 /* 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 Karabd62ad72014-04-03 14:46:54 -0700873 /*
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 Ma64f3b262011-02-22 22:24:57 +0800880 if (current == osb->dc_task)
Mark Fashehccd979b2005-12-15 14:31:24 -0800881 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800882
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 Fashehb06970532006-03-03 10:24:33 -0800888 mlog(ML_ERROR, "Skipping delete of system file %llu\n",
889 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890 goto bail_unlock;
891 }
892
Mark Fashehccd979b2005-12-15 14:31:24 -0800893 ret = 1;
894bail_unlock:
895 spin_unlock(&oi->ip_lock);
896bail:
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. */
904static int ocfs2_query_inode_wipe(struct inode *inode,
905 struct buffer_head *di_bh,
906 int *wipe)
907{
Tao Ma64f3b262011-02-22 22:24:57 +0800908 int status = 0, reason = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800909 struct ocfs2_inode_info *oi = OCFS2_I(inode);
910 struct ocfs2_dinode *di;
911
912 *wipe = 0;
913
Tao Ma64f3b262011-02-22 22:24:57 +0800914 trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
915 inode->i_nlink);
916
Mark Fashehccd979b2005-12-15 14:31:24 -0800917 /* 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 Ma64f3b262011-02-22 22:24:57 +0800921 reason = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800922 goto bail;
923 }
924
925 /* Now that we have an up to date inode, we can double check
926 * the link count. */
Tao Ma64f3b262011-02-22 22:24:57 +0800927 if (inode->i_nlink)
Mark Fashehccd979b2005-12-15 14:31:24 -0800928 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800929
930 /* Do some basic inode verification... */
931 di = (struct ocfs2_dinode *) di_bh->b_data;
Li Dongyangd4cd1872010-04-22 16:11:19 +0800932 if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
933 !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
Tristan Yeb54c2ca2010-03-19 09:21:10 +0800934 /*
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 Ma64f3b262011-02-22 22:24:57 +0800943 reason = 2;
Tristan Yeb54c2ca2010-03-19 09:21:10 +0800944 goto bail;
945 }
946
Mark Fashehccd979b2005-12-15 14:31:24 -0800947 /* for lack of a better error? */
948 status = -EEXIST;
949 mlog(ML_ERROR,
Mark Fashehb06970532006-03-03 10:24:33 -0800950 "Inode %llu (on-disk %llu) not orphaned! "
Mark Fashehccd979b2005-12-15 14:31:24 -0800951 "Disk flags 0x%x, inode flags 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -0800952 (unsigned long long)oi->ip_blkno,
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700953 (unsigned long long)le64_to_cpu(di->i_blkno),
954 le32_to_cpu(di->i_flags), oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800955 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 Fasheh6f16bf62007-03-20 17:17:54 -0700965 /*
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 Marchi25985ed2011-03-30 22:57:33 -0300971 * meta data lock. If the upconvert succeeds, we know the inode
Mark Fasheh6f16bf62007-03-20 17:17:54 -0700972 * 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 Yang50008632007-03-20 16:01:38 -0700978 if (status == -EAGAIN) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800979 status = 0;
Tao Ma64f3b262011-02-22 22:24:57 +0800980 reason = 3;
Mark Fashehccd979b2005-12-15 14:31:24 -0800981 goto bail;
982 }
983 if (status < 0) {
984 mlog_errno(status);
985 goto bail;
986 }
987
Tiger Yang50008632007-03-20 16:01:38 -0700988 *wipe = 1;
Tao Ma64f3b262011-02-22 22:24:57 +0800989 trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
Mark Fashehccd979b2005-12-15 14:31:24 -0800990
991bail:
Tao Ma64f3b262011-02-22 22:24:57 +0800992 trace_ocfs2_query_inode_wipe_end(status, reason);
Mark Fashehccd979b2005-12-15 14:31:24 -0800993 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. */
999static void ocfs2_cleanup_delete_inode(struct inode *inode,
1000 int sync_data)
1001{
Tao Ma64f3b262011-02-22 22:24:57 +08001002 trace_ocfs2_cleanup_delete_inode(
1003 (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
Mark Fashehccd979b2005-12-15 14:31:24 -08001004 if (sync_data)
Jan Kara249ec932011-11-08 00:20:39 +01001005 filemap_write_and_wait(inode->i_mapping);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07001006 truncate_inode_pages_final(&inode->i_data);
Mark Fashehccd979b2005-12-15 14:31:24 -08001007}
1008
Al Viro066d92d2010-06-08 21:28:10 -04001009static void ocfs2_delete_inode(struct inode *inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001010{
1011 int wipe, status;
Joel Beckere4b963f2009-09-02 17:17:36 -07001012 sigset_t oldset;
Mark Fashehccd979b2005-12-15 14:31:24 -08001013 struct buffer_head *di_bh = NULL;
Joseph Qiad694822015-09-04 15:44:40 -07001014 struct ocfs2_dinode *di = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001015
Tao Ma64f3b262011-02-22 22:24:57 +08001016 trace_ocfs2_delete_inode(inode->i_ino,
1017 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1018 is_bad_inode(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08001019
Jan Karaa90714c2008-10-09 19:38:40 +02001020 /* 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 Ma64f3b262011-02-22 22:24:57 +08001023 if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -08001024 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001025
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 Karabd62ad72014-04-03 14:46:54 -07001034 dquot_initialize(inode);
1035
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 /* 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 Beckere4b963f2009-09-02 17:17:36 -07001040 ocfs2_block_signals(&oldset);
Mark Fashehccd979b2005-12-15 14:31:24 -08001041
wengang wang6ca497a2009-03-06 21:29:10 +08001042 /*
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 Fashehccd979b2005-12-15 14:31:24 -08001053 /* Lock down the inode. This gives us an up to date view of
1054 * it's metadata (for verification), and allows us to
Mark Fasheh34d024f2007-09-24 15:56:19 -07001055 * serialize delete_inode on multiple nodes.
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 *
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 Fashehe63aecb62007-10-18 15:30:42 -07001061 status = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001062 if (status < 0) {
1063 if (status != -ENOENT)
1064 mlog_errno(status);
1065 ocfs2_cleanup_delete_inode(inode, 0);
wengang wang6ca497a2009-03-06 21:29:10 +08001066 goto bail_unlock_nfs_sync;
Mark Fashehccd979b2005-12-15 14:31:24 -08001067 }
1068
Joseph Qiad694822015-09-04 15:44:40 -07001069 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 Fashehccd979b2005-12-15 14:31:24 -08001077 /* 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 Fasheh34d024f2007-09-24 15:56:19 -07001081 /* Error and remote inode busy both mean we won't be
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 * removing the inode, so they take almost the same
1083 * path. */
1084 if (status < 0)
1085 mlog_errno(status);
1086
Mark Fasheh34d024f2007-09-24 15:56:19 -07001087 /* 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 Fashehccd979b2005-12-15 14:31:24 -08001090 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 Fashehb4df6ed2006-02-22 17:35:08 -08001098 if (status != -EDEADLK)
1099 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001100 goto bail_unlock_inode;
1101 }
1102
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001103 /*
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 Fashehccd979b2005-12-15 14:31:24 -08001112 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1113
1114bail_unlock_inode:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001115 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001116 brelse(di_bh);
wengang wang6ca497a2009-03-06 21:29:10 +08001117
1118bail_unlock_nfs_sync:
1119 ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
1120
Mark Fashehccd979b2005-12-15 14:31:24 -08001121bail_unblock:
Joel Beckere4b963f2009-09-02 17:17:36 -07001122 ocfs2_unblock_signals(&oldset);
Mark Fashehccd979b2005-12-15 14:31:24 -08001123bail:
Tao Mac1e8d352011-03-07 16:43:21 +08001124 return;
Mark Fashehccd979b2005-12-15 14:31:24 -08001125}
1126
Al Viro066d92d2010-06-08 21:28:10 -04001127static void ocfs2_clear_inode(struct inode *inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001128{
1129 int status;
1130 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Jan Kara84d86f82014-04-03 14:46:57 -07001131 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001132
Jan Karadbd57682012-05-03 14:48:02 +02001133 clear_inode(inode);
Tao Ma64f3b262011-02-22 22:24:57 +08001134 trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
1135 inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -08001136
1137 mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1138 "Inode=%lu\n", inode->i_ino);
1139
Christoph Hellwig9f754752010-03-03 09:05:05 -05001140 dquot_drop(inode);
Christoph Hellwig257ba152010-03-03 09:05:04 -05001141
Mark Fasheh34d024f2007-09-24 15:56:19 -07001142 /* To preven remote deletes we hold open lock before, now it
1143 * is time to unlock PR and EX open locks. */
Tiger Yang50008632007-03-20 16:01:38 -07001144 ocfs2_open_unlock(inode);
1145
Mark Fashehccd979b2005-12-15 14:31:24 -08001146 /* Do these before all the other work so that we don't bounce
Mark Fasheh34d024f2007-09-24 15:56:19 -07001147 * the downconvert thread while waiting to destroy the locks. */
Jan Kara84d86f82014-04-03 14:46:57 -07001148 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 Fashehccd979b2005-12-15 14:31:24 -08001151
Mark Fasheh4fe370a2009-12-07 13:15:40 -08001152 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 Fashehccd979b2005-12-15 14:31:24 -08001156 /* 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 Fashehb06970532006-03-03 10:24:33 -08001166 "Clear inode of %llu, inode has io markers\n",
1167 (unsigned long long)oi->ip_blkno);
Ryan Ding4506cfb2016-03-25 14:21:06 -07001168 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 Fashehccd979b2005-12-15 14:31:24 -08001171
Mark Fasheh83418972007-04-23 18:53:12 -07001172 ocfs2_extent_map_trunc(inode, 0);
1173
Mark Fashehccd979b2005-12-15 14:31:24 -08001174 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 Fashehe63aecb62007-10-18 15:30:42 -07001179 ocfs2_lock_res_free(&oi->ip_inode_lockres);
Tiger Yang50008632007-03-20 16:01:38 -07001180 ocfs2_lock_res_free(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001181
Joel Becker66fb3452009-02-12 15:24:40 -08001182 ocfs2_metadata_cache_exit(INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08001183
Joel Becker8cb471e2009-02-10 20:00:41 -08001184 mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
Mark Fashehb06970532006-03-03 10:24:33 -08001185 "Clear inode of %llu, inode has %u cache items\n",
Joel Becker8cb471e2009-02-10 20:00:41 -08001186 (unsigned long long)oi->ip_blkno,
1187 INODE_CACHE(inode)->ci_num_cached);
Mark Fashehccd979b2005-12-15 14:31:24 -08001188
Joel Becker8cb471e2009-02-10 20:00:41 -08001189 mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
Mark Fashehb06970532006-03-03 10:24:33 -08001190 "Clear inode of %llu, inode has a bad flag\n",
1191 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001192
1193 mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
Mark Fashehb06970532006-03-03 10:24:33 -08001194 "Clear inode of %llu, inode is locked\n",
1195 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001196
Mark Fasheh251b6ec2006-01-10 15:41:43 -08001197 mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
Mark Fashehb06970532006-03-03 10:24:33 -08001198 "Clear inode of %llu, io_mutex is locked\n",
1199 (unsigned long long)oi->ip_blkno);
Mark Fasheh251b6ec2006-01-10 15:41:43 -08001200 mutex_unlock(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001201
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 Fashehb06970532006-03-03 10:24:33 -08001207 "Clear inode of %llu, alloc_sem is locked\n",
1208 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001209 up_write(&oi->ip_alloc_sem);
1210
1211 mlog_bug_on_msg(oi->ip_open_count,
Mark Fashehb06970532006-03-03 10:24:33 -08001212 "Clear inode of %llu has open count %d\n",
1213 (unsigned long long)oi->ip_blkno, oi->ip_open_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001214
1215 /* Clear all other flags. */
Joel Becker47460d62009-02-10 16:05:07 -08001216 oi->ip_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001217 oi->ip_dir_start_lookup = 0;
1218 oi->ip_blkno = 0ULL;
Sunil Mushranae0dff62008-10-22 13:24:29 -07001219
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 Becker2b4e30f2008-09-03 20:03:41 -07001225 jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
1226 &oi->ip_jinode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001227}
1228
Al Viro066d92d2010-06-08 21:28:10 -04001229void 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 Weiner91b0abe2014-04-03 14:47:49 -07001235 truncate_inode_pages_final(&inode->i_data);
Al Viro066d92d2010-06-08 21:28:10 -04001236 }
1237 ocfs2_clear_inode(inode);
1238}
1239
Mark Fashehccd979b2005-12-15 14:31:24 -08001240/* 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 Viro45321ac2010-06-07 13:43:19 -04001243int ocfs2_drop_inode(struct inode *inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001244{
1245 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1246
Tao Ma64f3b262011-02-22 22:24:57 +08001247 trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno,
1248 inode->i_nlink, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001249
Xue jiufei513e2da2015-09-04 15:44:11 -07001250 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 Fashehccd979b2005-12-15 14:31:24 -08001257
Xue jiufei513e2da2015-09-04 15:44:11 -07001258 return 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001259}
1260
1261/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 * This is called from our getattr.
1263 */
1264int ocfs2_inode_revalidate(struct dentry *dentry)
1265{
David Howells2b0143b2015-03-17 22:25:59 +00001266 struct inode *inode = d_inode(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -08001267 int status = 0;
1268
Tao Ma64f3b262011-02-22 22:24:57 +08001269 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 Fashehccd979b2005-12-15 14:31:24 -08001272
1273 if (!inode) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001274 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 Fashehccd979b2005-12-15 14:31:24 -08001281 status = -ENOENT;
1282 goto bail;
1283 }
1284 spin_unlock(&OCFS2_I(inode)->ip_lock);
1285
Mark Fashehe63aecb62007-10-18 15:30:42 -07001286 /* Let ocfs2_inode_lock do the work of updating our struct
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 * inode for us. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001288 status = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 if (status < 0) {
1290 if (status != -ENOENT)
1291 mlog_errno(status);
1292 goto bail;
1293 }
Mark Fashehe63aecb62007-10-18 15:30:42 -07001294 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001295bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001296 return status;
1297}
1298
1299/*
1300 * Updates a disk inode from a
1301 * struct inode.
1302 * Only takes ip_lock.
1303 */
Mark Fasheh1fabe142006-10-09 18:11:45 -07001304int ocfs2_mark_inode_dirty(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001305 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 Ma64f3b262011-02-22 22:24:57 +08001311 trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001312
Joel Becker0cf2f762009-02-12 16:41:25 -08001313 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -07001314 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001315 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 Kara6e4b0d52007-04-27 11:08:01 -07001322 ocfs2_get_inode_flags(OCFS2_I(inode));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001323 fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001324 fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 spin_unlock(&OCFS2_I(inode)->ip_lock);
1326
1327 fe->i_size = cpu_to_le64(i_size_read(inode));
Mark Fasheh198a1ca2008-11-20 17:54:57 -08001328 ocfs2_set_links_count(fe, inode->i_nlink);
Eric W. Biederman2c034172013-01-31 17:27:11 -08001329 fe->i_uid = cpu_to_le32(i_uid_read(inode));
1330 fe->i_gid = cpu_to_le32(i_gid_read(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08001331 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 Beckerec20cec2010-03-19 14:13:52 -07001339 ocfs2_journal_dirty(handle, bh);
Darrick J. Wong2931cdc2014-04-03 14:46:48 -07001340 ocfs2_update_inode_fsync_trans(handle, inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001341leave:
Mark Fashehccd979b2005-12-15 14:31:24 -08001342 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 */
1350void ocfs2_refresh_inode(struct inode *inode,
1351 struct ocfs2_dinode *fe)
1352{
Mark Fashehccd979b2005-12-15 14:31:24 -08001353 spin_lock(&OCFS2_I(inode)->ip_lock);
1354
1355 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001356 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001357 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001358 ocfs2_set_inode_flags(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001359 i_size_write(inode, le64_to_cpu(fe->i_size));
Miklos Szeredibfe86842011-10-28 14:13:29 +02001360 set_nlink(inode, ocfs2_read_links_count(fe));
Eric W. Biederman2c034172013-01-31 17:27:11 -08001361 i_uid_write(inode, le32_to_cpu(fe->i_uid));
1362 i_gid_write(inode, le32_to_cpu(fe->i_gid));
Mark Fashehccd979b2005-12-15 14:31:24 -08001363 inode->i_mode = le16_to_cpu(fe->i_mode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001364 if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1365 inode->i_blocks = 0;
1366 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001367 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001368 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 Beckerb657c952008-11-13 14:49:11 -08001377
1378int ocfs2_validate_inode_block(struct super_block *sb,
1379 struct buffer_head *bh)
1380{
Joel Beckerd6b32bb2008-10-17 14:55:01 -07001381 int rc;
Joel Beckerb657c952008-11-13 14:49:11 -08001382 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1383
Tao Ma64f3b262011-02-22 22:24:57 +08001384 trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
Joel Becker970e4932008-11-13 14:49:19 -08001385
Joel Beckerb657c952008-11-13 14:49:11 -08001386 BUG_ON(!buffer_uptodate(bh));
1387
Joel Beckerd6b32bb2008-10-17 14:55:01 -07001388 /*
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 Becker13723d02008-10-17 19:25:01 -07001394 if (rc) {
1395 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
1396 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -07001397 goto bail;
Joel Becker13723d02008-10-17 19:25:01 -07001398 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -07001399
1400 /*
1401 * Errors after here are fatal.
1402 */
1403
1404 rc = -EINVAL;
1405
Joel Beckerb657c952008-11-13 14:49:11 -08001406 if (!OCFS2_IS_VALID_DINODE(di)) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -07001407 rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
Joe Perches7ecef142015-09-04 15:44:51 -07001408 (unsigned long long)bh->b_blocknr, 7,
1409 di->i_signature);
Joel Beckerb657c952008-11-13 14:49:11 -08001410 goto bail;
1411 }
1412
1413 if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -07001414 rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
Joe Perches7ecef142015-09-04 15:44:51 -07001415 (unsigned long long)bh->b_blocknr,
1416 (unsigned long long)le64_to_cpu(di->i_blkno));
Joel Beckerb657c952008-11-13 14:49:11 -08001417 goto bail;
1418 }
1419
1420 if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -07001421 rc = ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -07001422 "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
1423 (unsigned long long)bh->b_blocknr);
Joel Beckerb657c952008-11-13 14:49:11 -08001424 goto bail;
1425 }
1426
1427 if (le32_to_cpu(di->i_fs_generation) !=
1428 OCFS2_SB(sb)->fs_generation) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -07001429 rc = ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -07001430 "Invalid dinode #%llu: fs_generation is %u\n",
1431 (unsigned long long)bh->b_blocknr,
1432 le32_to_cpu(di->i_fs_generation));
Joel Beckerb657c952008-11-13 14:49:11 -08001433 goto bail;
1434 }
1435
1436 rc = 0;
1437
1438bail:
1439 return rc;
1440}
1441
Gang Hed56a8f32016-03-22 14:24:30 -07001442static 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
1504bail:
1505 return rc;
1506}
1507
1508static 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
1579static int
1580ocfs2_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 Beckerb657c952008-11-13 14:49:11 -08001605int 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 Becker8cb471e2009-02-10 20:00:41 -08001611 rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
1612 1, &tmp, flags, ocfs2_validate_inode_block);
Joel Beckerb657c952008-11-13 14:49:11 -08001613
1614 /* If ocfs2_read_blocks() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -08001615 if (!rc && !*bh)
Joel Beckerb657c952008-11-13 14:49:11 -08001616 *bh = tmp;
1617
Joel Beckerb657c952008-11-13 14:49:11 -08001618 return rc;
1619}
1620
1621int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
1622{
1623 return ocfs2_read_inode_block_full(inode, bh, 0);
1624}
Joel Becker6e5a3d72009-02-10 19:00:37 -08001625
Joel Becker6e5a3d72009-02-10 19:00:37 -08001626
1627static 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 Becker8cb471e2009-02-10 20:00:41 -08001634static 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 Becker6e5a3d72009-02-10 19:00:37 -08001641static 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
1648static 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
1655static 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
1662static 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
1669const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
1670 .co_owner = ocfs2_inode_cache_owner,
Joel Becker8cb471e2009-02-10 20:00:41 -08001671 .co_get_super = ocfs2_inode_cache_get_super,
Joel Becker6e5a3d72009-02-10 19:00:37 -08001672 .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