]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ocfs2/inode.c
Merge branch 'etnaviv/fixes' of https://git.pengutronix.de/git/lst/linux into drm...
[mirror_ubuntu-artful-kernel.git] / fs / ocfs2 / inode.c
CommitLineData
ccd979bd
MF
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>
ccd979bd
MF
28#include <linux/highmem.h>
29#include <linux/pagemap.h>
a90714c1 30#include <linux/quotaops.h>
ccd979bd
MF
31
32#include <asm/byteorder.h>
33
ccd979bd
MF
34#include <cluster/masklog.h>
35
36#include "ocfs2.h"
37
38#include "alloc.h"
9b7895ef 39#include "dir.h"
d6b32bbb 40#include "blockcheck.h"
ccd979bd
MF
41#include "dlmglue.h"
42#include "extent_map.h"
43#include "file.h"
b4df6ed8 44#include "heartbeat.h"
ccd979bd
MF
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"
cf1d6c76 53#include "xattr.h"
8b2c0dba 54#include "refcounttree.h"
64f3b269 55#include "ocfs2_trace.h"
d56a8f32 56#include "filecheck.h"
ccd979bd
MF
57
58#include "buffer_head_io.h"
59
ccd979bd
MF
60struct ocfs2_find_inode_args
61{
62 u64 fi_blkno;
63 unsigned long fi_ino;
64 unsigned int fi_flags;
5fa0613e 65 unsigned int fi_sysfile_type;
ccd979bd
MF
66};
67
5fa0613e
JK
68static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
69
ccd979bd
MF
70static 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
d56a8f32
GH
78static 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
ca4d147e
HP
86void 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
6e4b0d56
JK
106/* 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
6ca497a8 125struct 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}
5fa0613e
JK
136struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
137 int sysfile_type)
ccd979bd 138{
8c4d5a43 139 int rc = -ESTALE;
ccd979bd
MF
140 struct inode *inode = NULL;
141 struct super_block *sb = osb->sb;
142 struct ocfs2_find_inode_args args;
2931cdcb 143 journal_t *journal = OCFS2_SB(sb)->journal->j_journal;
ccd979bd 144
64f3b269
TM
145 trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
146 sysfile_type);
ccd979bd
MF
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;
24c19ef4 158 args.fi_flags = flags;
ccd979bd 159 args.fi_ino = ino_from_blkno(sb, blkno);
5fa0613e 160 args.fi_sysfile_type = sysfile_type;
ccd979bd
MF
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. */
ccd979bd
MF
167 if (inode == NULL) {
168 inode = ERR_PTR(-ENOMEM);
169 mlog_errno(PTR_ERR(inode));
170 goto bail;
171 }
64f3b269 172 trace_ocfs2_iget5_locked(inode->i_state);
6218b90e 173 if (inode->i_state & I_NEW) {
d56a8f32 174 rc = ocfs2_read_locked_inode(inode, &args);
6218b90e
TM
175 unlock_new_inode(inode);
176 }
ccd979bd
MF
177 if (is_bad_inode(inode)) {
178 iput(inode);
1f3a437f 179 inode = ERR_PTR(rc);
ccd979bd
MF
180 goto bail;
181 }
182
2931cdcb
DW
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
ccd979bd
MF
209bail:
210 if (!IS_ERR(inode)) {
64f3b269
TM
211 trace_ocfs2_iget_end(inode,
212 (unsigned long long)OCFS2_I(inode)->ip_blkno);
6a1bd4a5 213 }
ccd979bd
MF
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
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
ccd979bd
MF
232 args = opaque;
233
234 mlog_bug_on_msg(!inode, "No inode in find actor!\n");
235
64f3b269
TM
236 trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno);
237
ccd979bd
MF
238 if (oi->ip_blkno != args->fi_blkno)
239 goto bail;
240
ccd979bd
MF
241 ret = 1;
242bail:
ccd979bd
MF
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 */
251static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
252{
253 struct ocfs2_find_inode_args *args = opaque;
cb25797d
JK
254 static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
255 ocfs2_file_ip_alloc_sem_key;
ccd979bd 256
ccd979bd
MF
257 inode->i_ino = args->fi_ino;
258 OCFS2_I(inode)->ip_blkno = args->fi_blkno;
5fa0613e 259 if (args->fi_sysfile_type != 0)
9902af79 260 lockdep_set_class(&inode->i_rwsem,
5fa0613e 261 &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
cb25797d
JK
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);
ccd979bd 271
ccd979bd
MF
272 return 0;
273}
274
b657c95c
JB
275void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
276 int create_ino)
ccd979bd
MF
277{
278 struct super_block *sb;
279 struct ocfs2_super *osb;
53da4939 280 int use_plocks = 1;
ccd979bd 281
ccd979bd
MF
282 sb = inode->i_sb;
283 osb = OCFS2_SB(sb);
284
53da4939
MF
285 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
286 ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
287 use_plocks = 0;
288
b657c95c
JB
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);
ccd979bd 299
ccd979bd 300
8110b073
MF
301 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
302 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
15b1e36b 303 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
8110b073 304
ccd979bd
MF
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);
2c034176
EB
309 i_uid_write(inode, le32_to_cpu(fe->i_uid));
310 i_gid_write(inode, le32_to_cpu(fe->i_gid));
ccd979bd
MF
311
312 /* Fast symlinks will have i_size but no allocated clusters. */
ea022dfb 313 if (S_ISLNK(inode->i_mode) && !fe->i_clusters) {
ccd979bd 314 inode->i_blocks = 0;
ea022dfb
AV
315 inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
316 } else {
8110b073 317 inode->i_blocks = ocfs2_inode_sector_count(inode);
ea022dfb
AV
318 inode->i_mapping->a_ops = &ocfs2_aops;
319 }
ccd979bd
MF
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,
b0697053
MF
329 "ip_blkno %llu != i_blkno %llu!\n",
330 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1ca1a111 331 (unsigned long long)le64_to_cpu(fe->i_blkno));
ccd979bd 332
bfe86848 333 set_nlink(inode, ocfs2_read_links_count(fe));
ccd979bd 334
64f3b269
TM
335 trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno,
336 le32_to_cpu(fe->i_flags));
bbbd0eb3 337 if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
24c19ef4 338 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
bbbd0eb3
JK
339 inode->i_flags |= S_NOQUOTA;
340 }
64f3b269 341
ccd979bd
MF
342 if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
343 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
ccd979bd
MF
344 } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
345 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
1a224ad1
JK
346 } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
347 inode->i_flags |= S_NOQUOTA;
ccd979bd 348 } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
ccd979bd
MF
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:
53da4939
MF
356 if (use_plocks)
357 inode->i_fop = &ocfs2_fops;
358 else
359 inode->i_fop = &ocfs2_fops_no_plocks;
ccd979bd
MF
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;
53da4939
MF
365 if (use_plocks)
366 inode->i_fop = &ocfs2_dops;
367 else
368 inode->i_fop = &ocfs2_dops_no_plocks;
ccd979bd 369 i_size_write(inode, le64_to_cpu(fe->i_size));
5e98d492 370 OCFS2_I(inode)->ip_dir_lock_gen = 1;
ccd979bd
MF
371 break;
372 case S_IFLNK:
ea022dfb 373 inode->i_op = &ocfs2_symlink_inode_operations;
21fc61c7 374 inode_nohighmem(inode);
ccd979bd
MF
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
24c19ef4
MF
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 */
1ca1a111 393 BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
24c19ef4 394
e63aecb6 395 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
24c19ef4 396 OCFS2_LOCK_TYPE_META, 0, inode);
50008630
TY
397
398 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
399 OCFS2_LOCK_TYPE_OPEN, 0, inode);
24c19ef4
MF
400 }
401
ccd979bd 402 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
24c19ef4
MF
403 OCFS2_LOCK_TYPE_RW, inode->i_generation,
404 inode);
405
ca4d147e 406 ocfs2_set_inode_flags(inode);
ca4d147e 407
13821151
TM
408 OCFS2_I(inode)->ip_last_used_slot = 0;
409 OCFS2_I(inode)->ip_last_used_group = 0;
e3b4a97d
MF
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);
ccd979bd
MF
414}
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;
d56a8f32 423 int status, can_lock, lock_level = 0;
24c19ef4 424 u32 generation = 0;
ccd979bd 425
ccd979bd 426 status = -EINVAL;
ccd979bd
MF
427 sb = inode->i_sb;
428 osb = OCFS2_SB(sb);
429
24c19ef4
MF
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
b595076a 454 * concern is user-accessible files anyway.
24c19ef4
MF
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)
50008630 460 && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
c271c5c2 461 && !ocfs2_mount_local(osb);
24c19ef4 462
64f3b269
TM
463 trace_ocfs2_read_locked_inode(
464 (unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock);
465
24c19ef4
MF
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
e63aecb6 476 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
24c19ef4
MF
477 OCFS2_LOCK_TYPE_META,
478 generation, inode);
479
50008630
TY
480 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
481 OCFS2_LOCK_TYPE_OPEN,
482 0, inode);
483
24c19ef4 484 if (can_lock) {
50008630
TY
485 status = ocfs2_open_lock(inode);
486 if (status) {
487 make_bad_inode(inode);
488 mlog_errno(status);
489 return status;
490 }
d56a8f32 491 status = ocfs2_inode_lock(inode, NULL, lock_level);
24c19ef4
MF
492 if (status) {
493 make_bad_inode(inode);
494 mlog_errno(status);
495 return status;
496 }
ccd979bd
MF
497 }
498
50008630
TY
499 if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
500 status = ocfs2_try_open_lock(inode, 0);
501 if (status) {
2bd63216 502 make_bad_inode(inode);
50008630
TY
503 return status;
504 }
505 }
506
b657c95c 507 if (can_lock) {
d56a8f32
GH
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);
b657c95c 517 } else {
da1e9098 518 status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
f5ce5a08
SM
519 /*
520 * If buffer is in jbd, then its checksum may not have been
521 * computed as yet.
522 */
d56a8f32
GH
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 }
b657c95c 534 }
ccd979bd
MF
535 if (status < 0) {
536 mlog_errno(status);
ccd979bd
MF
537 goto bail;
538 }
539
24c19ef4 540 status = -EINVAL;
ccd979bd 541 fe = (struct ocfs2_dinode *) bh->b_data;
ccd979bd 542
24c19ef4
MF
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);
ccd979bd
MF
552
553 if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
554 S_ISBLK(le16_to_cpu(fe->i_mode)))
b657c95c 555 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
ccd979bd 556
b657c95c 557 ocfs2_populate_inode(inode, fe, 0);
ccd979bd
MF
558
559 BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
560
d56a8f32
GH
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
ccd979bd
MF
574 status = 0;
575
576bail:
24c19ef4 577 if (can_lock)
d56a8f32 578 ocfs2_inode_unlock(inode, lock_level);
24c19ef4
MF
579
580 if (status < 0)
581 make_bad_inode(inode);
582
4a635a11 583 brelse(bh);
ccd979bd 584
ccd979bd
MF
585 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;
ccd979bd 598 struct ocfs2_dinode *fe;
60b11392 599 handle_t *handle = NULL;
ccd979bd 600
ccd979bd
MF
601 fe = (struct ocfs2_dinode *) fe_bh->b_data;
602
1afc32b9
MF
603 /*
604 * This check will also skip truncate of inodes with inline
605 * data and fast symlinks.
606 */
3a0782d0 607 if (fe->i_clusters) {
2b4e30fb
JB
608 if (ocfs2_should_order_data(inode))
609 ocfs2_begin_ordered_truncate(inode, 0);
610
60b11392
MF
611 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
612 if (IS_ERR(handle)) {
613 status = PTR_ERR(handle);
0350cb07 614 handle = NULL;
60b11392
MF
615 mlog_errno(status);
616 goto out;
617 }
618
0cf2f763
JB
619 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
620 fe_bh,
13723d00 621 OCFS2_JOURNAL_ACCESS_WRITE);
60b11392
MF
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
78f94673 638 status = ocfs2_commit_truncate(osb, inode, fe_bh);
3a0782d0
MF
639 if (status < 0) {
640 mlog_errno(status);
641 goto out;
642 }
ccd979bd 643 }
ccd979bd 644
60b11392
MF
645out:
646 if (handle)
647 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
648 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;
1fabe148 659 handle_t *handle;
ccd979bd
MF
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) {
62f8b1f0 667 status = -ENOENT;
ccd979bd
MF
668 mlog_errno(status);
669 goto bail;
670 }
671
5955102c 672 inode_lock(inode_alloc_inode);
e63aecb6 673 status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
ccd979bd 674 if (status < 0) {
5955102c 675 inode_unlock(inode_alloc_inode);
ccd979bd
MF
676
677 mlog_errno(status);
678 goto bail;
679 }
680
a90714c1 681 handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
9b7895ef 682 ocfs2_quota_trans_credits(inode->i_sb));
ccd979bd
MF
683 if (IS_ERR(handle)) {
684 status = PTR_ERR(handle);
685 mlog_errno(status);
686 goto bail_unlock;
687 }
688
d4cd1871
LD
689 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
690 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
06ee5c75 691 orphan_dir_bh, false);
d4cd1871
LD
692 if (status < 0) {
693 mlog_errno(status);
694 goto bail_commit;
695 }
ccd979bd
MF
696 }
697
698 /* set the inodes dtime */
0cf2f763 699 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
13723d00 700 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
701 if (status < 0) {
702 mlog_errno(status);
703 goto bail_commit;
704 }
705
c62c38f6 706 di->i_dtime = cpu_to_le64(ktime_get_real_seconds());
4092d49f 707 di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
ec20cec7 708 ocfs2_journal_dirty(handle, di_bh);
ccd979bd 709
8cb471e8 710 ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
63936dda 711 dquot_free_inode(inode);
ccd979bd
MF
712
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:
02dc1af4 719 ocfs2_commit_trans(osb, handle);
ccd979bd 720bail_unlock:
e63aecb6 721 ocfs2_inode_unlock(inode_alloc_inode, 1);
5955102c 722 inode_unlock(inode_alloc_inode);
ccd979bd
MF
723 brelse(inode_alloc_bh);
724bail:
725 iput(inode_alloc_inode);
726
727 return status;
728}
729
2bd63216 730/*
b4df6ed8
MF
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 */
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)) {
b4df6ed8
MF
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]++;
749out:
750 spin_unlock(&osb->osb_lock);
64f3b269 751 trace_ocfs2_check_orphan_recovery_state(slot, ret);
b4df6ed8
MF
752 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
ccd979bd
MF
765static int ocfs2_wipe_inode(struct inode *inode,
766 struct buffer_head *di_bh)
767{
d577632e 768 int status, orphaned_slot = -1;
ccd979bd
MF
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);
d4cd1871 772 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
ccd979bd 773
d4cd1871
LD
774 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
775 orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
b4df6ed8 776
d4cd1871
LD
777 status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
778 if (status)
779 return status;
b4df6ed8 780
d4cd1871
LD
781 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
782 ORPHAN_DIR_SYSTEM_INODE,
783 orphaned_slot);
784 if (!orphan_dir_inode) {
62f8b1f0 785 status = -ENOENT;
d4cd1871
LD
786 mlog_errno(status);
787 goto bail;
788 }
ccd979bd 789
d4cd1871
LD
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. */
5955102c 793 inode_lock(orphan_dir_inode);
d4cd1871
LD
794 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
795 if (status < 0) {
5955102c 796 inode_unlock(orphan_dir_inode);
ccd979bd 797
d4cd1871
LD
798 mlog_errno(status);
799 goto bail;
800 }
ccd979bd
MF
801 }
802
803 /* we do this while holding the orphan dir lock because we
34d024f8
MF
804 * don't want recovery being run from another node to try an
805 * inode delete underneath us -- this will result in two nodes
ccd979bd
MF
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
9b7895ef
MF
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
cf1d6c76
TY
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
8b2c0dba
TM
829 status = ocfs2_remove_refcount_tree(inode, di_bh);
830 if (status < 0) {
831 mlog_errno(status);
832 goto bail_unlock_dir;
833 }
834
ccd979bd
MF
835 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:
d4cd1871
LD
841 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
842 return status;
843
e63aecb6 844 ocfs2_inode_unlock(orphan_dir_inode, 1);
5955102c 845 inode_unlock(orphan_dir_inode);
ccd979bd
MF
846 brelse(orphan_dir_bh);
847bail:
848 iput(orphan_dir_inode);
b4df6ed8 849 ocfs2_signal_wipe_completion(osb, orphaned_slot);
ccd979bd
MF
850
851 return status;
852}
853
854/* There is a series of simple checks that should be done before a
34d024f8 855 * trylock is even considered. Encapsulate those in this function. */
ccd979bd
MF
856static 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
64f3b269
TM
862 trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
863 (unsigned long long)oi->ip_blkno,
864 oi->ip_flags);
865
ccd979bd
MF
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
bd62ad7a
JK
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 */
64f3b269 880 if (current == osb->dc_task)
ccd979bd 881 goto bail;
ccd979bd
MF
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) {
b0697053
MF
888 mlog(ML_ERROR, "Skipping delete of system file %llu\n",
889 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
890 goto bail_unlock;
891 }
892
ccd979bd
MF
893 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{
64f3b269 908 int status = 0, reason = 0;
ccd979bd
MF
909 struct ocfs2_inode_info *oi = OCFS2_I(inode);
910 struct ocfs2_dinode *di;
911
912 *wipe = 0;
913
64f3b269
TM
914 trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
915 inode->i_nlink);
916
ccd979bd
MF
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)) {
64f3b269 921 reason = 1;
ccd979bd
MF
922 goto bail;
923 }
924
925 /* Now that we have an up to date inode, we can double check
926 * the link count. */
64f3b269 927 if (inode->i_nlink)
ccd979bd 928 goto bail;
ccd979bd
MF
929
930 /* Do some basic inode verification... */
931 di = (struct ocfs2_dinode *) di_bh->b_data;
d4cd1871
LD
932 if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
933 !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
b54c2ca4
TY
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)) {
64f3b269 943 reason = 2;
b54c2ca4
TY
944 goto bail;
945 }
946
ccd979bd
MF
947 /* for lack of a better error? */
948 status = -EEXIST;
949 mlog(ML_ERROR,
b0697053 950 "Inode %llu (on-disk %llu) not orphaned! "
ccd979bd 951 "Disk flags 0x%x, inode flags 0x%x\n",
b0697053 952 (unsigned long long)oi->ip_blkno,
1ca1a111
MF
953 (unsigned long long)le64_to_cpu(di->i_blkno),
954 le32_to_cpu(di->i_flags), oi->ip_flags);
ccd979bd
MF
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
6f16bf65
MF
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
25985edc 971 * meta data lock. If the upconvert succeeds, we know the inode
6f16bf65
MF
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);
50008630 978 if (status == -EAGAIN) {
ccd979bd 979 status = 0;
64f3b269 980 reason = 3;
ccd979bd
MF
981 goto bail;
982 }
983 if (status < 0) {
984 mlog_errno(status);
985 goto bail;
986 }
987
50008630 988 *wipe = 1;
64f3b269 989 trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
ccd979bd
MF
990
991bail:
64f3b269 992 trace_ocfs2_query_inode_wipe_end(status, reason);
ccd979bd
MF
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. */
999static void ocfs2_cleanup_delete_inode(struct inode *inode,
1000 int sync_data)
1001{
64f3b269
TM
1002 trace_ocfs2_cleanup_delete_inode(
1003 (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
ccd979bd 1004 if (sync_data)
249ec93c 1005 filemap_write_and_wait(inode->i_mapping);
91b0abe3 1006 truncate_inode_pages_final(&inode->i_data);
ccd979bd
MF
1007}
1008
066d92dc 1009static void ocfs2_delete_inode(struct inode *inode)
ccd979bd
MF
1010{
1011 int wipe, status;
e4b963f1 1012 sigset_t oldset;
ccd979bd 1013 struct buffer_head *di_bh = NULL;
ad694821 1014 struct ocfs2_dinode *di = NULL;
ccd979bd 1015
64f3b269
TM
1016 trace_ocfs2_delete_inode(inode->i_ino,
1017 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1018 is_bad_inode(inode));
ccd979bd 1019
a90714c1
JK
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. */
64f3b269 1023 if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
ccd979bd 1024 goto bail;
ccd979bd
MF
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
bd62ad7a
JK
1034 dquot_initialize(inode);
1035
ccd979bd
MF
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. */
e4b963f1 1040 ocfs2_block_signals(&oldset);
ccd979bd 1041
6ca497a8 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 }
ccd979bd
MF
1053 /* Lock down the inode. This gives us an up to date view of
1054 * it's metadata (for verification), and allows us to
34d024f8 1055 * serialize delete_inode on multiple nodes.
ccd979bd
MF
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 */
e63aecb6 1061 status = ocfs2_inode_lock(inode, &di_bh, 1);
ccd979bd
MF
1062 if (status < 0) {
1063 if (status != -ENOENT)
1064 mlog_errno(status);
1065 ocfs2_cleanup_delete_inode(inode, 0);
6ca497a8 1066 goto bail_unlock_nfs_sync;
ccd979bd
MF
1067 }
1068
ad694821
JQ
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
ccd979bd
MF
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) {
34d024f8 1081 /* Error and remote inode busy both mean we won't be
ccd979bd
MF
1082 * removing the inode, so they take almost the same
1083 * path. */
1084 if (status < 0)
1085 mlog_errno(status);
1086
34d024f8
MF
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. */
ccd979bd
MF
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) {
b4df6ed8
MF
1098 if (status != -EDEADLK)
1099 mlog_errno(status);
ccd979bd
MF
1100 goto bail_unlock_inode;
1101 }
1102
24c19ef4
MF
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 */
ccd979bd
MF
1112 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1113
1114bail_unlock_inode:
e63aecb6 1115 ocfs2_inode_unlock(inode, 1);
ccd979bd 1116 brelse(di_bh);
6ca497a8 1117
1118bail_unlock_nfs_sync:
1119 ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
1120
ccd979bd 1121bail_unblock:
e4b963f1 1122 ocfs2_unblock_signals(&oldset);
ccd979bd 1123bail:
c1e8d35e 1124 return;
ccd979bd
MF
1125}
1126
066d92dc 1127static void ocfs2_clear_inode(struct inode *inode)
ccd979bd
MF
1128{
1129 int status;
1130 struct ocfs2_inode_info *oi = OCFS2_I(inode);
84d86f83 1131 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
ccd979bd 1132
dbd5768f 1133 clear_inode(inode);
64f3b269
TM
1134 trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
1135 inode->i_nlink);
ccd979bd
MF
1136
1137 mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1138 "Inode=%lu\n", inode->i_ino);
1139
9f754758 1140 dquot_drop(inode);
257ba15c 1141
34d024f8
MF
1142 /* To preven remote deletes we hold open lock before, now it
1143 * is time to unlock PR and EX open locks. */
50008630
TY
1144 ocfs2_open_unlock(inode);
1145
ccd979bd 1146 /* Do these before all the other work so that we don't bounce
34d024f8 1147 * the downconvert thread while waiting to destroy the locks. */
84d86f83
JK
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);
ccd979bd 1151
4fe370af
MF
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
ccd979bd
MF
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),
b0697053
MF
1166 "Clear inode of %llu, inode has io markers\n",
1167 (unsigned long long)oi->ip_blkno);
4506cfb6
RD
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);
ccd979bd 1171
83418978
MF
1172 ocfs2_extent_map_trunc(inode, 0);
1173
ccd979bd
MF
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);
e63aecb6 1179 ocfs2_lock_res_free(&oi->ip_inode_lockres);
50008630 1180 ocfs2_lock_res_free(&oi->ip_open_lockres);
ccd979bd 1181
66fb345d 1182 ocfs2_metadata_cache_exit(INODE_CACHE(inode));
ccd979bd 1183
8cb471e8 1184 mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
b0697053 1185 "Clear inode of %llu, inode has %u cache items\n",
8cb471e8
JB
1186 (unsigned long long)oi->ip_blkno,
1187 INODE_CACHE(inode)->ci_num_cached);
ccd979bd 1188
8cb471e8 1189 mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
b0697053
MF
1190 "Clear inode of %llu, inode has a bad flag\n",
1191 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
1192
1193 mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
b0697053
MF
1194 "Clear inode of %llu, inode is locked\n",
1195 (unsigned long long)oi->ip_blkno);
ccd979bd 1196
251b6ecc 1197 mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
b0697053
MF
1198 "Clear inode of %llu, io_mutex is locked\n",
1199 (unsigned long long)oi->ip_blkno);
251b6ecc 1200 mutex_unlock(&oi->ip_io_mutex);
ccd979bd
MF
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),
b0697053
MF
1207 "Clear inode of %llu, alloc_sem is locked\n",
1208 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
1209 up_write(&oi->ip_alloc_sem);
1210
1211 mlog_bug_on_msg(oi->ip_open_count,
b0697053
MF
1212 "Clear inode of %llu has open count %d\n",
1213 (unsigned long long)oi->ip_blkno, oi->ip_open_count);
ccd979bd
MF
1214
1215 /* Clear all other flags. */
47460d65 1216 oi->ip_flags = 0;
ccd979bd
MF
1217 oi->ip_dir_start_lookup = 0;
1218 oi->ip_blkno = 0ULL;
ae0dff68
SM
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 */
2b4e30fb
JB
1225 jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
1226 &oi->ip_jinode);
ccd979bd
MF
1227}
1228
066d92dc
AV
1229void 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 {
91b0abe3 1235 truncate_inode_pages_final(&inode->i_data);
066d92dc
AV
1236 }
1237 ocfs2_clear_inode(inode);
1238}
1239
ccd979bd
MF
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. */
45321ac5 1243int ocfs2_drop_inode(struct inode *inode)
ccd979bd
MF
1244{
1245 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1246
64f3b269
TM
1247 trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno,
1248 inode->i_nlink, oi->ip_flags);
ccd979bd 1249
513e2dae
X
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;
ccd979bd 1257
513e2dae 1258 return 1;
ccd979bd
MF
1259}
1260
ccd979bd
MF
1261/*
1262 * This is called from our getattr.
1263 */
1264int ocfs2_inode_revalidate(struct dentry *dentry)
1265{
2b0143b5 1266 struct inode *inode = d_inode(dentry);
ccd979bd
MF
1267 int status = 0;
1268
64f3b269
TM
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);
ccd979bd
MF
1272
1273 if (!inode) {
ccd979bd
MF
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);
ccd979bd
MF
1281 status = -ENOENT;
1282 goto bail;
1283 }
1284 spin_unlock(&OCFS2_I(inode)->ip_lock);
1285
e63aecb6 1286 /* Let ocfs2_inode_lock do the work of updating our struct
ccd979bd 1287 * inode for us. */
e63aecb6 1288 status = ocfs2_inode_lock(inode, NULL, 0);
ccd979bd
MF
1289 if (status < 0) {
1290 if (status != -ENOENT)
1291 mlog_errno(status);
1292 goto bail;
1293 }
e63aecb6 1294 ocfs2_inode_unlock(inode, 0);
ccd979bd 1295bail:
ccd979bd
MF
1296 return status;
1297}
1298
1299/*
1300 * Updates a disk inode from a
1301 * struct inode.
1302 * Only takes ip_lock.
1303 */
1fabe148 1304int ocfs2_mark_inode_dirty(handle_t *handle,
ccd979bd
MF
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
64f3b269 1311 trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd 1312
0cf2f763 1313 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
13723d00 1314 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
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);
6e4b0d56 1322 ocfs2_get_inode_flags(OCFS2_I(inode));
ca4d147e 1323 fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
15b1e36b 1324 fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
ccd979bd
MF
1325 spin_unlock(&OCFS2_I(inode)->ip_lock);
1326
1327 fe->i_size = cpu_to_le64(i_size_read(inode));
198a1ca3 1328 ocfs2_set_links_count(fe, inode->i_nlink);
2c034176
EB
1329 fe->i_uid = cpu_to_le32(i_uid_read(inode));
1330 fe->i_gid = cpu_to_le32(i_gid_read(inode));
ccd979bd
MF
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
ec20cec7 1339 ocfs2_journal_dirty(handle, bh);
2931cdcb 1340 ocfs2_update_inode_fsync_trans(handle, inode, 1);
ccd979bd 1341leave:
ccd979bd
MF
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 */
1350void ocfs2_refresh_inode(struct inode *inode,
1351 struct ocfs2_dinode *fe)
1352{
ccd979bd
MF
1353 spin_lock(&OCFS2_I(inode)->ip_lock);
1354
1355 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
ca4d147e 1356 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
15b1e36b 1357 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
ca4d147e 1358 ocfs2_set_inode_flags(inode);
ccd979bd 1359 i_size_write(inode, le64_to_cpu(fe->i_size));
bfe86848 1360 set_nlink(inode, ocfs2_read_links_count(fe));
2c034176
EB
1361 i_uid_write(inode, le32_to_cpu(fe->i_uid));
1362 i_gid_write(inode, le32_to_cpu(fe->i_gid));
ccd979bd 1363 inode->i_mode = le16_to_cpu(fe->i_mode);
ccd979bd
MF
1364 if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1365 inode->i_blocks = 0;
1366 else
8110b073 1367 inode->i_blocks = ocfs2_inode_sector_count(inode);
ccd979bd
MF
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}
b657c95c
JB
1377
1378int ocfs2_validate_inode_block(struct super_block *sb,
1379 struct buffer_head *bh)
1380{
d6b32bbb 1381 int rc;
b657c95c
JB
1382 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1383
64f3b269 1384 trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
970e4936 1385
b657c95c
JB
1386 BUG_ON(!buffer_uptodate(bh));
1387
d6b32bbb
JB
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);
13723d00
JB
1394 if (rc) {
1395 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
1396 (unsigned long long)bh->b_blocknr);
d6b32bbb 1397 goto bail;
13723d00 1398 }
d6b32bbb
JB
1399
1400 /*
1401 * Errors after here are fatal.
1402 */
1403
1404 rc = -EINVAL;
1405
b657c95c 1406 if (!OCFS2_IS_VALID_DINODE(di)) {
17a5b9ab 1407 rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
7ecef14a
JP
1408 (unsigned long long)bh->b_blocknr, 7,
1409 di->i_signature);
b657c95c
JB
1410 goto bail;
1411 }
1412
1413 if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
17a5b9ab 1414 rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
7ecef14a
JP
1415 (unsigned long long)bh->b_blocknr,
1416 (unsigned long long)le64_to_cpu(di->i_blkno));
b657c95c
JB
1417 goto bail;
1418 }
1419
1420 if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
17a5b9ab 1421 rc = ocfs2_error(sb,
7ecef14a
JP
1422 "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
1423 (unsigned long long)bh->b_blocknr);
b657c95c
JB
1424 goto bail;
1425 }
1426
1427 if (le32_to_cpu(di->i_fs_generation) !=
1428 OCFS2_SB(sb)->fs_generation) {
17a5b9ab 1429 rc = ocfs2_error(sb,
7ecef14a
JP
1430 "Invalid dinode #%llu: fs_generation is %u\n",
1431 (unsigned long long)bh->b_blocknr,
1432 le32_to_cpu(di->i_fs_generation));
b657c95c
JB
1433 goto bail;
1434 }
1435
1436 rc = 0;
1437
1438bail:
1439 return rc;
1440}
1441
d56a8f32
GH
1442static 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
b657c95c
JB
1605int 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
8cb471e8
JB
1611 rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
1612 1, &tmp, flags, ocfs2_validate_inode_block);
b657c95c
JB
1613
1614 /* If ocfs2_read_blocks() got us a new bh, pass it up. */
970e4936 1615 if (!rc && !*bh)
b657c95c
JB
1616 *bh = tmp;
1617
b657c95c
JB
1618 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}
6e5a3d75 1625
6e5a3d75
JB
1626
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
8cb471e8
JB
1634static 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
6e5a3d75
JB
1641static 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,
8cb471e8 1671 .co_get_super = ocfs2_inode_cache_get_super,
6e5a3d75
JB
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