1328970deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fa60ce2cSMasahiro Yamada /*
3ccd979bdSMark Fasheh * inode.c
4ccd979bdSMark Fasheh *
5ccd979bdSMark Fasheh * vfs' aops, fops, dops and iops
6ccd979bdSMark Fasheh *
7ccd979bdSMark Fasheh * Copyright (C) 2002, 2004 Oracle. All rights reserved.
8ccd979bdSMark Fasheh */
9ccd979bdSMark Fasheh
10ccd979bdSMark Fasheh #include <linux/fs.h>
11ccd979bdSMark Fasheh #include <linux/types.h>
12ccd979bdSMark Fasheh #include <linux/highmem.h>
13ccd979bdSMark Fasheh #include <linux/pagemap.h>
14a90714c1SJan Kara #include <linux/quotaops.h>
15cc56c33eSJeff Layton #include <linux/iversion.h>
16ccd979bdSMark Fasheh
17ccd979bdSMark Fasheh #include <asm/byteorder.h>
18ccd979bdSMark Fasheh
19ccd979bdSMark Fasheh #include <cluster/masklog.h>
20ccd979bdSMark Fasheh
21ccd979bdSMark Fasheh #include "ocfs2.h"
22ccd979bdSMark Fasheh
23ccd979bdSMark Fasheh #include "alloc.h"
249b7895efSMark Fasheh #include "dir.h"
25d6b32bbbSJoel Becker #include "blockcheck.h"
26ccd979bdSMark Fasheh #include "dlmglue.h"
27ccd979bdSMark Fasheh #include "extent_map.h"
28ccd979bdSMark Fasheh #include "file.h"
29b4df6ed8SMark Fasheh #include "heartbeat.h"
30ccd979bdSMark Fasheh #include "inode.h"
31ccd979bdSMark Fasheh #include "journal.h"
32ccd979bdSMark Fasheh #include "namei.h"
33ccd979bdSMark Fasheh #include "suballoc.h"
34ccd979bdSMark Fasheh #include "super.h"
35ccd979bdSMark Fasheh #include "symlink.h"
36ccd979bdSMark Fasheh #include "sysfile.h"
37ccd979bdSMark Fasheh #include "uptodate.h"
38cf1d6c76STiger Yang #include "xattr.h"
398b2c0dbaSTao Ma #include "refcounttree.h"
4064f3b269STao Ma #include "ocfs2_trace.h"
41d56a8f32SGang He #include "filecheck.h"
42ccd979bdSMark Fasheh
43ccd979bdSMark Fasheh #include "buffer_head_io.h"
44ccd979bdSMark Fasheh
45ccd979bdSMark Fasheh struct ocfs2_find_inode_args
46ccd979bdSMark Fasheh {
47ccd979bdSMark Fasheh u64 fi_blkno;
48ccd979bdSMark Fasheh unsigned long fi_ino;
49ccd979bdSMark Fasheh unsigned int fi_flags;
505fa0613eSJan Kara unsigned int fi_sysfile_type;
51ccd979bdSMark Fasheh };
52ccd979bdSMark Fasheh
535fa0613eSJan Kara static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
545fa0613eSJan Kara
55ccd979bdSMark Fasheh static int ocfs2_read_locked_inode(struct inode *inode,
56ccd979bdSMark Fasheh struct ocfs2_find_inode_args *args);
57ccd979bdSMark Fasheh static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
58ccd979bdSMark Fasheh static int ocfs2_find_actor(struct inode *inode, void *opaque);
59ccd979bdSMark Fasheh static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
60ccd979bdSMark Fasheh struct inode *inode,
61ccd979bdSMark Fasheh struct buffer_head *fe_bh);
62ccd979bdSMark Fasheh
63d56a8f32SGang He static int ocfs2_filecheck_read_inode_block_full(struct inode *inode,
64d56a8f32SGang He struct buffer_head **bh,
65d56a8f32SGang He int flags, int type);
66d56a8f32SGang He static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
67d56a8f32SGang He struct buffer_head *bh);
68d56a8f32SGang He static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
69d56a8f32SGang He struct buffer_head *bh);
70d56a8f32SGang He
ocfs2_set_inode_flags(struct inode * inode)71ca4d147eSHerbert Poetzl void ocfs2_set_inode_flags(struct inode *inode)
72ca4d147eSHerbert Poetzl {
73ca4d147eSHerbert Poetzl unsigned int flags = OCFS2_I(inode)->ip_attr;
74ca4d147eSHerbert Poetzl
75ca4d147eSHerbert Poetzl inode->i_flags &= ~(S_IMMUTABLE |
76ca4d147eSHerbert Poetzl S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
77ca4d147eSHerbert Poetzl
78ca4d147eSHerbert Poetzl if (flags & OCFS2_IMMUTABLE_FL)
79ca4d147eSHerbert Poetzl inode->i_flags |= S_IMMUTABLE;
80ca4d147eSHerbert Poetzl
81ca4d147eSHerbert Poetzl if (flags & OCFS2_SYNC_FL)
82ca4d147eSHerbert Poetzl inode->i_flags |= S_SYNC;
83ca4d147eSHerbert Poetzl if (flags & OCFS2_APPEND_FL)
84ca4d147eSHerbert Poetzl inode->i_flags |= S_APPEND;
85ca4d147eSHerbert Poetzl if (flags & OCFS2_NOATIME_FL)
86ca4d147eSHerbert Poetzl inode->i_flags |= S_NOATIME;
87ca4d147eSHerbert Poetzl if (flags & OCFS2_DIRSYNC_FL)
88ca4d147eSHerbert Poetzl inode->i_flags |= S_DIRSYNC;
89ca4d147eSHerbert Poetzl }
90ca4d147eSHerbert Poetzl
916e4b0d56SJan Kara /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
ocfs2_get_inode_flags(struct ocfs2_inode_info * oi)926e4b0d56SJan Kara void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
936e4b0d56SJan Kara {
946e4b0d56SJan Kara unsigned int flags = oi->vfs_inode.i_flags;
956e4b0d56SJan Kara
966e4b0d56SJan Kara oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
976e4b0d56SJan Kara OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
986e4b0d56SJan Kara if (flags & S_SYNC)
996e4b0d56SJan Kara oi->ip_attr |= OCFS2_SYNC_FL;
1006e4b0d56SJan Kara if (flags & S_APPEND)
1016e4b0d56SJan Kara oi->ip_attr |= OCFS2_APPEND_FL;
1026e4b0d56SJan Kara if (flags & S_IMMUTABLE)
1036e4b0d56SJan Kara oi->ip_attr |= OCFS2_IMMUTABLE_FL;
1046e4b0d56SJan Kara if (flags & S_NOATIME)
1056e4b0d56SJan Kara oi->ip_attr |= OCFS2_NOATIME_FL;
1066e4b0d56SJan Kara if (flags & S_DIRSYNC)
1076e4b0d56SJan Kara oi->ip_attr |= OCFS2_DIRSYNC_FL;
1086e4b0d56SJan Kara }
1096e4b0d56SJan Kara
ocfs2_ilookup(struct super_block * sb,u64 blkno)1106ca497a8Swengang wang struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno)
1116ca497a8Swengang wang {
1126ca497a8Swengang wang struct ocfs2_find_inode_args args;
1136ca497a8Swengang wang
1146ca497a8Swengang wang args.fi_blkno = blkno;
1156ca497a8Swengang wang args.fi_flags = 0;
1166ca497a8Swengang wang args.fi_ino = ino_from_blkno(sb, blkno);
1176ca497a8Swengang wang args.fi_sysfile_type = 0;
1186ca497a8Swengang wang
1196ca497a8Swengang wang return ilookup5(sb, blkno, ocfs2_find_actor, &args);
1206ca497a8Swengang wang }
ocfs2_iget(struct ocfs2_super * osb,u64 blkno,unsigned flags,int sysfile_type)1215fa0613eSJan Kara struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
1225fa0613eSJan Kara int sysfile_type)
123ccd979bdSMark Fasheh {
1248c4d5a43SGang He int rc = -ESTALE;
125ccd979bdSMark Fasheh struct inode *inode = NULL;
126ccd979bdSMark Fasheh struct super_block *sb = osb->sb;
127ccd979bdSMark Fasheh struct ocfs2_find_inode_args args;
128bb20b31dSHeming Zhao via Ocfs2-devel journal_t *journal = osb->journal->j_journal;
129ccd979bdSMark Fasheh
13064f3b269STao Ma trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
13164f3b269STao Ma sysfile_type);
132ccd979bdSMark Fasheh
133ccd979bdSMark Fasheh /* Ok. By now we've either got the offsets passed to us by the
134ccd979bdSMark Fasheh * caller, or we just pulled them off the bh. Lets do some
135ccd979bdSMark Fasheh * sanity checks to make sure they're OK. */
136ccd979bdSMark Fasheh if (blkno == 0) {
137ccd979bdSMark Fasheh inode = ERR_PTR(-EINVAL);
138ccd979bdSMark Fasheh mlog_errno(PTR_ERR(inode));
139ccd979bdSMark Fasheh goto bail;
140ccd979bdSMark Fasheh }
141ccd979bdSMark Fasheh
142ccd979bdSMark Fasheh args.fi_blkno = blkno;
14324c19ef4SMark Fasheh args.fi_flags = flags;
144ccd979bdSMark Fasheh args.fi_ino = ino_from_blkno(sb, blkno);
1455fa0613eSJan Kara args.fi_sysfile_type = sysfile_type;
146ccd979bdSMark Fasheh
147ccd979bdSMark Fasheh inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
148ccd979bdSMark Fasheh ocfs2_init_locked_inode, &args);
149ccd979bdSMark Fasheh /* inode was *not* in the inode cache. 2.6.x requires
150ccd979bdSMark Fasheh * us to do our own read_inode call and unlock it
151ccd979bdSMark Fasheh * afterwards. */
152ccd979bdSMark Fasheh if (inode == NULL) {
153ccd979bdSMark Fasheh inode = ERR_PTR(-ENOMEM);
154ccd979bdSMark Fasheh mlog_errno(PTR_ERR(inode));
155ccd979bdSMark Fasheh goto bail;
156ccd979bdSMark Fasheh }
15764f3b269STao Ma trace_ocfs2_iget5_locked(inode->i_state);
1586218b90eSTao Ma if (inode->i_state & I_NEW) {
159d56a8f32SGang He rc = ocfs2_read_locked_inode(inode, &args);
1606218b90eSTao Ma unlock_new_inode(inode);
1616218b90eSTao Ma }
162ccd979bdSMark Fasheh if (is_bad_inode(inode)) {
163ccd979bdSMark Fasheh iput(inode);
164d56a8f32SGang He inode = ERR_PTR(rc);
165ccd979bdSMark Fasheh goto bail;
166ccd979bdSMark Fasheh }
167ccd979bdSMark Fasheh
1682931cdcbSDarrick J. Wong /*
1692931cdcbSDarrick J. Wong * Set transaction id's of transactions that have to be committed
1702931cdcbSDarrick J. Wong * to finish f[data]sync. We set them to currently running transaction
1712931cdcbSDarrick J. Wong * as we cannot be sure that the inode or some of its metadata isn't
1722931cdcbSDarrick J. Wong * part of the transaction - the inode could have been reclaimed and
1732931cdcbSDarrick J. Wong * now it is reread from disk.
1742931cdcbSDarrick J. Wong */
175bb20b31dSHeming Zhao via Ocfs2-devel if (journal) {
1762931cdcbSDarrick J. Wong transaction_t *transaction;
1772931cdcbSDarrick J. Wong tid_t tid;
1782931cdcbSDarrick J. Wong struct ocfs2_inode_info *oi = OCFS2_I(inode);
1792931cdcbSDarrick J. Wong
1802931cdcbSDarrick J. Wong read_lock(&journal->j_state_lock);
1812931cdcbSDarrick J. Wong if (journal->j_running_transaction)
1822931cdcbSDarrick J. Wong transaction = journal->j_running_transaction;
1832931cdcbSDarrick J. Wong else
1842931cdcbSDarrick J. Wong transaction = journal->j_committing_transaction;
1852931cdcbSDarrick J. Wong if (transaction)
1862931cdcbSDarrick J. Wong tid = transaction->t_tid;
1872931cdcbSDarrick J. Wong else
1882931cdcbSDarrick J. Wong tid = journal->j_commit_sequence;
1892931cdcbSDarrick J. Wong read_unlock(&journal->j_state_lock);
1902931cdcbSDarrick J. Wong oi->i_sync_tid = tid;
1912931cdcbSDarrick J. Wong oi->i_datasync_tid = tid;
1922931cdcbSDarrick J. Wong }
1932931cdcbSDarrick J. Wong
194ccd979bdSMark Fasheh bail:
195ccd979bdSMark Fasheh if (!IS_ERR(inode)) {
19664f3b269STao Ma trace_ocfs2_iget_end(inode,
197b0697053SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno);
1986a1bd4a5SMark Fasheh }
199ccd979bdSMark Fasheh
200ccd979bdSMark Fasheh return inode;
201ccd979bdSMark Fasheh }
202ccd979bdSMark Fasheh
203ccd979bdSMark Fasheh
204ccd979bdSMark Fasheh /*
205ccd979bdSMark Fasheh * here's how inodes get read from disk:
206ccd979bdSMark Fasheh * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
207ccd979bdSMark Fasheh * found? : return the in-memory inode
208ccd979bdSMark Fasheh * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
209ccd979bdSMark Fasheh */
210ccd979bdSMark Fasheh
ocfs2_find_actor(struct inode * inode,void * opaque)211ccd979bdSMark Fasheh static int ocfs2_find_actor(struct inode *inode, void *opaque)
212ccd979bdSMark Fasheh {
213ccd979bdSMark Fasheh struct ocfs2_find_inode_args *args = NULL;
214ccd979bdSMark Fasheh struct ocfs2_inode_info *oi = OCFS2_I(inode);
215ccd979bdSMark Fasheh int ret = 0;
216ccd979bdSMark Fasheh
217ccd979bdSMark Fasheh args = opaque;
218ccd979bdSMark Fasheh
219ccd979bdSMark Fasheh mlog_bug_on_msg(!inode, "No inode in find actor!\n");
220ccd979bdSMark Fasheh
22164f3b269STao Ma trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno);
22264f3b269STao Ma
223ccd979bdSMark Fasheh if (oi->ip_blkno != args->fi_blkno)
224ccd979bdSMark Fasheh goto bail;
225ccd979bdSMark Fasheh
226ccd979bdSMark Fasheh ret = 1;
227ccd979bdSMark Fasheh bail:
228ccd979bdSMark Fasheh return ret;
229ccd979bdSMark Fasheh }
230ccd979bdSMark Fasheh
231ccd979bdSMark Fasheh /*
232ccd979bdSMark Fasheh * initialize the new inode, but don't do anything that would cause
233ccd979bdSMark Fasheh * us to sleep.
234ccd979bdSMark Fasheh * return 0 on success, 1 on failure
235ccd979bdSMark Fasheh */
ocfs2_init_locked_inode(struct inode * inode,void * opaque)236ccd979bdSMark Fasheh static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
237ccd979bdSMark Fasheh {
238ccd979bdSMark Fasheh struct ocfs2_find_inode_args *args = opaque;
239cb25797dSJan Kara static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
240cb25797dSJan Kara ocfs2_file_ip_alloc_sem_key;
241ccd979bdSMark Fasheh
242ccd979bdSMark Fasheh inode->i_ino = args->fi_ino;
243ccd979bdSMark Fasheh OCFS2_I(inode)->ip_blkno = args->fi_blkno;
2445fa0613eSJan Kara if (args->fi_sysfile_type != 0)
2459902af79SAl Viro lockdep_set_class(&inode->i_rwsem,
2465fa0613eSJan Kara &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
247cb25797dSJan Kara if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE ||
248cb25797dSJan Kara args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE ||
249cb25797dSJan Kara args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
250cb25797dSJan Kara args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE)
251cb25797dSJan Kara lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
252cb25797dSJan Kara &ocfs2_quota_ip_alloc_sem_key);
253cb25797dSJan Kara else
254cb25797dSJan Kara lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
255cb25797dSJan Kara &ocfs2_file_ip_alloc_sem_key);
256ccd979bdSMark Fasheh
257ccd979bdSMark Fasheh return 0;
258ccd979bdSMark Fasheh }
259ccd979bdSMark Fasheh
ocfs2_populate_inode(struct inode * inode,struct ocfs2_dinode * fe,int create_ino)260b657c95cSJoel Becker void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
261ccd979bdSMark Fasheh int create_ino)
262ccd979bdSMark Fasheh {
263ccd979bdSMark Fasheh struct super_block *sb;
264ccd979bdSMark Fasheh struct ocfs2_super *osb;
26553da4939SMark Fasheh int use_plocks = 1;
266ccd979bdSMark Fasheh
267ccd979bdSMark Fasheh sb = inode->i_sb;
268ccd979bdSMark Fasheh osb = OCFS2_SB(sb);
269ccd979bdSMark Fasheh
27053da4939SMark Fasheh if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
27153da4939SMark Fasheh ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
27253da4939SMark Fasheh use_plocks = 0;
27353da4939SMark Fasheh
274b657c95cSJoel Becker /*
275b657c95cSJoel Becker * These have all been checked by ocfs2_read_inode_block() or set
276b657c95cSJoel Becker * by ocfs2_mknod_locked(), so a failure is a code bug.
277b657c95cSJoel Becker */
278b657c95cSJoel Becker BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode
279b657c95cSJoel Becker cannot create a superblock
280b657c95cSJoel Becker inode today. change if
281b657c95cSJoel Becker that is needed. */
282b657c95cSJoel Becker BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
283b657c95cSJoel Becker BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
284ccd979bdSMark Fasheh
285ccd979bdSMark Fasheh
2868110b073SMark Fasheh OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
2878110b073SMark Fasheh OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
28815b1e36bSMark Fasheh OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
2898110b073SMark Fasheh
290cc56c33eSJeff Layton inode_set_iversion(inode, 1);
291ccd979bdSMark Fasheh inode->i_generation = le32_to_cpu(fe->i_generation);
292ccd979bdSMark Fasheh inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
293ccd979bdSMark Fasheh inode->i_mode = le16_to_cpu(fe->i_mode);
2942c034176SEric W. Biederman i_uid_write(inode, le32_to_cpu(fe->i_uid));
2952c034176SEric W. Biederman i_gid_write(inode, le32_to_cpu(fe->i_gid));
296ccd979bdSMark Fasheh
297ccd979bdSMark Fasheh /* Fast symlinks will have i_size but no allocated clusters. */
298ea022dfbSAl Viro if (S_ISLNK(inode->i_mode) && !fe->i_clusters) {
299ccd979bdSMark Fasheh inode->i_blocks = 0;
300ea022dfbSAl Viro inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
301ea022dfbSAl Viro } else {
3028110b073SMark Fasheh inode->i_blocks = ocfs2_inode_sector_count(inode);
303ccd979bdSMark Fasheh inode->i_mapping->a_ops = &ocfs2_aops;
304ea022dfbSAl Viro }
305*10fc3a18SJeff Layton inode_set_atime(inode, le64_to_cpu(fe->i_atime),
306*10fc3a18SJeff Layton le32_to_cpu(fe->i_atime_nsec));
307*10fc3a18SJeff Layton inode_set_mtime(inode, le64_to_cpu(fe->i_mtime),
308*10fc3a18SJeff Layton le32_to_cpu(fe->i_mtime_nsec));
3096861de97SJeff Layton inode_set_ctime(inode, le64_to_cpu(fe->i_ctime),
3106861de97SJeff Layton le32_to_cpu(fe->i_ctime_nsec));
311ccd979bdSMark Fasheh
312ccd979bdSMark Fasheh if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
313ccd979bdSMark Fasheh mlog(ML_ERROR,
314b0697053SMark Fasheh "ip_blkno %llu != i_blkno %llu!\n",
315b0697053SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno,
3161ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_blkno));
317ccd979bdSMark Fasheh
318bfe86848SMiklos Szeredi set_nlink(inode, ocfs2_read_links_count(fe));
319ccd979bdSMark Fasheh
32064f3b269STao Ma trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno,
32164f3b269STao Ma le32_to_cpu(fe->i_flags));
322bbbd0eb3SJan Kara if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
32324c19ef4SMark Fasheh OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
324bbbd0eb3SJan Kara inode->i_flags |= S_NOQUOTA;
325bbbd0eb3SJan Kara }
32624c19ef4SMark Fasheh
327ccd979bdSMark Fasheh if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
328ccd979bdSMark Fasheh OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
329ccd979bdSMark Fasheh } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
330ccd979bdSMark Fasheh OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
3311a224ad1SJan Kara } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
3321a224ad1SJan Kara inode->i_flags |= S_NOQUOTA;
333ccd979bdSMark Fasheh } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
334ccd979bdSMark Fasheh /* we can't actually hit this as read_inode can't
335ccd979bdSMark Fasheh * handle superblocks today ;-) */
336ccd979bdSMark Fasheh BUG();
337ccd979bdSMark Fasheh }
338ccd979bdSMark Fasheh
339ccd979bdSMark Fasheh switch (inode->i_mode & S_IFMT) {
340ccd979bdSMark Fasheh case S_IFREG:
34153da4939SMark Fasheh if (use_plocks)
342ccd979bdSMark Fasheh inode->i_fop = &ocfs2_fops;
34353da4939SMark Fasheh else
34453da4939SMark Fasheh inode->i_fop = &ocfs2_fops_no_plocks;
345ccd979bdSMark Fasheh inode->i_op = &ocfs2_file_iops;
346ccd979bdSMark Fasheh i_size_write(inode, le64_to_cpu(fe->i_size));
347ccd979bdSMark Fasheh break;
348ccd979bdSMark Fasheh case S_IFDIR:
349ccd979bdSMark Fasheh inode->i_op = &ocfs2_dir_iops;
35053da4939SMark Fasheh if (use_plocks)
351ccd979bdSMark Fasheh inode->i_fop = &ocfs2_dops;
35253da4939SMark Fasheh else
35353da4939SMark Fasheh inode->i_fop = &ocfs2_dops_no_plocks;
354ccd979bdSMark Fasheh i_size_write(inode, le64_to_cpu(fe->i_size));
3555e98d492SGoldwyn Rodrigues OCFS2_I(inode)->ip_dir_lock_gen = 1;
356ccd979bdSMark Fasheh break;
357ccd979bdSMark Fasheh case S_IFLNK:
358ccd979bdSMark Fasheh inode->i_op = &ocfs2_symlink_inode_operations;
35921fc61c7SAl Viro inode_nohighmem(inode);
360ccd979bdSMark Fasheh i_size_write(inode, le64_to_cpu(fe->i_size));
361ccd979bdSMark Fasheh break;
362ccd979bdSMark Fasheh default:
363ccd979bdSMark Fasheh inode->i_op = &ocfs2_special_file_iops;
364ccd979bdSMark Fasheh init_special_inode(inode, inode->i_mode,
365ccd979bdSMark Fasheh inode->i_rdev);
366ccd979bdSMark Fasheh break;
367ccd979bdSMark Fasheh }
368ccd979bdSMark Fasheh
36924c19ef4SMark Fasheh if (create_ino) {
37024c19ef4SMark Fasheh inode->i_ino = ino_from_blkno(inode->i_sb,
37124c19ef4SMark Fasheh le64_to_cpu(fe->i_blkno));
37224c19ef4SMark Fasheh
37324c19ef4SMark Fasheh /*
37424c19ef4SMark Fasheh * If we ever want to create system files from kernel,
37524c19ef4SMark Fasheh * the generation argument to
37624c19ef4SMark Fasheh * ocfs2_inode_lock_res_init() will have to change.
37724c19ef4SMark Fasheh */
3781ca1a111SMark Fasheh BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
37924c19ef4SMark Fasheh
380e63aecb6SMark Fasheh ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
38124c19ef4SMark Fasheh OCFS2_LOCK_TYPE_META, 0, inode);
38250008630STiger Yang
38350008630STiger Yang ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
38450008630STiger Yang OCFS2_LOCK_TYPE_OPEN, 0, inode);
38524c19ef4SMark Fasheh }
38624c19ef4SMark Fasheh
38724c19ef4SMark Fasheh ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
38824c19ef4SMark Fasheh OCFS2_LOCK_TYPE_RW, inode->i_generation,
38924c19ef4SMark Fasheh inode);
39024c19ef4SMark Fasheh
391ca4d147eSHerbert Poetzl ocfs2_set_inode_flags(inode);
392ca4d147eSHerbert Poetzl
39313821151STao Ma OCFS2_I(inode)->ip_last_used_slot = 0;
39413821151STao Ma OCFS2_I(inode)->ip_last_used_group = 0;
395e3b4a97dSMark Fasheh
396e3b4a97dSMark Fasheh if (S_ISDIR(inode->i_mode))
397e3b4a97dSMark Fasheh ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv,
398e3b4a97dSMark Fasheh OCFS2_RESV_FLAG_DIR);
399ccd979bdSMark Fasheh }
400ccd979bdSMark Fasheh
ocfs2_read_locked_inode(struct inode * inode,struct ocfs2_find_inode_args * args)401ccd979bdSMark Fasheh static int ocfs2_read_locked_inode(struct inode *inode,
402ccd979bdSMark Fasheh struct ocfs2_find_inode_args *args)
403ccd979bdSMark Fasheh {
404ccd979bdSMark Fasheh struct super_block *sb;
405ccd979bdSMark Fasheh struct ocfs2_super *osb;
406ccd979bdSMark Fasheh struct ocfs2_dinode *fe;
407ccd979bdSMark Fasheh struct buffer_head *bh = NULL;
408d56a8f32SGang He int status, can_lock, lock_level = 0;
40924c19ef4SMark Fasheh u32 generation = 0;
410ccd979bdSMark Fasheh
411ccd979bdSMark Fasheh status = -EINVAL;
412ccd979bdSMark Fasheh sb = inode->i_sb;
413ccd979bdSMark Fasheh osb = OCFS2_SB(sb);
414ccd979bdSMark Fasheh
41524c19ef4SMark Fasheh /*
41624c19ef4SMark Fasheh * To improve performance of cold-cache inode stats, we take
41724c19ef4SMark Fasheh * the cluster lock here if possible.
41824c19ef4SMark Fasheh *
41924c19ef4SMark Fasheh * Generally, OCFS2 never trusts the contents of an inode
42024c19ef4SMark Fasheh * unless it's holding a cluster lock, so taking it here isn't
42124c19ef4SMark Fasheh * a correctness issue as much as it is a performance
42224c19ef4SMark Fasheh * improvement.
42324c19ef4SMark Fasheh *
42424c19ef4SMark Fasheh * There are three times when taking the lock is not a good idea:
42524c19ef4SMark Fasheh *
42624c19ef4SMark Fasheh * 1) During startup, before we have initialized the DLM.
42724c19ef4SMark Fasheh *
42824c19ef4SMark Fasheh * 2) If we are reading certain system files which never get
42924c19ef4SMark Fasheh * cluster locks (local alloc, truncate log).
43024c19ef4SMark Fasheh *
43124c19ef4SMark Fasheh * 3) If the process doing the iget() is responsible for
43224c19ef4SMark Fasheh * orphan dir recovery. We're holding the orphan dir lock and
43324c19ef4SMark Fasheh * can get into a deadlock with another process on another
43424c19ef4SMark Fasheh * node in ->delete_inode().
43524c19ef4SMark Fasheh *
43624c19ef4SMark Fasheh * #1 and #2 can be simply solved by never taking the lock
43724c19ef4SMark Fasheh * here for system files (which are the only type we read
43824c19ef4SMark Fasheh * during mount). It's a heavier approach, but our main
439b595076aSUwe Kleine-König * concern is user-accessible files anyway.
44024c19ef4SMark Fasheh *
44124c19ef4SMark Fasheh * #3 works itself out because we'll eventually take the
44224c19ef4SMark Fasheh * cluster lock before trusting anything anyway.
44324c19ef4SMark Fasheh */
44424c19ef4SMark Fasheh can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
44550008630STiger Yang && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
446c271c5c2SSunil Mushran && !ocfs2_mount_local(osb);
44724c19ef4SMark Fasheh
44864f3b269STao Ma trace_ocfs2_read_locked_inode(
44964f3b269STao Ma (unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock);
45064f3b269STao Ma
45124c19ef4SMark Fasheh /*
45224c19ef4SMark Fasheh * To maintain backwards compatibility with older versions of
45324c19ef4SMark Fasheh * ocfs2-tools, we still store the generation value for system
45424c19ef4SMark Fasheh * files. The only ones that actually matter to userspace are
45524c19ef4SMark Fasheh * the journals, but it's easier and inexpensive to just flag
45624c19ef4SMark Fasheh * all system files similarly.
45724c19ef4SMark Fasheh */
45824c19ef4SMark Fasheh if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
45924c19ef4SMark Fasheh generation = osb->fs_generation;
46024c19ef4SMark Fasheh
461e63aecb6SMark Fasheh ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
46224c19ef4SMark Fasheh OCFS2_LOCK_TYPE_META,
46324c19ef4SMark Fasheh generation, inode);
46424c19ef4SMark Fasheh
46550008630STiger Yang ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
46650008630STiger Yang OCFS2_LOCK_TYPE_OPEN,
46750008630STiger Yang 0, inode);
46850008630STiger Yang
46924c19ef4SMark Fasheh if (can_lock) {
47050008630STiger Yang status = ocfs2_open_lock(inode);
47150008630STiger Yang if (status) {
47250008630STiger Yang make_bad_inode(inode);
47350008630STiger Yang mlog_errno(status);
47450008630STiger Yang return status;
47550008630STiger Yang }
476d56a8f32SGang He status = ocfs2_inode_lock(inode, NULL, lock_level);
47724c19ef4SMark Fasheh if (status) {
47824c19ef4SMark Fasheh make_bad_inode(inode);
47924c19ef4SMark Fasheh mlog_errno(status);
48024c19ef4SMark Fasheh return status;
48124c19ef4SMark Fasheh }
48224c19ef4SMark Fasheh }
48324c19ef4SMark Fasheh
48450008630STiger Yang if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
48550008630STiger Yang status = ocfs2_try_open_lock(inode, 0);
48650008630STiger Yang if (status) {
48750008630STiger Yang make_bad_inode(inode);
48850008630STiger Yang return status;
48950008630STiger Yang }
49050008630STiger Yang }
49150008630STiger Yang
492b657c95cSJoel Becker if (can_lock) {
493d56a8f32SGang He if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK)
494d56a8f32SGang He status = ocfs2_filecheck_read_inode_block_full(inode,
495d56a8f32SGang He &bh, OCFS2_BH_IGNORE_CACHE, 0);
496d56a8f32SGang He else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX)
497d56a8f32SGang He status = ocfs2_filecheck_read_inode_block_full(inode,
498d56a8f32SGang He &bh, OCFS2_BH_IGNORE_CACHE, 1);
499d56a8f32SGang He else
500d56a8f32SGang He status = ocfs2_read_inode_block_full(inode,
501d56a8f32SGang He &bh, OCFS2_BH_IGNORE_CACHE);
502b657c95cSJoel Becker } else {
503da1e9098SJoel Becker status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
504f5ce5a08SSunil Mushran /*
505f5ce5a08SSunil Mushran * If buffer is in jbd, then its checksum may not have been
506f5ce5a08SSunil Mushran * computed as yet.
507f5ce5a08SSunil Mushran */
508d56a8f32SGang He if (!status && !buffer_jbd(bh)) {
509d56a8f32SGang He if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK)
510d56a8f32SGang He status = ocfs2_filecheck_validate_inode_block(
511d56a8f32SGang He osb->sb, bh);
512d56a8f32SGang He else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX)
513d56a8f32SGang He status = ocfs2_filecheck_repair_inode_block(
514d56a8f32SGang He osb->sb, bh);
515d56a8f32SGang He else
516d56a8f32SGang He status = ocfs2_validate_inode_block(
517d56a8f32SGang He osb->sb, bh);
518d56a8f32SGang He }
519b657c95cSJoel Becker }
520ccd979bdSMark Fasheh if (status < 0) {
521ccd979bdSMark Fasheh mlog_errno(status);
522ccd979bdSMark Fasheh goto bail;
523ccd979bdSMark Fasheh }
524ccd979bdSMark Fasheh
52524c19ef4SMark Fasheh status = -EINVAL;
526ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) bh->b_data;
527ccd979bdSMark Fasheh
52824c19ef4SMark Fasheh /*
52924c19ef4SMark Fasheh * This is a code bug. Right now the caller needs to
53024c19ef4SMark Fasheh * understand whether it is asking for a system file inode or
53124c19ef4SMark Fasheh * not so the proper lock names can be built.
53224c19ef4SMark Fasheh */
53324c19ef4SMark Fasheh mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
53424c19ef4SMark Fasheh !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
5351c3ce541SColin Ian King "Inode %llu: system file state is ambiguous\n",
53624c19ef4SMark Fasheh (unsigned long long)args->fi_blkno);
537ccd979bdSMark Fasheh
538ccd979bdSMark Fasheh if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
539ccd979bdSMark Fasheh S_ISBLK(le16_to_cpu(fe->i_mode)))
540ccd979bdSMark Fasheh inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
541ccd979bdSMark Fasheh
542b657c95cSJoel Becker ocfs2_populate_inode(inode, fe, 0);
543ccd979bdSMark Fasheh
544ccd979bdSMark Fasheh BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
545ccd979bdSMark Fasheh
546d56a8f32SGang He if (buffer_dirty(bh) && !buffer_jbd(bh)) {
547d56a8f32SGang He if (can_lock) {
548d56a8f32SGang He ocfs2_inode_unlock(inode, lock_level);
549d56a8f32SGang He lock_level = 1;
550d56a8f32SGang He ocfs2_inode_lock(inode, NULL, lock_level);
551d56a8f32SGang He }
552d56a8f32SGang He status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
553d56a8f32SGang He if (status < 0) {
554d56a8f32SGang He mlog_errno(status);
555d56a8f32SGang He goto bail;
556d56a8f32SGang He }
557d56a8f32SGang He }
558d56a8f32SGang He
559ccd979bdSMark Fasheh status = 0;
560ccd979bdSMark Fasheh
561ccd979bdSMark Fasheh bail:
56224c19ef4SMark Fasheh if (can_lock)
563d56a8f32SGang He ocfs2_inode_unlock(inode, lock_level);
56424c19ef4SMark Fasheh
56524c19ef4SMark Fasheh if (status < 0)
56624c19ef4SMark Fasheh make_bad_inode(inode);
56724c19ef4SMark Fasheh
568ccd979bdSMark Fasheh brelse(bh);
569ccd979bdSMark Fasheh
570ccd979bdSMark Fasheh return status;
571ccd979bdSMark Fasheh }
572ccd979bdSMark Fasheh
ocfs2_sync_blockdev(struct super_block * sb)573ccd979bdSMark Fasheh void ocfs2_sync_blockdev(struct super_block *sb)
574ccd979bdSMark Fasheh {
575ccd979bdSMark Fasheh sync_blockdev(sb->s_bdev);
576ccd979bdSMark Fasheh }
577ccd979bdSMark Fasheh
ocfs2_truncate_for_delete(struct ocfs2_super * osb,struct inode * inode,struct buffer_head * fe_bh)578ccd979bdSMark Fasheh static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
579ccd979bdSMark Fasheh struct inode *inode,
580ccd979bdSMark Fasheh struct buffer_head *fe_bh)
581ccd979bdSMark Fasheh {
582ccd979bdSMark Fasheh int status = 0;
583ccd979bdSMark Fasheh struct ocfs2_dinode *fe;
58460b11392SMark Fasheh handle_t *handle = NULL;
585ccd979bdSMark Fasheh
586ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) fe_bh->b_data;
587ccd979bdSMark Fasheh
5881afc32b9SMark Fasheh /*
5891afc32b9SMark Fasheh * This check will also skip truncate of inodes with inline
5901afc32b9SMark Fasheh * data and fast symlinks.
5911afc32b9SMark Fasheh */
5923a0782d0SMark Fasheh if (fe->i_clusters) {
5932b4e30fbSJoel Becker if (ocfs2_should_order_data(inode))
5942b4e30fbSJoel Becker ocfs2_begin_ordered_truncate(inode, 0);
5952b4e30fbSJoel Becker
59660b11392SMark Fasheh handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
59760b11392SMark Fasheh if (IS_ERR(handle)) {
59860b11392SMark Fasheh status = PTR_ERR(handle);
5990350cb07SDan Carpenter handle = NULL;
60060b11392SMark Fasheh mlog_errno(status);
60160b11392SMark Fasheh goto out;
60260b11392SMark Fasheh }
60360b11392SMark Fasheh
6040cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
6050cf2f763SJoel Becker fe_bh,
60660b11392SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE);
60760b11392SMark Fasheh if (status < 0) {
60860b11392SMark Fasheh mlog_errno(status);
60960b11392SMark Fasheh goto out;
61060b11392SMark Fasheh }
61160b11392SMark Fasheh
61260b11392SMark Fasheh i_size_write(inode, 0);
61360b11392SMark Fasheh
61460b11392SMark Fasheh status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
61560b11392SMark Fasheh if (status < 0) {
61660b11392SMark Fasheh mlog_errno(status);
61760b11392SMark Fasheh goto out;
61860b11392SMark Fasheh }
61960b11392SMark Fasheh
62060b11392SMark Fasheh ocfs2_commit_trans(osb, handle);
62160b11392SMark Fasheh handle = NULL;
62260b11392SMark Fasheh
62378f94673STristan Ye status = ocfs2_commit_truncate(osb, inode, fe_bh);
624229ba1f8Swangyan if (status < 0)
625ccd979bdSMark Fasheh mlog_errno(status);
6263a0782d0SMark Fasheh }
627ccd979bdSMark Fasheh
62860b11392SMark Fasheh out:
62960b11392SMark Fasheh if (handle)
63060b11392SMark Fasheh ocfs2_commit_trans(osb, handle);
631ccd979bdSMark Fasheh return status;
632ccd979bdSMark Fasheh }
633ccd979bdSMark Fasheh
ocfs2_remove_inode(struct inode * inode,struct buffer_head * di_bh,struct inode * orphan_dir_inode,struct buffer_head * orphan_dir_bh)634ccd979bdSMark Fasheh static int ocfs2_remove_inode(struct inode *inode,
635ccd979bdSMark Fasheh struct buffer_head *di_bh,
636ccd979bdSMark Fasheh struct inode *orphan_dir_inode,
637ccd979bdSMark Fasheh struct buffer_head *orphan_dir_bh)
638ccd979bdSMark Fasheh {
639ccd979bdSMark Fasheh int status;
640ccd979bdSMark Fasheh struct inode *inode_alloc_inode = NULL;
641ccd979bdSMark Fasheh struct buffer_head *inode_alloc_bh = NULL;
6421fabe148SMark Fasheh handle_t *handle;
643ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
644ccd979bdSMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
645ccd979bdSMark Fasheh
646ccd979bdSMark Fasheh inode_alloc_inode =
647ccd979bdSMark Fasheh ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
648ccd979bdSMark Fasheh le16_to_cpu(di->i_suballoc_slot));
649ccd979bdSMark Fasheh if (!inode_alloc_inode) {
65062f8b1f0SJoseph Qi status = -ENOENT;
651ccd979bdSMark Fasheh mlog_errno(status);
652ccd979bdSMark Fasheh goto bail;
653ccd979bdSMark Fasheh }
654ccd979bdSMark Fasheh
6555955102cSAl Viro inode_lock(inode_alloc_inode);
656e63aecb6SMark Fasheh status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
657ccd979bdSMark Fasheh if (status < 0) {
6585955102cSAl Viro inode_unlock(inode_alloc_inode);
659ccd979bdSMark Fasheh
660ccd979bdSMark Fasheh mlog_errno(status);
661ccd979bdSMark Fasheh goto bail;
662ccd979bdSMark Fasheh }
663ccd979bdSMark Fasheh
664a90714c1SJan Kara handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
665a90714c1SJan Kara ocfs2_quota_trans_credits(inode->i_sb));
666ccd979bdSMark Fasheh if (IS_ERR(handle)) {
667ccd979bdSMark Fasheh status = PTR_ERR(handle);
668ccd979bdSMark Fasheh mlog_errno(status);
669ccd979bdSMark Fasheh goto bail_unlock;
670ccd979bdSMark Fasheh }
671ccd979bdSMark Fasheh
672d4cd1871SLi Dongyang if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
673ccd979bdSMark Fasheh status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
67406ee5c75SJoseph Qi orphan_dir_bh, false);
675ccd979bdSMark Fasheh if (status < 0) {
676ccd979bdSMark Fasheh mlog_errno(status);
677ccd979bdSMark Fasheh goto bail_commit;
678ccd979bdSMark Fasheh }
679d4cd1871SLi Dongyang }
680ccd979bdSMark Fasheh
681ccd979bdSMark Fasheh /* set the inodes dtime */
6820cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
683ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE);
684ccd979bdSMark Fasheh if (status < 0) {
685ccd979bdSMark Fasheh mlog_errno(status);
686ccd979bdSMark Fasheh goto bail_commit;
687ccd979bdSMark Fasheh }
688ccd979bdSMark Fasheh
689c62c38f6SDeepa Dinamani di->i_dtime = cpu_to_le64(ktime_get_real_seconds());
6904092d49fSMarcin Slusarz di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
691ec20cec7SJoel Becker ocfs2_journal_dirty(handle, di_bh);
692ccd979bdSMark Fasheh
6938cb471e8SJoel Becker ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
69463936ddaSChristoph Hellwig dquot_free_inode(inode);
695ccd979bdSMark Fasheh
696ccd979bdSMark Fasheh status = ocfs2_free_dinode(handle, inode_alloc_inode,
697ccd979bdSMark Fasheh inode_alloc_bh, di);
698ccd979bdSMark Fasheh if (status < 0)
699ccd979bdSMark Fasheh mlog_errno(status);
700ccd979bdSMark Fasheh
701ccd979bdSMark Fasheh bail_commit:
70202dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle);
703ccd979bdSMark Fasheh bail_unlock:
704e63aecb6SMark Fasheh ocfs2_inode_unlock(inode_alloc_inode, 1);
7055955102cSAl Viro inode_unlock(inode_alloc_inode);
706ccd979bdSMark Fasheh brelse(inode_alloc_bh);
707ccd979bdSMark Fasheh bail:
708ccd979bdSMark Fasheh iput(inode_alloc_inode);
709ccd979bdSMark Fasheh
710ccd979bdSMark Fasheh return status;
711ccd979bdSMark Fasheh }
712ccd979bdSMark Fasheh
713b4df6ed8SMark Fasheh /*
714b4df6ed8SMark Fasheh * Serialize with orphan dir recovery. If the process doing
715b4df6ed8SMark Fasheh * recovery on this orphan dir does an iget() with the dir
716137cebf9Shongnanli * i_rwsem held, we'll deadlock here. Instead we detect this
717b4df6ed8SMark Fasheh * and exit early - recovery will wipe this inode for us.
718b4df6ed8SMark Fasheh */
ocfs2_check_orphan_recovery_state(struct ocfs2_super * osb,int slot)719b4df6ed8SMark Fasheh static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
720b4df6ed8SMark Fasheh int slot)
721b4df6ed8SMark Fasheh {
722b4df6ed8SMark Fasheh int ret = 0;
723b4df6ed8SMark Fasheh
724b4df6ed8SMark Fasheh spin_lock(&osb->osb_lock);
725b4df6ed8SMark Fasheh if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
726b4df6ed8SMark Fasheh ret = -EDEADLK;
727b4df6ed8SMark Fasheh goto out;
728b4df6ed8SMark Fasheh }
729b4df6ed8SMark Fasheh /* This signals to the orphan recovery process that it should
730b4df6ed8SMark Fasheh * wait for us to handle the wipe. */
731b4df6ed8SMark Fasheh osb->osb_orphan_wipes[slot]++;
732b4df6ed8SMark Fasheh out:
733b4df6ed8SMark Fasheh spin_unlock(&osb->osb_lock);
73464f3b269STao Ma trace_ocfs2_check_orphan_recovery_state(slot, ret);
735b4df6ed8SMark Fasheh return ret;
736b4df6ed8SMark Fasheh }
737b4df6ed8SMark Fasheh
ocfs2_signal_wipe_completion(struct ocfs2_super * osb,int slot)738b4df6ed8SMark Fasheh static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
739b4df6ed8SMark Fasheh int slot)
740b4df6ed8SMark Fasheh {
741b4df6ed8SMark Fasheh spin_lock(&osb->osb_lock);
742b4df6ed8SMark Fasheh osb->osb_orphan_wipes[slot]--;
743b4df6ed8SMark Fasheh spin_unlock(&osb->osb_lock);
744b4df6ed8SMark Fasheh
745b4df6ed8SMark Fasheh wake_up(&osb->osb_wipe_event);
746b4df6ed8SMark Fasheh }
747b4df6ed8SMark Fasheh
ocfs2_wipe_inode(struct inode * inode,struct buffer_head * di_bh)748ccd979bdSMark Fasheh static int ocfs2_wipe_inode(struct inode *inode,
749ccd979bdSMark Fasheh struct buffer_head *di_bh)
750ccd979bdSMark Fasheh {
751d577632eSJoel Becker int status, orphaned_slot = -1;
752ccd979bdSMark Fasheh struct inode *orphan_dir_inode = NULL;
753ccd979bdSMark Fasheh struct buffer_head *orphan_dir_bh = NULL;
754ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
755d4cd1871SLi Dongyang struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
756ccd979bdSMark Fasheh
757d4cd1871SLi Dongyang if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
75850008630STiger Yang orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
759b4df6ed8SMark Fasheh
760b4df6ed8SMark Fasheh status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
761b4df6ed8SMark Fasheh if (status)
762b4df6ed8SMark Fasheh return status;
763b4df6ed8SMark Fasheh
764ccd979bdSMark Fasheh orphan_dir_inode = ocfs2_get_system_file_inode(osb,
765ccd979bdSMark Fasheh ORPHAN_DIR_SYSTEM_INODE,
766ccd979bdSMark Fasheh orphaned_slot);
767ccd979bdSMark Fasheh if (!orphan_dir_inode) {
76862f8b1f0SJoseph Qi status = -ENOENT;
769ccd979bdSMark Fasheh mlog_errno(status);
770ccd979bdSMark Fasheh goto bail;
771ccd979bdSMark Fasheh }
772ccd979bdSMark Fasheh
773ccd979bdSMark Fasheh /* Lock the orphan dir. The lock will be held for the entire
774ccd979bdSMark Fasheh * delete_inode operation. We do this now to avoid races with
775ccd979bdSMark Fasheh * recovery completion on other nodes. */
7765955102cSAl Viro inode_lock(orphan_dir_inode);
777e63aecb6SMark Fasheh status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
778ccd979bdSMark Fasheh if (status < 0) {
7795955102cSAl Viro inode_unlock(orphan_dir_inode);
780ccd979bdSMark Fasheh
781ccd979bdSMark Fasheh mlog_errno(status);
782ccd979bdSMark Fasheh goto bail;
783ccd979bdSMark Fasheh }
784d4cd1871SLi Dongyang }
785ccd979bdSMark Fasheh
786ccd979bdSMark Fasheh /* we do this while holding the orphan dir lock because we
78734d024f8SMark Fasheh * don't want recovery being run from another node to try an
78834d024f8SMark Fasheh * inode delete underneath us -- this will result in two nodes
789ccd979bdSMark Fasheh * truncating the same file! */
790ccd979bdSMark Fasheh status = ocfs2_truncate_for_delete(osb, inode, di_bh);
791ccd979bdSMark Fasheh if (status < 0) {
792ccd979bdSMark Fasheh mlog_errno(status);
793ccd979bdSMark Fasheh goto bail_unlock_dir;
794ccd979bdSMark Fasheh }
795ccd979bdSMark Fasheh
7969b7895efSMark Fasheh /* Remove any dir index tree */
7979b7895efSMark Fasheh if (S_ISDIR(inode->i_mode)) {
7989b7895efSMark Fasheh status = ocfs2_dx_dir_truncate(inode, di_bh);
7999b7895efSMark Fasheh if (status) {
8009b7895efSMark Fasheh mlog_errno(status);
8019b7895efSMark Fasheh goto bail_unlock_dir;
8029b7895efSMark Fasheh }
8039b7895efSMark Fasheh }
8049b7895efSMark Fasheh
805cf1d6c76STiger Yang /*Free extended attribute resources associated with this inode.*/
806cf1d6c76STiger Yang status = ocfs2_xattr_remove(inode, di_bh);
807cf1d6c76STiger Yang if (status < 0) {
808cf1d6c76STiger Yang mlog_errno(status);
809cf1d6c76STiger Yang goto bail_unlock_dir;
810cf1d6c76STiger Yang }
811cf1d6c76STiger Yang
8128b2c0dbaSTao Ma status = ocfs2_remove_refcount_tree(inode, di_bh);
8138b2c0dbaSTao Ma if (status < 0) {
8148b2c0dbaSTao Ma mlog_errno(status);
8158b2c0dbaSTao Ma goto bail_unlock_dir;
8168b2c0dbaSTao Ma }
8178b2c0dbaSTao Ma
818ccd979bdSMark Fasheh status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
819ccd979bdSMark Fasheh orphan_dir_bh);
820ccd979bdSMark Fasheh if (status < 0)
821ccd979bdSMark Fasheh mlog_errno(status);
822ccd979bdSMark Fasheh
823ccd979bdSMark Fasheh bail_unlock_dir:
824d4cd1871SLi Dongyang if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
825d4cd1871SLi Dongyang return status;
826d4cd1871SLi Dongyang
827e63aecb6SMark Fasheh ocfs2_inode_unlock(orphan_dir_inode, 1);
8285955102cSAl Viro inode_unlock(orphan_dir_inode);
829ccd979bdSMark Fasheh brelse(orphan_dir_bh);
830ccd979bdSMark Fasheh bail:
831ccd979bdSMark Fasheh iput(orphan_dir_inode);
832b4df6ed8SMark Fasheh ocfs2_signal_wipe_completion(osb, orphaned_slot);
833ccd979bdSMark Fasheh
834ccd979bdSMark Fasheh return status;
835ccd979bdSMark Fasheh }
836ccd979bdSMark Fasheh
837ccd979bdSMark Fasheh /* There is a series of simple checks that should be done before a
83834d024f8SMark Fasheh * trylock is even considered. Encapsulate those in this function. */
ocfs2_inode_is_valid_to_delete(struct inode * inode)839ccd979bdSMark Fasheh static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
840ccd979bdSMark Fasheh {
841ccd979bdSMark Fasheh int ret = 0;
842ccd979bdSMark Fasheh struct ocfs2_inode_info *oi = OCFS2_I(inode);
843ccd979bdSMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
844ccd979bdSMark Fasheh
84564f3b269STao Ma trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
84664f3b269STao Ma (unsigned long long)oi->ip_blkno,
84764f3b269STao Ma oi->ip_flags);
84864f3b269STao Ma
849ccd979bdSMark Fasheh /* We shouldn't be getting here for the root directory
850ccd979bdSMark Fasheh * inode.. */
851ccd979bdSMark Fasheh if (inode == osb->root_inode) {
852ccd979bdSMark Fasheh mlog(ML_ERROR, "Skipping delete of root inode.\n");
853ccd979bdSMark Fasheh goto bail;
854ccd979bdSMark Fasheh }
855ccd979bdSMark Fasheh
856bd62ad7aSJan Kara /*
857bd62ad7aSJan Kara * If we're coming from downconvert_thread we can't go into our own
858bd62ad7aSJan Kara * voting [hello, deadlock city!] so we cannot delete the inode. But
859bd62ad7aSJan Kara * since we dropped last inode ref when downconverting dentry lock,
860bd62ad7aSJan Kara * we cannot have the file open and thus the node doing unlink will
861bd62ad7aSJan Kara * take care of deleting the inode.
862bd62ad7aSJan Kara */
86364f3b269STao Ma if (current == osb->dc_task)
864ccd979bdSMark Fasheh goto bail;
865ccd979bdSMark Fasheh
866ccd979bdSMark Fasheh spin_lock(&oi->ip_lock);
867ccd979bdSMark Fasheh /* OCFS2 *never* deletes system files. This should technically
868ccd979bdSMark Fasheh * never get here as system file inodes should always have a
869ccd979bdSMark Fasheh * positive link count. */
870ccd979bdSMark Fasheh if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
871b0697053SMark Fasheh mlog(ML_ERROR, "Skipping delete of system file %llu\n",
872b0697053SMark Fasheh (unsigned long long)oi->ip_blkno);
873ccd979bdSMark Fasheh goto bail_unlock;
874ccd979bdSMark Fasheh }
875ccd979bdSMark Fasheh
876ccd979bdSMark Fasheh ret = 1;
877ccd979bdSMark Fasheh bail_unlock:
878ccd979bdSMark Fasheh spin_unlock(&oi->ip_lock);
879ccd979bdSMark Fasheh bail:
880ccd979bdSMark Fasheh return ret;
881ccd979bdSMark Fasheh }
882ccd979bdSMark Fasheh
883ccd979bdSMark Fasheh /* Query the cluster to determine whether we should wipe an inode from
884ccd979bdSMark Fasheh * disk or not.
885ccd979bdSMark Fasheh *
886ccd979bdSMark Fasheh * Requires the inode to have the cluster lock. */
ocfs2_query_inode_wipe(struct inode * inode,struct buffer_head * di_bh,int * wipe)887ccd979bdSMark Fasheh static int ocfs2_query_inode_wipe(struct inode *inode,
888ccd979bdSMark Fasheh struct buffer_head *di_bh,
889ccd979bdSMark Fasheh int *wipe)
890ccd979bdSMark Fasheh {
89164f3b269STao Ma int status = 0, reason = 0;
892ccd979bdSMark Fasheh struct ocfs2_inode_info *oi = OCFS2_I(inode);
893ccd979bdSMark Fasheh struct ocfs2_dinode *di;
894ccd979bdSMark Fasheh
895ccd979bdSMark Fasheh *wipe = 0;
896ccd979bdSMark Fasheh
89764f3b269STao Ma trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
89864f3b269STao Ma inode->i_nlink);
89964f3b269STao Ma
900ccd979bdSMark Fasheh /* While we were waiting for the cluster lock in
901ccd979bdSMark Fasheh * ocfs2_delete_inode, another node might have asked to delete
902ccd979bdSMark Fasheh * the inode. Recheck our flags to catch this. */
903ccd979bdSMark Fasheh if (!ocfs2_inode_is_valid_to_delete(inode)) {
90464f3b269STao Ma reason = 1;
905ccd979bdSMark Fasheh goto bail;
906ccd979bdSMark Fasheh }
907ccd979bdSMark Fasheh
908ccd979bdSMark Fasheh /* Now that we have an up to date inode, we can double check
909ccd979bdSMark Fasheh * the link count. */
91064f3b269STao Ma if (inode->i_nlink)
911ccd979bdSMark Fasheh goto bail;
912ccd979bdSMark Fasheh
913ccd979bdSMark Fasheh /* Do some basic inode verification... */
914ccd979bdSMark Fasheh di = (struct ocfs2_dinode *) di_bh->b_data;
915d4cd1871SLi Dongyang if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
916d4cd1871SLi Dongyang !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
917b54c2ca4STristan Ye /*
918b54c2ca4STristan Ye * Inodes in the orphan dir must have ORPHANED_FL. The only
919b54c2ca4STristan Ye * inodes that come back out of the orphan dir are reflink
920b54c2ca4STristan Ye * targets. A reflink target may be moved out of the orphan
921b54c2ca4STristan Ye * dir between the time we scan the directory and the time we
922b54c2ca4STristan Ye * process it. This would lead to HAS_REFCOUNT_FL being set but
923b54c2ca4STristan Ye * ORPHANED_FL not.
924b54c2ca4STristan Ye */
925b54c2ca4STristan Ye if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
92664f3b269STao Ma reason = 2;
927b54c2ca4STristan Ye goto bail;
928b54c2ca4STristan Ye }
929b54c2ca4STristan Ye
930ccd979bdSMark Fasheh /* for lack of a better error? */
931ccd979bdSMark Fasheh status = -EEXIST;
932ccd979bdSMark Fasheh mlog(ML_ERROR,
933b0697053SMark Fasheh "Inode %llu (on-disk %llu) not orphaned! "
934ccd979bdSMark Fasheh "Disk flags 0x%x, inode flags 0x%x\n",
935b0697053SMark Fasheh (unsigned long long)oi->ip_blkno,
9361ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(di->i_blkno),
9371ca1a111SMark Fasheh le32_to_cpu(di->i_flags), oi->ip_flags);
938ccd979bdSMark Fasheh goto bail;
939ccd979bdSMark Fasheh }
940ccd979bdSMark Fasheh
941ccd979bdSMark Fasheh /* has someone already deleted us?! baaad... */
942ccd979bdSMark Fasheh if (di->i_dtime) {
943ccd979bdSMark Fasheh status = -EEXIST;
944ccd979bdSMark Fasheh mlog_errno(status);
945ccd979bdSMark Fasheh goto bail;
946ccd979bdSMark Fasheh }
947ccd979bdSMark Fasheh
9486f16bf65SMark Fasheh /*
9496f16bf65SMark Fasheh * This is how ocfs2 determines whether an inode is still live
9506f16bf65SMark Fasheh * within the cluster. Every node takes a shared read lock on
9516f16bf65SMark Fasheh * the inode open lock in ocfs2_read_locked_inode(). When we
9526f16bf65SMark Fasheh * get to ->delete_inode(), each node tries to convert it's
9536f16bf65SMark Fasheh * lock to an exclusive. Trylocks are serialized by the inode
95425985edcSLucas De Marchi * meta data lock. If the upconvert succeeds, we know the inode
9556f16bf65SMark Fasheh * is no longer live and can be deleted.
9566f16bf65SMark Fasheh *
9576f16bf65SMark Fasheh * Though we call this with the meta data lock held, the
9586f16bf65SMark Fasheh * trylock keeps us from ABBA deadlock.
9596f16bf65SMark Fasheh */
9606f16bf65SMark Fasheh status = ocfs2_try_open_lock(inode, 1);
96150008630STiger Yang if (status == -EAGAIN) {
962ccd979bdSMark Fasheh status = 0;
96364f3b269STao Ma reason = 3;
964ccd979bdSMark Fasheh goto bail;
965ccd979bdSMark Fasheh }
966ccd979bdSMark Fasheh if (status < 0) {
967ccd979bdSMark Fasheh mlog_errno(status);
968ccd979bdSMark Fasheh goto bail;
969ccd979bdSMark Fasheh }
970ccd979bdSMark Fasheh
971ccd979bdSMark Fasheh *wipe = 1;
97264f3b269STao Ma trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
973ccd979bdSMark Fasheh
974ccd979bdSMark Fasheh bail:
97564f3b269STao Ma trace_ocfs2_query_inode_wipe_end(status, reason);
976ccd979bdSMark Fasheh return status;
977ccd979bdSMark Fasheh }
978ccd979bdSMark Fasheh
979ccd979bdSMark Fasheh /* Support function for ocfs2_delete_inode. Will help us keep the
980ccd979bdSMark Fasheh * inode data in a consistent state for clear_inode. Always truncates
981ccd979bdSMark Fasheh * pages, optionally sync's them first. */
ocfs2_cleanup_delete_inode(struct inode * inode,int sync_data)982ccd979bdSMark Fasheh static void ocfs2_cleanup_delete_inode(struct inode *inode,
983ccd979bdSMark Fasheh int sync_data)
984ccd979bdSMark Fasheh {
98564f3b269STao Ma trace_ocfs2_cleanup_delete_inode(
986b0697053SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
987ccd979bdSMark Fasheh if (sync_data)
988249ec93cSJan Kara filemap_write_and_wait(inode->i_mapping);
98991b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data);
990ccd979bdSMark Fasheh }
991ccd979bdSMark Fasheh
ocfs2_delete_inode(struct inode * inode)992066d92dcSAl Viro static void ocfs2_delete_inode(struct inode *inode)
993ccd979bdSMark Fasheh {
994ccd979bdSMark Fasheh int wipe, status;
995e4b963f1SJoel Becker sigset_t oldset;
996ccd979bdSMark Fasheh struct buffer_head *di_bh = NULL;
997ad694821SJoseph Qi struct ocfs2_dinode *di = NULL;
998ccd979bdSMark Fasheh
99964f3b269STao Ma trace_ocfs2_delete_inode(inode->i_ino,
100064f3b269STao Ma (unsigned long long)OCFS2_I(inode)->ip_blkno,
100164f3b269STao Ma is_bad_inode(inode));
1002ccd979bdSMark Fasheh
1003a90714c1SJan Kara /* When we fail in read_inode() we mark inode as bad. The second test
1004a90714c1SJan Kara * catches the case when inode allocation fails before allocating
1005a90714c1SJan Kara * a block for inode. */
100664f3b269STao Ma if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
1007ccd979bdSMark Fasheh goto bail;
1008ccd979bdSMark Fasheh
1009ccd979bdSMark Fasheh if (!ocfs2_inode_is_valid_to_delete(inode)) {
1010ccd979bdSMark Fasheh /* It's probably not necessary to truncate_inode_pages
1011ccd979bdSMark Fasheh * here but we do it for safety anyway (it will most
1012ccd979bdSMark Fasheh * likely be a no-op anyway) */
1013ccd979bdSMark Fasheh ocfs2_cleanup_delete_inode(inode, 0);
1014ccd979bdSMark Fasheh goto bail;
1015ccd979bdSMark Fasheh }
1016ccd979bdSMark Fasheh
1017bd62ad7aSJan Kara dquot_initialize(inode);
1018bd62ad7aSJan Kara
1019ccd979bdSMark Fasheh /* We want to block signals in delete_inode as the lock and
1020ccd979bdSMark Fasheh * messaging paths may return us -ERESTARTSYS. Which would
1021ccd979bdSMark Fasheh * cause us to exit early, resulting in inodes being orphaned
1022ccd979bdSMark Fasheh * forever. */
1023e4b963f1SJoel Becker ocfs2_block_signals(&oldset);
1024ccd979bdSMark Fasheh
10256ca497a8Swengang wang /*
10266ca497a8Swengang wang * Synchronize us against ocfs2_get_dentry. We take this in
10276ca497a8Swengang wang * shared mode so that all nodes can still concurrently
10286ca497a8Swengang wang * process deletes.
10296ca497a8Swengang wang */
10306ca497a8Swengang wang status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
10316ca497a8Swengang wang if (status < 0) {
10326ca497a8Swengang wang mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status);
10336ca497a8Swengang wang ocfs2_cleanup_delete_inode(inode, 0);
10346ca497a8Swengang wang goto bail_unblock;
10356ca497a8Swengang wang }
1036ccd979bdSMark Fasheh /* Lock down the inode. This gives us an up to date view of
1037ccd979bdSMark Fasheh * it's metadata (for verification), and allows us to
103834d024f8SMark Fasheh * serialize delete_inode on multiple nodes.
1039ccd979bdSMark Fasheh *
1040ccd979bdSMark Fasheh * Even though we might be doing a truncate, we don't take the
1041ccd979bdSMark Fasheh * allocation lock here as it won't be needed - nobody will
1042ccd979bdSMark Fasheh * have the file open.
1043ccd979bdSMark Fasheh */
1044e63aecb6SMark Fasheh status = ocfs2_inode_lock(inode, &di_bh, 1);
1045ccd979bdSMark Fasheh if (status < 0) {
1046ccd979bdSMark Fasheh if (status != -ENOENT)
1047ccd979bdSMark Fasheh mlog_errno(status);
1048ccd979bdSMark Fasheh ocfs2_cleanup_delete_inode(inode, 0);
10496ca497a8Swengang wang goto bail_unlock_nfs_sync;
1050ccd979bdSMark Fasheh }
1051ccd979bdSMark Fasheh
1052ad694821SJoseph Qi di = (struct ocfs2_dinode *)di_bh->b_data;
1053ad694821SJoseph Qi /* Skip inode deletion and wait for dio orphan entry recovered
1054ad694821SJoseph Qi * first */
1055ad694821SJoseph Qi if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
1056ad694821SJoseph Qi ocfs2_cleanup_delete_inode(inode, 0);
1057ad694821SJoseph Qi goto bail_unlock_inode;
1058ad694821SJoseph Qi }
1059ad694821SJoseph Qi
1060ccd979bdSMark Fasheh /* Query the cluster. This will be the final decision made
1061ccd979bdSMark Fasheh * before we go ahead and wipe the inode. */
1062ccd979bdSMark Fasheh status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
1063ccd979bdSMark Fasheh if (!wipe || status < 0) {
106434d024f8SMark Fasheh /* Error and remote inode busy both mean we won't be
1065ccd979bdSMark Fasheh * removing the inode, so they take almost the same
1066ccd979bdSMark Fasheh * path. */
1067ccd979bdSMark Fasheh if (status < 0)
1068ccd979bdSMark Fasheh mlog_errno(status);
1069ccd979bdSMark Fasheh
107034d024f8SMark Fasheh /* Someone in the cluster has disallowed a wipe of
107134d024f8SMark Fasheh * this inode, or it was never completely
107234d024f8SMark Fasheh * orphaned. Write out the pages and exit now. */
1073ccd979bdSMark Fasheh ocfs2_cleanup_delete_inode(inode, 1);
1074ccd979bdSMark Fasheh goto bail_unlock_inode;
1075ccd979bdSMark Fasheh }
1076ccd979bdSMark Fasheh
1077ccd979bdSMark Fasheh ocfs2_cleanup_delete_inode(inode, 0);
1078ccd979bdSMark Fasheh
1079ccd979bdSMark Fasheh status = ocfs2_wipe_inode(inode, di_bh);
1080ccd979bdSMark Fasheh if (status < 0) {
1081b4df6ed8SMark Fasheh if (status != -EDEADLK)
1082ccd979bdSMark Fasheh mlog_errno(status);
1083ccd979bdSMark Fasheh goto bail_unlock_inode;
1084ccd979bdSMark Fasheh }
1085ccd979bdSMark Fasheh
108624c19ef4SMark Fasheh /*
108724c19ef4SMark Fasheh * Mark the inode as successfully deleted.
108824c19ef4SMark Fasheh *
108924c19ef4SMark Fasheh * This is important for ocfs2_clear_inode() as it will check
109024c19ef4SMark Fasheh * this flag and skip any checkpointing work
109124c19ef4SMark Fasheh *
109224c19ef4SMark Fasheh * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
109324c19ef4SMark Fasheh * the LVB for other nodes.
109424c19ef4SMark Fasheh */
1095ccd979bdSMark Fasheh OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1096ccd979bdSMark Fasheh
1097ccd979bdSMark Fasheh bail_unlock_inode:
1098e63aecb6SMark Fasheh ocfs2_inode_unlock(inode, 1);
1099ccd979bdSMark Fasheh brelse(di_bh);
11006ca497a8Swengang wang
11016ca497a8Swengang wang bail_unlock_nfs_sync:
11026ca497a8Swengang wang ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
11036ca497a8Swengang wang
1104ccd979bdSMark Fasheh bail_unblock:
1105e4b963f1SJoel Becker ocfs2_unblock_signals(&oldset);
1106ccd979bdSMark Fasheh bail:
1107c1e8d35eSTao Ma return;
1108ccd979bdSMark Fasheh }
1109ccd979bdSMark Fasheh
ocfs2_clear_inode(struct inode * inode)1110066d92dcSAl Viro static void ocfs2_clear_inode(struct inode *inode)
1111ccd979bdSMark Fasheh {
1112ccd979bdSMark Fasheh int status;
1113ccd979bdSMark Fasheh struct ocfs2_inode_info *oi = OCFS2_I(inode);
111484d86f83SJan Kara struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1115ccd979bdSMark Fasheh
1116dbd5768fSJan Kara clear_inode(inode);
111764f3b269STao Ma trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
111864f3b269STao Ma inode->i_nlink);
1119ccd979bdSMark Fasheh
11201119d3c0Spiaojun mlog_bug_on_msg(osb == NULL,
1121ccd979bdSMark Fasheh "Inode=%lu\n", inode->i_ino);
1122ccd979bdSMark Fasheh
11239f754758SChristoph Hellwig dquot_drop(inode);
1124257ba15cSChristoph Hellwig
112534d024f8SMark Fasheh /* To preven remote deletes we hold open lock before, now it
112634d024f8SMark Fasheh * is time to unlock PR and EX open locks. */
112750008630STiger Yang ocfs2_open_unlock(inode);
112850008630STiger Yang
1129ccd979bdSMark Fasheh /* Do these before all the other work so that we don't bounce
113034d024f8SMark Fasheh * the downconvert thread while waiting to destroy the locks. */
113184d86f83SJan Kara ocfs2_mark_lockres_freeing(osb, &oi->ip_rw_lockres);
113284d86f83SJan Kara ocfs2_mark_lockres_freeing(osb, &oi->ip_inode_lockres);
113384d86f83SJan Kara ocfs2_mark_lockres_freeing(osb, &oi->ip_open_lockres);
1134ccd979bdSMark Fasheh
11351119d3c0Spiaojun ocfs2_resv_discard(&osb->osb_la_resmap,
11364fe370afSMark Fasheh &oi->ip_la_data_resv);
11374fe370afSMark Fasheh ocfs2_resv_init_once(&oi->ip_la_data_resv);
11384fe370afSMark Fasheh
1139ccd979bdSMark Fasheh /* We very well may get a clear_inode before all an inodes
1140ccd979bdSMark Fasheh * metadata has hit disk. Of course, we can't drop any cluster
1141ccd979bdSMark Fasheh * locks until the journal has finished with it. The only
1142ccd979bdSMark Fasheh * exception here are successfully wiped inodes - their
1143ccd979bdSMark Fasheh * metadata can now be considered to be part of the system
1144ccd979bdSMark Fasheh * inodes from which it came. */
1145d324cd4cSpiaojun if (!(oi->ip_flags & OCFS2_INODE_DELETED))
1146ccd979bdSMark Fasheh ocfs2_checkpoint_inode(inode);
1147ccd979bdSMark Fasheh
1148ccd979bdSMark Fasheh mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
1149b0697053SMark Fasheh "Clear inode of %llu, inode has io markers\n",
1150b0697053SMark Fasheh (unsigned long long)oi->ip_blkno);
11514506cfb6SRyan Ding mlog_bug_on_msg(!list_empty(&oi->ip_unwritten_list),
11524506cfb6SRyan Ding "Clear inode of %llu, inode has unwritten extents\n",
11534506cfb6SRyan Ding (unsigned long long)oi->ip_blkno);
1154ccd979bdSMark Fasheh
115583418978SMark Fasheh ocfs2_extent_map_trunc(inode, 0);
115683418978SMark Fasheh
1157ccd979bdSMark Fasheh status = ocfs2_drop_inode_locks(inode);
1158ccd979bdSMark Fasheh if (status < 0)
1159ccd979bdSMark Fasheh mlog_errno(status);
1160ccd979bdSMark Fasheh
1161ccd979bdSMark Fasheh ocfs2_lock_res_free(&oi->ip_rw_lockres);
1162e63aecb6SMark Fasheh ocfs2_lock_res_free(&oi->ip_inode_lockres);
116350008630STiger Yang ocfs2_lock_res_free(&oi->ip_open_lockres);
1164ccd979bdSMark Fasheh
116566fb345dSJoel Becker ocfs2_metadata_cache_exit(INODE_CACHE(inode));
1166ccd979bdSMark Fasheh
11678cb471e8SJoel Becker mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
1168b0697053SMark Fasheh "Clear inode of %llu, inode has %u cache items\n",
11698cb471e8SJoel Becker (unsigned long long)oi->ip_blkno,
11708cb471e8SJoel Becker INODE_CACHE(inode)->ci_num_cached);
1171ccd979bdSMark Fasheh
11728cb471e8SJoel Becker mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
1173b0697053SMark Fasheh "Clear inode of %llu, inode has a bad flag\n",
1174b0697053SMark Fasheh (unsigned long long)oi->ip_blkno);
1175ccd979bdSMark Fasheh
1176ccd979bdSMark Fasheh mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
1177b0697053SMark Fasheh "Clear inode of %llu, inode is locked\n",
1178b0697053SMark Fasheh (unsigned long long)oi->ip_blkno);
1179ccd979bdSMark Fasheh
1180251b6eccSMark Fasheh mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
1181b0697053SMark Fasheh "Clear inode of %llu, io_mutex is locked\n",
1182b0697053SMark Fasheh (unsigned long long)oi->ip_blkno);
1183251b6eccSMark Fasheh mutex_unlock(&oi->ip_io_mutex);
1184ccd979bdSMark Fasheh
1185ccd979bdSMark Fasheh /*
1186ccd979bdSMark Fasheh * down_trylock() returns 0, down_write_trylock() returns 1
1187ccd979bdSMark Fasheh * kernel 1, world 0
1188ccd979bdSMark Fasheh */
1189ccd979bdSMark Fasheh mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
1190b0697053SMark Fasheh "Clear inode of %llu, alloc_sem is locked\n",
1191b0697053SMark Fasheh (unsigned long long)oi->ip_blkno);
1192ccd979bdSMark Fasheh up_write(&oi->ip_alloc_sem);
1193ccd979bdSMark Fasheh
1194ccd979bdSMark Fasheh mlog_bug_on_msg(oi->ip_open_count,
1195b0697053SMark Fasheh "Clear inode of %llu has open count %d\n",
1196b0697053SMark Fasheh (unsigned long long)oi->ip_blkno, oi->ip_open_count);
1197ccd979bdSMark Fasheh
1198ccd979bdSMark Fasheh /* Clear all other flags. */
119947460d65SJoel Becker oi->ip_flags = 0;
1200ccd979bdSMark Fasheh oi->ip_dir_start_lookup = 0;
1201ccd979bdSMark Fasheh oi->ip_blkno = 0ULL;
1202ae0dff68SSunil Mushran
1203ae0dff68SSunil Mushran /*
1204ae0dff68SSunil Mushran * ip_jinode is used to track txns against this inode. We ensure that
1205ae0dff68SSunil Mushran * the journal is flushed before journal shutdown. Thus it is safe to
1206ae0dff68SSunil Mushran * have inodes get cleaned up after journal shutdown.
1207ae0dff68SSunil Mushran */
12081119d3c0Spiaojun jbd2_journal_release_jbd_inode(osb->journal->j_journal,
12092b4e30fbSJoel Becker &oi->ip_jinode);
1210ccd979bdSMark Fasheh }
1211ccd979bdSMark Fasheh
ocfs2_evict_inode(struct inode * inode)1212066d92dcSAl Viro void ocfs2_evict_inode(struct inode *inode)
1213066d92dcSAl Viro {
1214066d92dcSAl Viro if (!inode->i_nlink ||
1215066d92dcSAl Viro (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
1216066d92dcSAl Viro ocfs2_delete_inode(inode);
1217066d92dcSAl Viro } else {
121891b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data);
1219066d92dcSAl Viro }
1220066d92dcSAl Viro ocfs2_clear_inode(inode);
1221066d92dcSAl Viro }
1222066d92dcSAl Viro
1223ccd979bdSMark Fasheh /* Called under inode_lock, with no more references on the
1224ccd979bdSMark Fasheh * struct inode, so it's safe here to check the flags field
1225ccd979bdSMark Fasheh * and to manipulate i_nlink without any other locks. */
ocfs2_drop_inode(struct inode * inode)122645321ac5SAl Viro int ocfs2_drop_inode(struct inode *inode)
1227ccd979bdSMark Fasheh {
1228ccd979bdSMark Fasheh struct ocfs2_inode_info *oi = OCFS2_I(inode);
1229ccd979bdSMark Fasheh
123064f3b269STao Ma trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno,
123164f3b269STao Ma inode->i_nlink, oi->ip_flags);
1232ccd979bdSMark Fasheh
1233513e2daeSXue jiufei assert_spin_locked(&inode->i_lock);
1234513e2daeSXue jiufei inode->i_state |= I_WILL_FREE;
1235513e2daeSXue jiufei spin_unlock(&inode->i_lock);
1236513e2daeSXue jiufei write_inode_now(inode, 1);
1237513e2daeSXue jiufei spin_lock(&inode->i_lock);
1238513e2daeSXue jiufei WARN_ON(inode->i_state & I_NEW);
1239513e2daeSXue jiufei inode->i_state &= ~I_WILL_FREE;
1240ccd979bdSMark Fasheh
1241513e2daeSXue jiufei return 1;
1242ccd979bdSMark Fasheh }
1243ccd979bdSMark Fasheh
1244ccd979bdSMark Fasheh /*
1245ccd979bdSMark Fasheh * This is called from our getattr.
1246ccd979bdSMark Fasheh */
ocfs2_inode_revalidate(struct dentry * dentry)1247ccd979bdSMark Fasheh int ocfs2_inode_revalidate(struct dentry *dentry)
1248ccd979bdSMark Fasheh {
12492b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
1250ccd979bdSMark Fasheh int status = 0;
1251ccd979bdSMark Fasheh
125264f3b269STao Ma trace_ocfs2_inode_revalidate(inode,
125364f3b269STao Ma inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL,
125464f3b269STao Ma inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0);
1255ccd979bdSMark Fasheh
1256ccd979bdSMark Fasheh if (!inode) {
1257ccd979bdSMark Fasheh status = -ENOENT;
1258ccd979bdSMark Fasheh goto bail;
1259ccd979bdSMark Fasheh }
1260ccd979bdSMark Fasheh
1261ccd979bdSMark Fasheh spin_lock(&OCFS2_I(inode)->ip_lock);
1262ccd979bdSMark Fasheh if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1263ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(inode)->ip_lock);
1264ccd979bdSMark Fasheh status = -ENOENT;
1265ccd979bdSMark Fasheh goto bail;
1266ccd979bdSMark Fasheh }
1267ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(inode)->ip_lock);
1268ccd979bdSMark Fasheh
1269e63aecb6SMark Fasheh /* Let ocfs2_inode_lock do the work of updating our struct
1270ccd979bdSMark Fasheh * inode for us. */
1271e63aecb6SMark Fasheh status = ocfs2_inode_lock(inode, NULL, 0);
1272ccd979bdSMark Fasheh if (status < 0) {
1273ccd979bdSMark Fasheh if (status != -ENOENT)
1274ccd979bdSMark Fasheh mlog_errno(status);
1275ccd979bdSMark Fasheh goto bail;
1276ccd979bdSMark Fasheh }
1277e63aecb6SMark Fasheh ocfs2_inode_unlock(inode, 0);
1278ccd979bdSMark Fasheh bail:
1279ccd979bdSMark Fasheh return status;
1280ccd979bdSMark Fasheh }
1281ccd979bdSMark Fasheh
1282ccd979bdSMark Fasheh /*
1283ccd979bdSMark Fasheh * Updates a disk inode from a
1284ccd979bdSMark Fasheh * struct inode.
1285ccd979bdSMark Fasheh * Only takes ip_lock.
1286ccd979bdSMark Fasheh */
ocfs2_mark_inode_dirty(handle_t * handle,struct inode * inode,struct buffer_head * bh)12871fabe148SMark Fasheh int ocfs2_mark_inode_dirty(handle_t *handle,
1288ccd979bdSMark Fasheh struct inode *inode,
1289ccd979bdSMark Fasheh struct buffer_head *bh)
1290ccd979bdSMark Fasheh {
1291ccd979bdSMark Fasheh int status;
1292ccd979bdSMark Fasheh struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1293ccd979bdSMark Fasheh
129464f3b269STao Ma trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
1295ccd979bdSMark Fasheh
12960cf2f763SJoel Becker status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
1297ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE);
1298ccd979bdSMark Fasheh if (status < 0) {
1299ccd979bdSMark Fasheh mlog_errno(status);
1300ccd979bdSMark Fasheh goto leave;
1301ccd979bdSMark Fasheh }
1302ccd979bdSMark Fasheh
1303ccd979bdSMark Fasheh spin_lock(&OCFS2_I(inode)->ip_lock);
1304ccd979bdSMark Fasheh fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
13056e4b0d56SJan Kara ocfs2_get_inode_flags(OCFS2_I(inode));
1306ca4d147eSHerbert Poetzl fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
130715b1e36bSMark Fasheh fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
1308ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(inode)->ip_lock);
1309ccd979bdSMark Fasheh
1310ccd979bdSMark Fasheh fe->i_size = cpu_to_le64(i_size_read(inode));
1311198a1ca3SMark Fasheh ocfs2_set_links_count(fe, inode->i_nlink);
13122c034176SEric W. Biederman fe->i_uid = cpu_to_le32(i_uid_read(inode));
13132c034176SEric W. Biederman fe->i_gid = cpu_to_le32(i_gid_read(inode));
1314ccd979bdSMark Fasheh fe->i_mode = cpu_to_le16(inode->i_mode);
1315*10fc3a18SJeff Layton fe->i_atime = cpu_to_le64(inode_get_atime_sec(inode));
1316*10fc3a18SJeff Layton fe->i_atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode));
1317*10fc3a18SJeff Layton fe->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode));
1318*10fc3a18SJeff Layton fe->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
1319*10fc3a18SJeff Layton fe->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode));
1320*10fc3a18SJeff Layton fe->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode));
1321ccd979bdSMark Fasheh
1322ec20cec7SJoel Becker ocfs2_journal_dirty(handle, bh);
13232931cdcbSDarrick J. Wong ocfs2_update_inode_fsync_trans(handle, inode, 1);
1324ccd979bdSMark Fasheh leave:
1325ccd979bdSMark Fasheh return status;
1326ccd979bdSMark Fasheh }
1327ccd979bdSMark Fasheh
1328ccd979bdSMark Fasheh /*
1329ccd979bdSMark Fasheh *
1330ccd979bdSMark Fasheh * Updates a struct inode from a disk inode.
1331ccd979bdSMark Fasheh * does no i/o, only takes ip_lock.
1332ccd979bdSMark Fasheh */
ocfs2_refresh_inode(struct inode * inode,struct ocfs2_dinode * fe)1333ccd979bdSMark Fasheh void ocfs2_refresh_inode(struct inode *inode,
1334ccd979bdSMark Fasheh struct ocfs2_dinode *fe)
1335ccd979bdSMark Fasheh {
1336ccd979bdSMark Fasheh spin_lock(&OCFS2_I(inode)->ip_lock);
1337ccd979bdSMark Fasheh
1338ccd979bdSMark Fasheh OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1339ca4d147eSHerbert Poetzl OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
134015b1e36bSMark Fasheh OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
1341ca4d147eSHerbert Poetzl ocfs2_set_inode_flags(inode);
1342ccd979bdSMark Fasheh i_size_write(inode, le64_to_cpu(fe->i_size));
1343bfe86848SMiklos Szeredi set_nlink(inode, ocfs2_read_links_count(fe));
13442c034176SEric W. Biederman i_uid_write(inode, le32_to_cpu(fe->i_uid));
13452c034176SEric W. Biederman i_gid_write(inode, le32_to_cpu(fe->i_gid));
1346ccd979bdSMark Fasheh inode->i_mode = le16_to_cpu(fe->i_mode);
1347ccd979bdSMark Fasheh if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1348ccd979bdSMark Fasheh inode->i_blocks = 0;
1349ccd979bdSMark Fasheh else
13508110b073SMark Fasheh inode->i_blocks = ocfs2_inode_sector_count(inode);
1351*10fc3a18SJeff Layton inode_set_atime(inode, le64_to_cpu(fe->i_atime),
1352*10fc3a18SJeff Layton le32_to_cpu(fe->i_atime_nsec));
1353*10fc3a18SJeff Layton inode_set_mtime(inode, le64_to_cpu(fe->i_mtime),
1354*10fc3a18SJeff Layton le32_to_cpu(fe->i_mtime_nsec));
13556861de97SJeff Layton inode_set_ctime(inode, le64_to_cpu(fe->i_ctime),
13566861de97SJeff Layton le32_to_cpu(fe->i_ctime_nsec));
1357ccd979bdSMark Fasheh
1358ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(inode)->ip_lock);
1359ccd979bdSMark Fasheh }
1360b657c95cSJoel Becker
ocfs2_validate_inode_block(struct super_block * sb,struct buffer_head * bh)1361b657c95cSJoel Becker int ocfs2_validate_inode_block(struct super_block *sb,
1362b657c95cSJoel Becker struct buffer_head *bh)
1363b657c95cSJoel Becker {
1364d6b32bbbSJoel Becker int rc;
1365b657c95cSJoel Becker struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1366b657c95cSJoel Becker
136764f3b269STao Ma trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
1368970e4936SJoel Becker
1369b657c95cSJoel Becker BUG_ON(!buffer_uptodate(bh));
1370b657c95cSJoel Becker
1371d6b32bbbSJoel Becker /*
1372d6b32bbbSJoel Becker * If the ecc fails, we return the error but otherwise
1373d6b32bbbSJoel Becker * leave the filesystem running. We know any error is
1374d6b32bbbSJoel Becker * local to this block.
1375d6b32bbbSJoel Becker */
1376d6b32bbbSJoel Becker rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
137713723d00SJoel Becker if (rc) {
137813723d00SJoel Becker mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
137913723d00SJoel Becker (unsigned long long)bh->b_blocknr);
1380d6b32bbbSJoel Becker goto bail;
138113723d00SJoel Becker }
1382d6b32bbbSJoel Becker
1383d6b32bbbSJoel Becker /*
1384d6b32bbbSJoel Becker * Errors after here are fatal.
1385d6b32bbbSJoel Becker */
1386d6b32bbbSJoel Becker
1387d6b32bbbSJoel Becker rc = -EINVAL;
1388d6b32bbbSJoel Becker
1389b657c95cSJoel Becker if (!OCFS2_IS_VALID_DINODE(di)) {
139017a5b9abSGoldwyn Rodrigues rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
1391b657c95cSJoel Becker (unsigned long long)bh->b_blocknr, 7,
1392b657c95cSJoel Becker di->i_signature);
1393b657c95cSJoel Becker goto bail;
1394b657c95cSJoel Becker }
1395b657c95cSJoel Becker
1396b657c95cSJoel Becker if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
139717a5b9abSGoldwyn Rodrigues rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
1398b657c95cSJoel Becker (unsigned long long)bh->b_blocknr,
1399b657c95cSJoel Becker (unsigned long long)le64_to_cpu(di->i_blkno));
1400b657c95cSJoel Becker goto bail;
1401b657c95cSJoel Becker }
1402b657c95cSJoel Becker
1403b657c95cSJoel Becker if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
140417a5b9abSGoldwyn Rodrigues rc = ocfs2_error(sb,
1405b657c95cSJoel Becker "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
1406b657c95cSJoel Becker (unsigned long long)bh->b_blocknr);
1407b657c95cSJoel Becker goto bail;
1408b657c95cSJoel Becker }
1409b657c95cSJoel Becker
1410b657c95cSJoel Becker if (le32_to_cpu(di->i_fs_generation) !=
1411b657c95cSJoel Becker OCFS2_SB(sb)->fs_generation) {
141217a5b9abSGoldwyn Rodrigues rc = ocfs2_error(sb,
1413b657c95cSJoel Becker "Invalid dinode #%llu: fs_generation is %u\n",
1414b657c95cSJoel Becker (unsigned long long)bh->b_blocknr,
1415b657c95cSJoel Becker le32_to_cpu(di->i_fs_generation));
1416b657c95cSJoel Becker goto bail;
1417b657c95cSJoel Becker }
1418b657c95cSJoel Becker
1419b657c95cSJoel Becker rc = 0;
1420b657c95cSJoel Becker
1421b657c95cSJoel Becker bail:
1422b657c95cSJoel Becker return rc;
1423b657c95cSJoel Becker }
1424b657c95cSJoel Becker
ocfs2_filecheck_validate_inode_block(struct super_block * sb,struct buffer_head * bh)1425d56a8f32SGang He static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
1426d56a8f32SGang He struct buffer_head *bh)
1427d56a8f32SGang He {
1428d56a8f32SGang He int rc = 0;
1429d56a8f32SGang He struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1430d56a8f32SGang He
1431d56a8f32SGang He trace_ocfs2_filecheck_validate_inode_block(
1432d56a8f32SGang He (unsigned long long)bh->b_blocknr);
1433d56a8f32SGang He
1434d56a8f32SGang He BUG_ON(!buffer_uptodate(bh));
1435d56a8f32SGang He
1436d56a8f32SGang He /*
1437d56a8f32SGang He * Call ocfs2_validate_meta_ecc() first since it has ecc repair
1438d56a8f32SGang He * function, but we should not return error immediately when ecc
1439d56a8f32SGang He * validation fails, because the reason is quite likely the invalid
1440d56a8f32SGang He * inode number inputed.
1441d56a8f32SGang He */
1442d56a8f32SGang He rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
1443d56a8f32SGang He if (rc) {
1444d56a8f32SGang He mlog(ML_ERROR,
1445d56a8f32SGang He "Filecheck: checksum failed for dinode %llu\n",
1446d56a8f32SGang He (unsigned long long)bh->b_blocknr);
1447d56a8f32SGang He rc = -OCFS2_FILECHECK_ERR_BLOCKECC;
1448d56a8f32SGang He }
1449d56a8f32SGang He
1450d56a8f32SGang He if (!OCFS2_IS_VALID_DINODE(di)) {
1451d56a8f32SGang He mlog(ML_ERROR,
1452d56a8f32SGang He "Filecheck: invalid dinode #%llu: signature = %.*s\n",
1453d56a8f32SGang He (unsigned long long)bh->b_blocknr, 7, di->i_signature);
1454d56a8f32SGang He rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
1455d56a8f32SGang He goto bail;
1456d56a8f32SGang He } else if (rc)
1457d56a8f32SGang He goto bail;
1458d56a8f32SGang He
1459d56a8f32SGang He if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1460d56a8f32SGang He mlog(ML_ERROR,
1461d56a8f32SGang He "Filecheck: invalid dinode #%llu: i_blkno is %llu\n",
1462d56a8f32SGang He (unsigned long long)bh->b_blocknr,
1463d56a8f32SGang He (unsigned long long)le64_to_cpu(di->i_blkno));
1464d56a8f32SGang He rc = -OCFS2_FILECHECK_ERR_BLOCKNO;
1465d56a8f32SGang He goto bail;
1466d56a8f32SGang He }
1467d56a8f32SGang He
1468d56a8f32SGang He if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1469d56a8f32SGang He mlog(ML_ERROR,
1470d56a8f32SGang He "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL "
1471d56a8f32SGang He "not set\n",
1472d56a8f32SGang He (unsigned long long)bh->b_blocknr);
1473d56a8f32SGang He rc = -OCFS2_FILECHECK_ERR_VALIDFLAG;
1474d56a8f32SGang He goto bail;
1475d56a8f32SGang He }
1476d56a8f32SGang He
1477d56a8f32SGang He if (le32_to_cpu(di->i_fs_generation) !=
1478d56a8f32SGang He OCFS2_SB(sb)->fs_generation) {
1479d56a8f32SGang He mlog(ML_ERROR,
1480d56a8f32SGang He "Filecheck: invalid dinode #%llu: fs_generation is %u\n",
1481d56a8f32SGang He (unsigned long long)bh->b_blocknr,
1482d56a8f32SGang He le32_to_cpu(di->i_fs_generation));
1483d56a8f32SGang He rc = -OCFS2_FILECHECK_ERR_GENERATION;
1484d56a8f32SGang He }
1485d56a8f32SGang He
1486d56a8f32SGang He bail:
1487d56a8f32SGang He return rc;
1488d56a8f32SGang He }
1489d56a8f32SGang He
ocfs2_filecheck_repair_inode_block(struct super_block * sb,struct buffer_head * bh)1490d56a8f32SGang He static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
1491d56a8f32SGang He struct buffer_head *bh)
1492d56a8f32SGang He {
1493d56a8f32SGang He int changed = 0;
1494d56a8f32SGang He struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1495d56a8f32SGang He
1496d56a8f32SGang He if (!ocfs2_filecheck_validate_inode_block(sb, bh))
1497d56a8f32SGang He return 0;
1498d56a8f32SGang He
1499d56a8f32SGang He trace_ocfs2_filecheck_repair_inode_block(
1500d56a8f32SGang He (unsigned long long)bh->b_blocknr);
1501d56a8f32SGang He
1502d56a8f32SGang He if (ocfs2_is_hard_readonly(OCFS2_SB(sb)) ||
1503d56a8f32SGang He ocfs2_is_soft_readonly(OCFS2_SB(sb))) {
1504d56a8f32SGang He mlog(ML_ERROR,
1505d56a8f32SGang He "Filecheck: cannot repair dinode #%llu "
1506d56a8f32SGang He "on readonly filesystem\n",
1507d56a8f32SGang He (unsigned long long)bh->b_blocknr);
1508d56a8f32SGang He return -OCFS2_FILECHECK_ERR_READONLY;
1509d56a8f32SGang He }
1510d56a8f32SGang He
1511d56a8f32SGang He if (buffer_jbd(bh)) {
1512d56a8f32SGang He mlog(ML_ERROR,
1513d56a8f32SGang He "Filecheck: cannot repair dinode #%llu, "
1514d56a8f32SGang He "its buffer is in jbd\n",
1515d56a8f32SGang He (unsigned long long)bh->b_blocknr);
1516d56a8f32SGang He return -OCFS2_FILECHECK_ERR_INJBD;
1517d56a8f32SGang He }
1518d56a8f32SGang He
1519d56a8f32SGang He if (!OCFS2_IS_VALID_DINODE(di)) {
1520d56a8f32SGang He /* Cannot fix invalid inode block */
1521d56a8f32SGang He return -OCFS2_FILECHECK_ERR_INVALIDINO;
1522d56a8f32SGang He }
1523d56a8f32SGang He
1524d56a8f32SGang He if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1525d56a8f32SGang He /* Cannot just add VALID_FL flag back as a fix,
1526d56a8f32SGang He * need more things to check here.
1527d56a8f32SGang He */
1528d56a8f32SGang He return -OCFS2_FILECHECK_ERR_VALIDFLAG;
1529d56a8f32SGang He }
1530d56a8f32SGang He
1531d56a8f32SGang He if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1532d56a8f32SGang He di->i_blkno = cpu_to_le64(bh->b_blocknr);
1533d56a8f32SGang He changed = 1;
1534d56a8f32SGang He mlog(ML_ERROR,
1535d56a8f32SGang He "Filecheck: reset dinode #%llu: i_blkno to %llu\n",
1536d56a8f32SGang He (unsigned long long)bh->b_blocknr,
1537d56a8f32SGang He (unsigned long long)le64_to_cpu(di->i_blkno));
1538d56a8f32SGang He }
1539d56a8f32SGang He
1540d56a8f32SGang He if (le32_to_cpu(di->i_fs_generation) !=
1541d56a8f32SGang He OCFS2_SB(sb)->fs_generation) {
1542d56a8f32SGang He di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1543d56a8f32SGang He changed = 1;
1544d56a8f32SGang He mlog(ML_ERROR,
1545d56a8f32SGang He "Filecheck: reset dinode #%llu: fs_generation to %u\n",
1546d56a8f32SGang He (unsigned long long)bh->b_blocknr,
1547d56a8f32SGang He le32_to_cpu(di->i_fs_generation));
1548d56a8f32SGang He }
1549d56a8f32SGang He
1550d56a8f32SGang He if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) {
1551d56a8f32SGang He ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check);
1552d56a8f32SGang He mark_buffer_dirty(bh);
1553d56a8f32SGang He mlog(ML_ERROR,
1554d56a8f32SGang He "Filecheck: reset dinode #%llu: compute meta ecc\n",
1555d56a8f32SGang He (unsigned long long)bh->b_blocknr);
1556d56a8f32SGang He }
1557d56a8f32SGang He
1558d56a8f32SGang He return 0;
1559d56a8f32SGang He }
1560d56a8f32SGang He
1561d56a8f32SGang He static int
ocfs2_filecheck_read_inode_block_full(struct inode * inode,struct buffer_head ** bh,int flags,int type)1562d56a8f32SGang He ocfs2_filecheck_read_inode_block_full(struct inode *inode,
1563d56a8f32SGang He struct buffer_head **bh,
1564d56a8f32SGang He int flags, int type)
1565d56a8f32SGang He {
1566d56a8f32SGang He int rc;
1567d56a8f32SGang He struct buffer_head *tmp = *bh;
1568d56a8f32SGang He
1569d56a8f32SGang He if (!type) /* Check inode block */
1570d56a8f32SGang He rc = ocfs2_read_blocks(INODE_CACHE(inode),
1571d56a8f32SGang He OCFS2_I(inode)->ip_blkno,
1572d56a8f32SGang He 1, &tmp, flags,
1573d56a8f32SGang He ocfs2_filecheck_validate_inode_block);
1574d56a8f32SGang He else /* Repair inode block */
1575d56a8f32SGang He rc = ocfs2_read_blocks(INODE_CACHE(inode),
1576d56a8f32SGang He OCFS2_I(inode)->ip_blkno,
1577d56a8f32SGang He 1, &tmp, flags,
1578d56a8f32SGang He ocfs2_filecheck_repair_inode_block);
1579d56a8f32SGang He
1580d56a8f32SGang He /* If ocfs2_read_blocks() got us a new bh, pass it up. */
1581d56a8f32SGang He if (!rc && !*bh)
1582d56a8f32SGang He *bh = tmp;
1583d56a8f32SGang He
1584d56a8f32SGang He return rc;
1585d56a8f32SGang He }
1586d56a8f32SGang He
ocfs2_read_inode_block_full(struct inode * inode,struct buffer_head ** bh,int flags)1587b657c95cSJoel Becker int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
1588b657c95cSJoel Becker int flags)
1589b657c95cSJoel Becker {
1590b657c95cSJoel Becker int rc;
1591b657c95cSJoel Becker struct buffer_head *tmp = *bh;
1592b657c95cSJoel Becker
15938cb471e8SJoel Becker rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
15948cb471e8SJoel Becker 1, &tmp, flags, ocfs2_validate_inode_block);
1595b657c95cSJoel Becker
1596b657c95cSJoel Becker /* If ocfs2_read_blocks() got us a new bh, pass it up. */
1597970e4936SJoel Becker if (!rc && !*bh)
1598b657c95cSJoel Becker *bh = tmp;
1599b657c95cSJoel Becker
1600b657c95cSJoel Becker return rc;
1601b657c95cSJoel Becker }
1602b657c95cSJoel Becker
ocfs2_read_inode_block(struct inode * inode,struct buffer_head ** bh)1603b657c95cSJoel Becker int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
1604b657c95cSJoel Becker {
1605b657c95cSJoel Becker return ocfs2_read_inode_block_full(inode, bh, 0);
1606b657c95cSJoel Becker }
16076e5a3d75SJoel Becker
16086e5a3d75SJoel Becker
ocfs2_inode_cache_owner(struct ocfs2_caching_info * ci)16096e5a3d75SJoel Becker static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
16106e5a3d75SJoel Becker {
16116e5a3d75SJoel Becker struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
16126e5a3d75SJoel Becker
16136e5a3d75SJoel Becker return oi->ip_blkno;
16146e5a3d75SJoel Becker }
16156e5a3d75SJoel Becker
ocfs2_inode_cache_get_super(struct ocfs2_caching_info * ci)16168cb471e8SJoel Becker static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
16178cb471e8SJoel Becker {
16188cb471e8SJoel Becker struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
16198cb471e8SJoel Becker
16208cb471e8SJoel Becker return oi->vfs_inode.i_sb;
16218cb471e8SJoel Becker }
16228cb471e8SJoel Becker
ocfs2_inode_cache_lock(struct ocfs2_caching_info * ci)16236e5a3d75SJoel Becker static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
16246e5a3d75SJoel Becker {
16256e5a3d75SJoel Becker struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
16266e5a3d75SJoel Becker
16276e5a3d75SJoel Becker spin_lock(&oi->ip_lock);
16286e5a3d75SJoel Becker }
16296e5a3d75SJoel Becker
ocfs2_inode_cache_unlock(struct ocfs2_caching_info * ci)16306e5a3d75SJoel Becker static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
16316e5a3d75SJoel Becker {
16326e5a3d75SJoel Becker struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
16336e5a3d75SJoel Becker
16346e5a3d75SJoel Becker spin_unlock(&oi->ip_lock);
16356e5a3d75SJoel Becker }
16366e5a3d75SJoel Becker
ocfs2_inode_cache_io_lock(struct ocfs2_caching_info * ci)16376e5a3d75SJoel Becker static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
16386e5a3d75SJoel Becker {
16396e5a3d75SJoel Becker struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
16406e5a3d75SJoel Becker
16416e5a3d75SJoel Becker mutex_lock(&oi->ip_io_mutex);
16426e5a3d75SJoel Becker }
16436e5a3d75SJoel Becker
ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info * ci)16446e5a3d75SJoel Becker static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
16456e5a3d75SJoel Becker {
16466e5a3d75SJoel Becker struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
16476e5a3d75SJoel Becker
16486e5a3d75SJoel Becker mutex_unlock(&oi->ip_io_mutex);
16496e5a3d75SJoel Becker }
16506e5a3d75SJoel Becker
16516e5a3d75SJoel Becker const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
16526e5a3d75SJoel Becker .co_owner = ocfs2_inode_cache_owner,
16538cb471e8SJoel Becker .co_get_super = ocfs2_inode_cache_get_super,
16546e5a3d75SJoel Becker .co_cache_lock = ocfs2_inode_cache_lock,
16556e5a3d75SJoel Becker .co_cache_unlock = ocfs2_inode_cache_unlock,
16566e5a3d75SJoel Becker .co_io_lock = ocfs2_inode_cache_io_lock,
16576e5a3d75SJoel Becker .co_io_unlock = ocfs2_inode_cache_io_unlock,
16586e5a3d75SJoel Becker };
16596e5a3d75SJoel Becker
1660