Blame view

fs/ocfs2/inode.c 39.4 KB
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  /* -*- mode: c; c-basic-offset: 8; -*-
   * vim: noexpandtab sw=8 ts=8 sts=0:
   *
   * inode.c
   *
   * vfs' aops, fops, dops and iops
   *
   * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License as published by the Free Software Foundation; either
   * version 2 of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public
   * License along with this program; if not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 021110-1307, USA.
   */
  
  #include <linux/fs.h>
  #include <linux/types.h>
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
28
29
  #include <linux/highmem.h>
  #include <linux/pagemap.h>
a90714c15   Jan Kara   ocfs2: Add quota ...
30
  #include <linux/quotaops.h>
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
31
32
  
  #include <asm/byteorder.h>
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
33
34
35
36
37
  #include <cluster/masklog.h>
  
  #include "ocfs2.h"
  
  #include "alloc.h"
9b7895efa   Mark Fasheh   ocfs2: Add a name...
38
  #include "dir.h"
d6b32bbb3   Joel Becker   ocfs2: block read...
39
  #include "blockcheck.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
40
41
42
  #include "dlmglue.h"
  #include "extent_map.h"
  #include "file.h"
b4df6ed8d   Mark Fasheh   [PATCH] ocfs2: fi...
43
  #include "heartbeat.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
44
45
46
47
48
49
50
51
  #include "inode.h"
  #include "journal.h"
  #include "namei.h"
  #include "suballoc.h"
  #include "super.h"
  #include "symlink.h"
  #include "sysfile.h"
  #include "uptodate.h"
cf1d6c763   Tiger Yang   ocfs2: Add extend...
52
  #include "xattr.h"
8b2c0dba5   Tao Ma   ocfs2: Call refco...
53
  #include "refcounttree.h"
64f3b2692   Tao Ma   ocfs2: Remove mas...
54
  #include "ocfs2_trace.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
55
56
  
  #include "buffer_head_io.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
57
58
59
60
61
  struct ocfs2_find_inode_args
  {
  	u64		fi_blkno;
  	unsigned long	fi_ino;
  	unsigned int	fi_flags;
5fa0613ea   Jan Kara   ocfs2: Silence fa...
62
  	unsigned int	fi_sysfile_type;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
63
  };
5fa0613ea   Jan Kara   ocfs2: Silence fa...
64
  static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
65
66
67
68
69
70
71
  static int ocfs2_read_locked_inode(struct inode *inode,
  				   struct ocfs2_find_inode_args *args);
  static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
  static int ocfs2_find_actor(struct inode *inode, void *opaque);
  static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  				    struct inode *inode,
  				    struct buffer_head *fe_bh);
ca4d147e6   Herbert Poetzl   ocfs2: add ext2 a...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  void ocfs2_set_inode_flags(struct inode *inode)
  {
  	unsigned int flags = OCFS2_I(inode)->ip_attr;
  
  	inode->i_flags &= ~(S_IMMUTABLE |
  		S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
  
  	if (flags & OCFS2_IMMUTABLE_FL)
  		inode->i_flags |= S_IMMUTABLE;
  
  	if (flags & OCFS2_SYNC_FL)
  		inode->i_flags |= S_SYNC;
  	if (flags & OCFS2_APPEND_FL)
  		inode->i_flags |= S_APPEND;
  	if (flags & OCFS2_NOATIME_FL)
  		inode->i_flags |= S_NOATIME;
  	if (flags & OCFS2_DIRSYNC_FL)
  		inode->i_flags |= S_DIRSYNC;
  }
6e4b0d569   Jan Kara   [PATCH] Copy i_fl...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
  void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
  {
  	unsigned int flags = oi->vfs_inode.i_flags;
  
  	oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
  			OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
  	if (flags & S_SYNC)
  		oi->ip_attr |= OCFS2_SYNC_FL;
  	if (flags & S_APPEND)
  		oi->ip_attr |= OCFS2_APPEND_FL;
  	if (flags & S_IMMUTABLE)
  		oi->ip_attr |= OCFS2_IMMUTABLE_FL;
  	if (flags & S_NOATIME)
  		oi->ip_attr |= OCFS2_NOATIME_FL;
  	if (flags & S_DIRSYNC)
  		oi->ip_attr |= OCFS2_DIRSYNC_FL;
  }
6ca497a83   wengang wang   ocfs2: fix rare s...
109
110
111
112
113
114
115
116
117
118
119
  struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno)
  {
  	struct ocfs2_find_inode_args args;
  
  	args.fi_blkno = blkno;
  	args.fi_flags = 0;
  	args.fi_ino = ino_from_blkno(sb, blkno);
  	args.fi_sysfile_type = 0;
  
  	return ilookup5(sb, blkno, ocfs2_find_actor, &args);
  }
5fa0613ea   Jan Kara   ocfs2: Silence fa...
120
121
  struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
  			 int sysfile_type)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
122
123
124
125
  {
  	struct inode *inode = NULL;
  	struct super_block *sb = osb->sb;
  	struct ocfs2_find_inode_args args;
64f3b2692   Tao Ma   ocfs2: Remove mas...
126
127
  	trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
  			       sysfile_type);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
128
129
130
131
132
133
134
135
136
137
138
  
  	/* Ok. By now we've either got the offsets passed to us by the
  	 * caller, or we just pulled them off the bh. Lets do some
  	 * sanity checks to make sure they're OK. */
  	if (blkno == 0) {
  		inode = ERR_PTR(-EINVAL);
  		mlog_errno(PTR_ERR(inode));
  		goto bail;
  	}
  
  	args.fi_blkno = blkno;
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
139
  	args.fi_flags = flags;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
140
  	args.fi_ino = ino_from_blkno(sb, blkno);
5fa0613ea   Jan Kara   ocfs2: Silence fa...
141
  	args.fi_sysfile_type = sysfile_type;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
142
143
144
145
146
147
  
  	inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
  			     ocfs2_init_locked_inode, &args);
  	/* inode was *not* in the inode cache. 2.6.x requires
  	 * us to do our own read_inode call and unlock it
  	 * afterwards. */
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
148
149
150
151
152
  	if (inode == NULL) {
  		inode = ERR_PTR(-ENOMEM);
  		mlog_errno(PTR_ERR(inode));
  		goto bail;
  	}
64f3b2692   Tao Ma   ocfs2: Remove mas...
153
  	trace_ocfs2_iget5_locked(inode->i_state);
6218b90e7   Tao Ma   ocfs2: Little ref...
154
  	if (inode->i_state & I_NEW) {
6218b90e7   Tao Ma   ocfs2: Little ref...
155
156
157
  		ocfs2_read_locked_inode(inode, &args);
  		unlock_new_inode(inode);
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
158
159
160
  	if (is_bad_inode(inode)) {
  		iput(inode);
  		inode = ERR_PTR(-ESTALE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
161
162
163
164
165
  		goto bail;
  	}
  
  bail:
  	if (!IS_ERR(inode)) {
64f3b2692   Tao Ma   ocfs2: Remove mas...
166
167
  		trace_ocfs2_iget_end(inode, 
  			(unsigned long long)OCFS2_I(inode)->ip_blkno);
6a1bd4a57   Mark Fasheh   ocfs2: cleanup oc...
168
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  
  	return inode;
  }
  
  
  /*
   * here's how inodes get read from disk:
   * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
   * found? : return the in-memory inode
   * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
   */
  
  static int ocfs2_find_actor(struct inode *inode, void *opaque)
  {
  	struct ocfs2_find_inode_args *args = NULL;
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
  	int ret = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
186
187
188
189
  	args = opaque;
  
  	mlog_bug_on_msg(!inode, "No inode in find actor!
  ");
64f3b2692   Tao Ma   ocfs2: Remove mas...
190
  	trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
191
192
  	if (oi->ip_blkno != args->fi_blkno)
  		goto bail;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
193
194
  	ret = 1;
  bail:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
195
196
197
198
199
200
201
202
203
204
205
  	return ret;
  }
  
  /*
   * initialize the new inode, but don't do anything that would cause
   * us to sleep.
   * return 0 on success, 1 on failure
   */
  static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
  {
  	struct ocfs2_find_inode_args *args = opaque;
cb25797d4   Jan Kara   ocfs2: Add lockde...
206
207
  	static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
  				     ocfs2_file_ip_alloc_sem_key;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
208

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
209
210
  	inode->i_ino = args->fi_ino;
  	OCFS2_I(inode)->ip_blkno = args->fi_blkno;
5fa0613ea   Jan Kara   ocfs2: Silence fa...
211
212
213
  	if (args->fi_sysfile_type != 0)
  		lockdep_set_class(&inode->i_mutex,
  			&ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
cb25797d4   Jan Kara   ocfs2: Add lockde...
214
215
216
217
218
219
220
221
222
  	if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE ||
  	    args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE ||
  	    args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
  	    args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE)
  		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
  				  &ocfs2_quota_ip_alloc_sem_key);
  	else
  		lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
  				  &ocfs2_file_ip_alloc_sem_key);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
223

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
224
225
  	return 0;
  }
b657c95c1   Joel Becker   ocfs2: Wrap inode...
226
227
  void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
  			  int create_ino)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
228
229
230
  {
  	struct super_block *sb;
  	struct ocfs2_super *osb;
53da4939f   Mark Fasheh   ocfs2: POSIX file...
231
  	int use_plocks = 1;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
232

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
233
234
  	sb = inode->i_sb;
  	osb = OCFS2_SB(sb);
53da4939f   Mark Fasheh   ocfs2: POSIX file...
235
236
237
  	if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
  	    ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
  		use_plocks = 0;
b657c95c1   Joel Becker   ocfs2: Wrap inode...
238
239
240
241
242
243
244
245
246
247
  	/*
  	 * These have all been checked by ocfs2_read_inode_block() or set
  	 * by ocfs2_mknod_locked(), so a failure is a code bug.
  	 */
  	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));  /* This means that read_inode
  						cannot create a superblock
  						inode today.  change if
  						that is needed. */
  	BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
  	BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
248

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
249

8110b073a   Mark Fasheh   ocfs2: Fix up i_b...
250
251
  	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
15b1e36bd   Mark Fasheh   ocfs2: Structure ...
252
  	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
8110b073a   Mark Fasheh   ocfs2: Fix up i_b...
253

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
254
255
256
257
258
259
  	inode->i_version = 1;
  	inode->i_generation = le32_to_cpu(fe->i_generation);
  	inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  	inode->i_mode = le16_to_cpu(fe->i_mode);
  	inode->i_uid = le32_to_cpu(fe->i_uid);
  	inode->i_gid = le32_to_cpu(fe->i_gid);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
260
261
262
263
264
  
  	/* Fast symlinks will have i_size but no allocated clusters. */
  	if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
  		inode->i_blocks = 0;
  	else
8110b073a   Mark Fasheh   ocfs2: Fix up i_b...
265
  		inode->i_blocks = ocfs2_inode_sector_count(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
266
  	inode->i_mapping->a_ops = &ocfs2_aops;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
267
268
269
270
271
272
273
274
275
  	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  
  	if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
  		mlog(ML_ERROR,
b0697053f   Mark Fasheh   ocfs2: don't use ...
276
277
278
  		     "ip_blkno %llu != i_blkno %llu!
  ",
  		     (unsigned long long)OCFS2_I(inode)->ip_blkno,
1ca1a111b   Mark Fasheh   ocfs2: fix sparse...
279
  		     (unsigned long long)le64_to_cpu(fe->i_blkno));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
280

bfe868486   Miklos Szeredi   filesystems: add ...
281
  	set_nlink(inode, ocfs2_read_links_count(fe));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
282

64f3b2692   Tao Ma   ocfs2: Remove mas...
283
284
  	trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno,
  				   le32_to_cpu(fe->i_flags));
bbbd0eb34   Jan Kara   ocfs2: Mark syste...
285
  	if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
286
  		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
bbbd0eb34   Jan Kara   ocfs2: Mark syste...
287
288
  		inode->i_flags |= S_NOQUOTA;
  	}
64f3b2692   Tao Ma   ocfs2: Remove mas...
289
    
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
290
291
  	if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
  		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
292
293
  	} else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
  		OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
1a224ad11   Jan Kara   ocfs2: Assign fea...
294
295
  	} else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
  		inode->i_flags |= S_NOQUOTA;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
296
  	} else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
297
298
299
300
301
302
303
  		/* we can't actually hit this as read_inode can't
  		 * handle superblocks today ;-) */
  		BUG();
  	}
  
  	switch (inode->i_mode & S_IFMT) {
  	    case S_IFREG:
53da4939f   Mark Fasheh   ocfs2: POSIX file...
304
305
306
307
  		    if (use_plocks)
  			    inode->i_fop = &ocfs2_fops;
  		    else
  			    inode->i_fop = &ocfs2_fops_no_plocks;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
308
309
310
311
312
  		    inode->i_op = &ocfs2_file_iops;
  		    i_size_write(inode, le64_to_cpu(fe->i_size));
  		    break;
  	    case S_IFDIR:
  		    inode->i_op = &ocfs2_dir_iops;
53da4939f   Mark Fasheh   ocfs2: POSIX file...
313
314
315
316
  		    if (use_plocks)
  			    inode->i_fop = &ocfs2_dops;
  		    else
  			    inode->i_fop = &ocfs2_dops_no_plocks;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
317
  		    i_size_write(inode, le64_to_cpu(fe->i_size));
5e98d4924   Goldwyn Rodrigues   Track negative en...
318
  		    OCFS2_I(inode)->ip_dir_lock_gen = 1;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  		    break;
  	    case S_IFLNK:
  		    if (ocfs2_inode_is_fast_symlink(inode))
  			inode->i_op = &ocfs2_fast_symlink_inode_operations;
  		    else
  			inode->i_op = &ocfs2_symlink_inode_operations;
  		    i_size_write(inode, le64_to_cpu(fe->i_size));
  		    break;
  	    default:
  		    inode->i_op = &ocfs2_special_file_iops;
  		    init_special_inode(inode, inode->i_mode,
  				       inode->i_rdev);
  		    break;
  	}
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
333
334
335
336
337
338
339
340
341
  	if (create_ino) {
  		inode->i_ino = ino_from_blkno(inode->i_sb,
  			       le64_to_cpu(fe->i_blkno));
  
  		/*
  		 * If we ever want to create system files from kernel,
  		 * the generation argument to
  		 * ocfs2_inode_lock_res_init() will have to change.
  		 */
1ca1a111b   Mark Fasheh   ocfs2: fix sparse...
342
  		BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
343

e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
344
  		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
345
  					  OCFS2_LOCK_TYPE_META, 0, inode);
500086300   Tiger Yang   ocfs2: Remove del...
346
347
348
  
  		ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  					  OCFS2_LOCK_TYPE_OPEN, 0, inode);
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
349
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
350
  	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
351
352
  				  OCFS2_LOCK_TYPE_RW, inode->i_generation,
  				  inode);
ca4d147e6   Herbert Poetzl   ocfs2: add ext2 a...
353
  	ocfs2_set_inode_flags(inode);
ca4d147e6   Herbert Poetzl   ocfs2: add ext2 a...
354

138211515   Tao Ma   ocfs2: Optimize i...
355
356
  	OCFS2_I(inode)->ip_last_used_slot = 0;
  	OCFS2_I(inode)->ip_last_used_group = 0;
e3b4a97db   Mark Fasheh   ocfs2: use alloca...
357
358
359
360
  
  	if (S_ISDIR(inode->i_mode))
  		ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv,
  				    OCFS2_RESV_FLAG_DIR);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
361
362
363
364
365
366
367
368
369
  }
  
  static int ocfs2_read_locked_inode(struct inode *inode,
  				   struct ocfs2_find_inode_args *args)
  {
  	struct super_block *sb;
  	struct ocfs2_super *osb;
  	struct ocfs2_dinode *fe;
  	struct buffer_head *bh = NULL;
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
370
371
  	int status, can_lock;
  	u32 generation = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
372

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
373
374
375
376
  	status = -EINVAL;
  	if (inode == NULL || inode->i_sb == NULL) {
  		mlog(ML_ERROR, "bad inode
  ");
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
377
  		return status;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
378
379
380
381
382
383
384
385
  	}
  	sb = inode->i_sb;
  	osb = OCFS2_SB(sb);
  
  	if (!args) {
  		mlog(ML_ERROR, "bad inode args
  ");
  		make_bad_inode(inode);
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
  		return status;
  	}
  
  	/*
  	 * To improve performance of cold-cache inode stats, we take
  	 * the cluster lock here if possible.
  	 *
  	 * Generally, OCFS2 never trusts the contents of an inode
  	 * unless it's holding a cluster lock, so taking it here isn't
  	 * a correctness issue as much as it is a performance
  	 * improvement.
  	 *
  	 * There are three times when taking the lock is not a good idea:
  	 *
  	 * 1) During startup, before we have initialized the DLM.
  	 *
  	 * 2) If we are reading certain system files which never get
  	 *    cluster locks (local alloc, truncate log).
  	 *
  	 * 3) If the process doing the iget() is responsible for
  	 *    orphan dir recovery. We're holding the orphan dir lock and
  	 *    can get into a deadlock with another process on another
  	 *    node in ->delete_inode().
  	 *
  	 * #1 and #2 can be simply solved by never taking the lock
  	 * here for system files (which are the only type we read
  	 * during mount). It's a heavier approach, but our main
b595076a1   Uwe Kleine-König   tree-wide: fix co...
413
  	 * concern is user-accessible files anyway.
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
414
415
416
417
418
  	 *
  	 * #3 works itself out because we'll eventually take the
  	 * cluster lock before trusting anything anyway.
  	 */
  	can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
500086300   Tiger Yang   ocfs2: Remove del...
419
  		&& !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
c271c5c22   Sunil Mushran   ocfs2: local mounts
420
  		&& !ocfs2_mount_local(osb);
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
421

64f3b2692   Tao Ma   ocfs2: Remove mas...
422
423
  	trace_ocfs2_read_locked_inode(
  		(unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock);
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
424
425
426
427
428
429
430
431
432
  	/*
  	 * To maintain backwards compatibility with older versions of
  	 * ocfs2-tools, we still store the generation value for system
  	 * files. The only ones that actually matter to userspace are
  	 * the journals, but it's easier and inexpensive to just flag
  	 * all system files similarly.
  	 */
  	if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  		generation = osb->fs_generation;
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
433
  	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
434
435
  				  OCFS2_LOCK_TYPE_META,
  				  generation, inode);
500086300   Tiger Yang   ocfs2: Remove del...
436
437
438
  	ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  				  OCFS2_LOCK_TYPE_OPEN,
  				  0, inode);
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
439
  	if (can_lock) {
500086300   Tiger Yang   ocfs2: Remove del...
440
441
442
443
444
445
  		status = ocfs2_open_lock(inode);
  		if (status) {
  			make_bad_inode(inode);
  			mlog_errno(status);
  			return status;
  		}
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
446
  		status = ocfs2_inode_lock(inode, NULL, 0);
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
447
448
449
450
451
  		if (status) {
  			make_bad_inode(inode);
  			mlog_errno(status);
  			return status;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
452
  	}
500086300   Tiger Yang   ocfs2: Remove del...
453
454
455
  	if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
  		status = ocfs2_try_open_lock(inode, 0);
  		if (status) {
2bd632165   Sunil Mushran   ocfs2/trivial: Re...
456
  			make_bad_inode(inode);
500086300   Tiger Yang   ocfs2: Remove del...
457
458
459
  			return status;
  		}
  	}
b657c95c1   Joel Becker   ocfs2: Wrap inode...
460
461
462
463
  	if (can_lock) {
  		status = ocfs2_read_inode_block_full(inode, &bh,
  						     OCFS2_BH_IGNORE_CACHE);
  	} else {
da1e90985   Joel Becker   ocfs2: Separate o...
464
  		status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
f5ce5a08a   Sunil Mushran   ocfs2: Fix incorr...
465
466
467
468
469
  		/*
  		 * If buffer is in jbd, then its checksum may not have been
  		 * computed as yet.
  		 */
  		if (!status && !buffer_jbd(bh))
b657c95c1   Joel Becker   ocfs2: Wrap inode...
470
471
  			status = ocfs2_validate_inode_block(osb->sb, bh);
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
472
473
  	if (status < 0) {
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
474
475
  		goto bail;
  	}
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
476
  	status = -EINVAL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
477
  	fe = (struct ocfs2_dinode *) bh->b_data;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
478

24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
479
480
481
482
483
484
485
486
487
488
  	/*
  	 * This is a code bug. Right now the caller needs to
  	 * understand whether it is asking for a system file inode or
  	 * not so the proper lock names can be built.
  	 */
  	mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
  			!!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
  			"Inode %llu: system file state is ambigous
  ",
  			(unsigned long long)args->fi_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
489
490
491
  
  	if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
  	    S_ISBLK(le16_to_cpu(fe->i_mode)))
b657c95c1   Joel Becker   ocfs2: Wrap inode...
492
  		inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
493

b657c95c1   Joel Becker   ocfs2: Wrap inode...
494
  	ocfs2_populate_inode(inode, fe, 0);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
495
496
  
  	BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
497
498
499
  	status = 0;
  
  bail:
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
500
  	if (can_lock)
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
501
  		ocfs2_inode_unlock(inode, 0);
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
502
503
504
  
  	if (status < 0)
  		make_bad_inode(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
505
506
  	if (args && bh)
  		brelse(bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
507
508
509
510
511
512
513
514
515
516
517
518
519
  	return status;
  }
  
  void ocfs2_sync_blockdev(struct super_block *sb)
  {
  	sync_blockdev(sb->s_bdev);
  }
  
  static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  				     struct inode *inode,
  				     struct buffer_head *fe_bh)
  {
  	int status = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
520
  	struct ocfs2_dinode *fe;
60b11392f   Mark Fasheh   ocfs2: zero tail ...
521
  	handle_t *handle = NULL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
522

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
523
  	fe = (struct ocfs2_dinode *) fe_bh->b_data;
1afc32b95   Mark Fasheh   ocfs2: Write supp...
524
525
526
527
  	/*
  	 * This check will also skip truncate of inodes with inline
  	 * data and fast symlinks.
  	 */
3a0782d09   Mark Fasheh   ocfs2: teach exte...
528
  	if (fe->i_clusters) {
2b4e30fbd   Joel Becker   ocfs2: Switch ove...
529
530
  		if (ocfs2_should_order_data(inode))
  			ocfs2_begin_ordered_truncate(inode, 0);
60b11392f   Mark Fasheh   ocfs2: zero tail ...
531
532
533
  		handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  		if (IS_ERR(handle)) {
  			status = PTR_ERR(handle);
0350cb078   Dan Carpenter   ocfs2: potential ...
534
  			handle = NULL;
60b11392f   Mark Fasheh   ocfs2: zero tail ...
535
536
537
  			mlog_errno(status);
  			goto out;
  		}
0cf2f7632   Joel Becker   ocfs2: Pass struc...
538
539
  		status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  						 fe_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
540
  						 OCFS2_JOURNAL_ACCESS_WRITE);
60b11392f   Mark Fasheh   ocfs2: zero tail ...
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
  		if (status < 0) {
  			mlog_errno(status);
  			goto out;
  		}
  
  		i_size_write(inode, 0);
  
  		status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  		if (status < 0) {
  			mlog_errno(status);
  			goto out;
  		}
  
  		ocfs2_commit_trans(osb, handle);
  		handle = NULL;
78f94673d   Tristan Ye   Ocfs2: Optimize o...
556
  		status = ocfs2_commit_truncate(osb, inode, fe_bh);
3a0782d09   Mark Fasheh   ocfs2: teach exte...
557
558
559
560
  		if (status < 0) {
  			mlog_errno(status);
  			goto out;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
561
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
562

60b11392f   Mark Fasheh   ocfs2: zero tail ...
563
564
565
  out:
  	if (handle)
  		ocfs2_commit_trans(osb, handle);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
566
567
568
569
570
571
572
573
574
575
576
  	return status;
  }
  
  static int ocfs2_remove_inode(struct inode *inode,
  			      struct buffer_head *di_bh,
  			      struct inode *orphan_dir_inode,
  			      struct buffer_head *orphan_dir_bh)
  {
  	int status;
  	struct inode *inode_alloc_inode = NULL;
  	struct buffer_head *inode_alloc_bh = NULL;
1fabe1481   Mark Fasheh   ocfs2: Remove str...
577
  	handle_t *handle;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
578
579
580
581
582
583
584
585
586
587
588
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  
  	inode_alloc_inode =
  		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  					    le16_to_cpu(di->i_suballoc_slot));
  	if (!inode_alloc_inode) {
  		status = -EEXIST;
  		mlog_errno(status);
  		goto bail;
  	}
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
589
  	mutex_lock(&inode_alloc_inode->i_mutex);
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
590
  	status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
591
  	if (status < 0) {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
592
  		mutex_unlock(&inode_alloc_inode->i_mutex);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
593
594
595
596
  
  		mlog_errno(status);
  		goto bail;
  	}
a90714c15   Jan Kara   ocfs2: Add quota ...
597
  	handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
9b7895efa   Mark Fasheh   ocfs2: Add a name...
598
  				   ocfs2_quota_trans_credits(inode->i_sb));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
599
600
601
602
603
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		mlog_errno(status);
  		goto bail_unlock;
  	}
d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
604
605
606
607
608
609
610
  	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  		status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  					  orphan_dir_bh);
  		if (status < 0) {
  			mlog_errno(status);
  			goto bail_commit;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
611
612
613
  	}
  
  	/* set the inodes dtime */
0cf2f7632   Joel Becker   ocfs2: Pass struc...
614
  	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
615
  					 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
616
617
618
619
620
621
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail_commit;
  	}
  
  	di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
4092d49f7   Marcin Slusarz   ocfs2: convert by...
622
  	di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
623
  	ocfs2_journal_dirty(handle, di_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
624

8cb471e8f   Joel Becker   ocfs2: Take the i...
625
  	ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
63936ddaa   Christoph Hellwig   dquot: cleanup in...
626
  	dquot_free_inode(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
627
628
629
630
631
632
633
  
  	status = ocfs2_free_dinode(handle, inode_alloc_inode,
  				   inode_alloc_bh, di);
  	if (status < 0)
  		mlog_errno(status);
  
  bail_commit:
02dc1af44   Mark Fasheh   ocfs2: pass ocfs2...
634
  	ocfs2_commit_trans(osb, handle);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
635
  bail_unlock:
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
636
  	ocfs2_inode_unlock(inode_alloc_inode, 1);
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
637
  	mutex_unlock(&inode_alloc_inode->i_mutex);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
638
639
640
641
642
643
  	brelse(inode_alloc_bh);
  bail:
  	iput(inode_alloc_inode);
  
  	return status;
  }
2bd632165   Sunil Mushran   ocfs2/trivial: Re...
644
  /*
b4df6ed8d   Mark Fasheh   [PATCH] ocfs2: fi...
645
646
647
648
649
650
651
652
653
654
655
656
   * Serialize with orphan dir recovery. If the process doing
   * recovery on this orphan dir does an iget() with the dir
   * i_mutex held, we'll deadlock here. Instead we detect this
   * and exit early - recovery will wipe this inode for us.
   */
  static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
  					     int slot)
  {
  	int ret = 0;
  
  	spin_lock(&osb->osb_lock);
  	if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
b4df6ed8d   Mark Fasheh   [PATCH] ocfs2: fi...
657
658
659
660
661
662
663
664
  		ret = -EDEADLK;
  		goto out;
  	}
  	/* This signals to the orphan recovery process that it should
  	 * wait for us to handle the wipe. */
  	osb->osb_orphan_wipes[slot]++;
  out:
  	spin_unlock(&osb->osb_lock);
64f3b2692   Tao Ma   ocfs2: Remove mas...
665
  	trace_ocfs2_check_orphan_recovery_state(slot, ret);
b4df6ed8d   Mark Fasheh   [PATCH] ocfs2: fi...
666
667
668
669
670
671
672
673
674
675
676
677
  	return ret;
  }
  
  static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
  					 int slot)
  {
  	spin_lock(&osb->osb_lock);
  	osb->osb_orphan_wipes[slot]--;
  	spin_unlock(&osb->osb_lock);
  
  	wake_up(&osb->osb_wipe_event);
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
678
679
680
  static int ocfs2_wipe_inode(struct inode *inode,
  			    struct buffer_head *di_bh)
  {
d577632e6   Joel Becker   ocfs2: Avoid a gc...
681
  	int status, orphaned_slot = -1;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
682
683
684
  	struct inode *orphan_dir_inode = NULL;
  	struct buffer_head *orphan_dir_bh = NULL;
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
685
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
686

d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
687
688
  	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  		orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
b4df6ed8d   Mark Fasheh   [PATCH] ocfs2: fi...
689

d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
690
691
692
  		status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
  		if (status)
  			return status;
b4df6ed8d   Mark Fasheh   [PATCH] ocfs2: fi...
693

d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
694
695
696
697
698
699
700
701
  		orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  							       ORPHAN_DIR_SYSTEM_INODE,
  							       orphaned_slot);
  		if (!orphan_dir_inode) {
  			status = -EEXIST;
  			mlog_errno(status);
  			goto bail;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
702

d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
703
704
705
706
707
708
709
  		/* Lock the orphan dir. The lock will be held for the entire
  		 * delete_inode operation. We do this now to avoid races with
  		 * recovery completion on other nodes. */
  		mutex_lock(&orphan_dir_inode->i_mutex);
  		status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  		if (status < 0) {
  			mutex_unlock(&orphan_dir_inode->i_mutex);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
710

d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
711
712
713
  			mlog_errno(status);
  			goto bail;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
714
715
716
  	}
  
  	/* we do this while holding the orphan dir lock because we
34d024f84   Mark Fasheh   ocfs2: Remove mou...
717
718
  	 * don't want recovery being run from another node to try an
  	 * inode delete underneath us -- this will result in two nodes
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
719
720
721
722
723
724
  	 * truncating the same file! */
  	status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail_unlock_dir;
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
725
726
727
728
729
730
731
732
  	/* Remove any dir index tree */
  	if (S_ISDIR(inode->i_mode)) {
  		status = ocfs2_dx_dir_truncate(inode, di_bh);
  		if (status) {
  			mlog_errno(status);
  			goto bail_unlock_dir;
  		}
  	}
cf1d6c763   Tiger Yang   ocfs2: Add extend...
733
734
735
736
737
738
  	/*Free extended attribute resources associated with this inode.*/
  	status = ocfs2_xattr_remove(inode, di_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail_unlock_dir;
  	}
8b2c0dba5   Tao Ma   ocfs2: Call refco...
739
740
741
742
743
  	status = ocfs2_remove_refcount_tree(inode, di_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail_unlock_dir;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
744
745
746
747
748
749
  	status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  				    orphan_dir_bh);
  	if (status < 0)
  		mlog_errno(status);
  
  bail_unlock_dir:
d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
750
751
  	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
  		return status;
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
752
  	ocfs2_inode_unlock(orphan_dir_inode, 1);
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
753
  	mutex_unlock(&orphan_dir_inode->i_mutex);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
754
755
756
  	brelse(orphan_dir_bh);
  bail:
  	iput(orphan_dir_inode);
b4df6ed8d   Mark Fasheh   [PATCH] ocfs2: fi...
757
  	ocfs2_signal_wipe_completion(osb, orphaned_slot);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
758
759
760
761
762
  
  	return status;
  }
  
  /* There is a series of simple checks that should be done before a
34d024f84   Mark Fasheh   ocfs2: Remove mou...
763
   * trylock is even considered. Encapsulate those in this function. */
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
764
765
766
767
768
  static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  {
  	int ret = 0;
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
64f3b2692   Tao Ma   ocfs2: Remove mas...
769
770
771
  	trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
  					     (unsigned long long)oi->ip_blkno,
  					     oi->ip_flags);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
772
773
774
775
776
777
778
  	/* We shouldn't be getting here for the root directory
  	 * inode.. */
  	if (inode == osb->root_inode) {
  		mlog(ML_ERROR, "Skipping delete of root inode.
  ");
  		goto bail;
  	}
34d024f84   Mark Fasheh   ocfs2: Remove mou...
779
  	/* If we're coming from downconvert_thread we can't go into our own
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
780
781
782
783
  	 * voting [hello, deadlock city!], so unforuntately we just
  	 * have to skip deleting this guy. That's OK though because
  	 * the node who's doing the actual deleting should handle it
  	 * anyway. */
64f3b2692   Tao Ma   ocfs2: Remove mas...
784
  	if (current == osb->dc_task)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
785
  		goto bail;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
786
787
788
789
790
791
  
  	spin_lock(&oi->ip_lock);
  	/* OCFS2 *never* deletes system files. This should technically
  	 * never get here as system file inodes should always have a
  	 * positive link count. */
  	if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
b0697053f   Mark Fasheh   ocfs2: don't use ...
792
793
794
  		mlog(ML_ERROR, "Skipping delete of system file %llu
  ",
  		     (unsigned long long)oi->ip_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
795
796
  		goto bail_unlock;
  	}
34d024f84   Mark Fasheh   ocfs2: Remove mou...
797
798
  	/* If we have allowd wipe of this inode for another node, it
  	 * will be marked here so we can safely skip it. Recovery will
25985edce   Lucas De Marchi   Fix common misspe...
799
  	 * cleanup any inodes we might inadvertently skip here. */
64f3b2692   Tao Ma   ocfs2: Remove mas...
800
  	if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
801
  		goto bail_unlock;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
  
  	ret = 1;
  bail_unlock:
  	spin_unlock(&oi->ip_lock);
  bail:
  	return ret;
  }
  
  /* Query the cluster to determine whether we should wipe an inode from
   * disk or not.
   *
   * Requires the inode to have the cluster lock. */
  static int ocfs2_query_inode_wipe(struct inode *inode,
  				  struct buffer_head *di_bh,
  				  int *wipe)
  {
64f3b2692   Tao Ma   ocfs2: Remove mas...
818
  	int status = 0, reason = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
819
820
821
822
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
  	struct ocfs2_dinode *di;
  
  	*wipe = 0;
64f3b2692   Tao Ma   ocfs2: Remove mas...
823
824
  	trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
  					   inode->i_nlink);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
825
826
827
828
  	/* While we were waiting for the cluster lock in
  	 * ocfs2_delete_inode, another node might have asked to delete
  	 * the inode. Recheck our flags to catch this. */
  	if (!ocfs2_inode_is_valid_to_delete(inode)) {
64f3b2692   Tao Ma   ocfs2: Remove mas...
829
  		reason = 1;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
830
831
832
833
834
  		goto bail;
  	}
  
  	/* Now that we have an up to date inode, we can double check
  	 * the link count. */
64f3b2692   Tao Ma   ocfs2: Remove mas...
835
  	if (inode->i_nlink)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
836
  		goto bail;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
837
838
839
  
  	/* Do some basic inode verification... */
  	di = (struct ocfs2_dinode *) di_bh->b_data;
d4cd1871c   Li Dongyang   ocfs2: add OCFS2_...
840
841
  	if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
  	    !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
b54c2ca47   Tristan Ye   Ocfs2: Handle del...
842
843
844
845
846
847
848
849
850
  		/*
  		 * Inodes in the orphan dir must have ORPHANED_FL.  The only
  		 * inodes that come back out of the orphan dir are reflink
  		 * targets. A reflink target may be moved out of the orphan
  		 * dir between the time we scan the directory and the time we
  		 * process it. This would lead to HAS_REFCOUNT_FL being set but
  		 * ORPHANED_FL not.
  		 */
  		if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
64f3b2692   Tao Ma   ocfs2: Remove mas...
851
  			reason = 2;
b54c2ca47   Tristan Ye   Ocfs2: Handle del...
852
853
  			goto bail;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
854
855
856
  		/* for lack of a better error? */
  		status = -EEXIST;
  		mlog(ML_ERROR,
b0697053f   Mark Fasheh   ocfs2: don't use ...
857
  		     "Inode %llu (on-disk %llu) not orphaned! "
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
858
859
  		     "Disk flags  0x%x, inode flags 0x%x
  ",
b0697053f   Mark Fasheh   ocfs2: don't use ...
860
  		     (unsigned long long)oi->ip_blkno,
1ca1a111b   Mark Fasheh   ocfs2: fix sparse...
861
862
  		     (unsigned long long)le64_to_cpu(di->i_blkno),
  		     le32_to_cpu(di->i_flags), oi->ip_flags);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
863
864
865
866
867
868
869
870
871
  		goto bail;
  	}
  
  	/* has someone already deleted us?! baaad... */
  	if (di->i_dtime) {
  		status = -EEXIST;
  		mlog_errno(status);
  		goto bail;
  	}
6f16bf655   Mark Fasheh   ocfs2: small clea...
872
873
874
875
876
877
  	/*
  	 * This is how ocfs2 determines whether an inode is still live
  	 * within the cluster. Every node takes a shared read lock on
  	 * the inode open lock in ocfs2_read_locked_inode(). When we
  	 * get to ->delete_inode(), each node tries to convert it's
  	 * lock to an exclusive. Trylocks are serialized by the inode
25985edce   Lucas De Marchi   Fix common misspe...
878
  	 * meta data lock. If the upconvert succeeds, we know the inode
6f16bf655   Mark Fasheh   ocfs2: small clea...
879
880
881
882
883
884
  	 * is no longer live and can be deleted.
  	 *
  	 * Though we call this with the meta data lock held, the
  	 * trylock keeps us from ABBA deadlock.
  	 */
  	status = ocfs2_try_open_lock(inode, 1);
500086300   Tiger Yang   ocfs2: Remove del...
885
  	if (status == -EAGAIN) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
886
  		status = 0;
64f3b2692   Tao Ma   ocfs2: Remove mas...
887
  		reason = 3;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
888
889
890
891
892
893
  		goto bail;
  	}
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
500086300   Tiger Yang   ocfs2: Remove del...
894
  	*wipe = 1;
64f3b2692   Tao Ma   ocfs2: Remove mas...
895
  	trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
896
897
  
  bail:
64f3b2692   Tao Ma   ocfs2: Remove mas...
898
  	trace_ocfs2_query_inode_wipe_end(status, reason);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
899
900
901
902
903
904
905
906
907
  	return status;
  }
  
  /* Support function for ocfs2_delete_inode. Will help us keep the
   * inode data in a consistent state for clear_inode. Always truncates
   * pages, optionally sync's them first. */
  static void ocfs2_cleanup_delete_inode(struct inode *inode,
  				       int sync_data)
  {
64f3b2692   Tao Ma   ocfs2: Remove mas...
908
909
  	trace_ocfs2_cleanup_delete_inode(
  		(unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
910
  	if (sync_data)
249ec93c0   Jan Kara   ocfs2: Use filema...
911
  		filemap_write_and_wait(inode->i_mapping);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
912
913
  	truncate_inode_pages(&inode->i_data, 0);
  }
066d92dcb   Al Viro   convert ocfs2 to ...
914
  static void ocfs2_delete_inode(struct inode *inode)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
915
916
  {
  	int wipe, status;
e4b963f10   Joel Becker   ocfs2: Wrap signa...
917
  	sigset_t oldset;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
918
  	struct buffer_head *di_bh = NULL;
64f3b2692   Tao Ma   ocfs2: Remove mas...
919
920
921
  	trace_ocfs2_delete_inode(inode->i_ino,
  				 (unsigned long long)OCFS2_I(inode)->ip_blkno,
  				 is_bad_inode(inode));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
922

a90714c15   Jan Kara   ocfs2: Add quota ...
923
924
925
  	/* When we fail in read_inode() we mark inode as bad. The second test
  	 * catches the case when inode allocation fails before allocating
  	 * a block for inode. */
64f3b2692   Tao Ma   ocfs2: Remove mas...
926
  	if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
927
  		goto bail;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
928

871a29315   Christoph Hellwig   dquot: cleanup dq...
929
  	dquot_initialize(inode);
907f4554e   Christoph Hellwig   dquot: move dquot...
930

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
931
932
933
934
935
936
937
938
939
940
941
942
  	if (!ocfs2_inode_is_valid_to_delete(inode)) {
  		/* It's probably not necessary to truncate_inode_pages
  		 * here but we do it for safety anyway (it will most
  		 * likely be a no-op anyway) */
  		ocfs2_cleanup_delete_inode(inode, 0);
  		goto bail;
  	}
  
  	/* We want to block signals in delete_inode as the lock and
  	 * messaging paths may return us -ERESTARTSYS. Which would
  	 * cause us to exit early, resulting in inodes being orphaned
  	 * forever. */
e4b963f10   Joel Becker   ocfs2: Wrap signa...
943
  	ocfs2_block_signals(&oldset);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
944

6ca497a83   wengang wang   ocfs2: fix rare s...
945
946
947
948
949
950
951
952
953
954
955
956
  	/*
  	 * Synchronize us against ocfs2_get_dentry. We take this in
  	 * shared mode so that all nodes can still concurrently
  	 * process deletes.
  	 */
  	status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
  	if (status < 0) {
  		mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d
  ", status);
  		ocfs2_cleanup_delete_inode(inode, 0);
  		goto bail_unblock;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
957
958
  	/* Lock down the inode. This gives us an up to date view of
  	 * it's metadata (for verification), and allows us to
34d024f84   Mark Fasheh   ocfs2: Remove mou...
959
  	 * serialize delete_inode on multiple nodes.
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
960
961
962
963
964
  	 *
  	 * Even though we might be doing a truncate, we don't take the
  	 * allocation lock here as it won't be needed - nobody will
  	 * have the file open.
  	 */
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
965
  	status = ocfs2_inode_lock(inode, &di_bh, 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
966
967
968
969
  	if (status < 0) {
  		if (status != -ENOENT)
  			mlog_errno(status);
  		ocfs2_cleanup_delete_inode(inode, 0);
6ca497a83   wengang wang   ocfs2: fix rare s...
970
  		goto bail_unlock_nfs_sync;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
971
972
973
974
975
976
  	}
  
  	/* Query the cluster. This will be the final decision made
  	 * before we go ahead and wipe the inode. */
  	status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  	if (!wipe || status < 0) {
34d024f84   Mark Fasheh   ocfs2: Remove mou...
977
  		/* Error and remote inode busy both mean we won't be
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
978
979
980
981
  		 * removing the inode, so they take almost the same
  		 * path. */
  		if (status < 0)
  			mlog_errno(status);
34d024f84   Mark Fasheh   ocfs2: Remove mou...
982
983
984
  		/* Someone in the cluster has disallowed a wipe of
  		 * this inode, or it was never completely
  		 * orphaned. Write out the pages and exit now. */
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
985
986
987
988
989
990
991
992
  		ocfs2_cleanup_delete_inode(inode, 1);
  		goto bail_unlock_inode;
  	}
  
  	ocfs2_cleanup_delete_inode(inode, 0);
  
  	status = ocfs2_wipe_inode(inode, di_bh);
  	if (status < 0) {
b4df6ed8d   Mark Fasheh   [PATCH] ocfs2: fi...
993
994
  		if (status != -EDEADLK)
  			mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
995
996
  		goto bail_unlock_inode;
  	}
24c19ef40   Mark Fasheh   ocfs2: Remove i_g...
997
998
999
1000
1001
1002
1003
1004
1005
  	/*
  	 * Mark the inode as successfully deleted.
  	 *
  	 * This is important for ocfs2_clear_inode() as it will check
  	 * this flag and skip any checkpointing work
  	 *
  	 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
  	 * the LVB for other nodes.
  	 */
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1006
1007
1008
  	OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  
  bail_unlock_inode:
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1009
  	ocfs2_inode_unlock(inode, 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1010
  	brelse(di_bh);
6ca497a83   wengang wang   ocfs2: fix rare s...
1011
1012
1013
  
  bail_unlock_nfs_sync:
  	ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1014
  bail_unblock:
e4b963f10   Joel Becker   ocfs2: Wrap signa...
1015
  	ocfs2_unblock_signals(&oldset);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1016
  bail:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
1017
  	return;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1018
  }
066d92dcb   Al Viro   convert ocfs2 to ...
1019
  static void ocfs2_clear_inode(struct inode *inode)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1020
1021
1022
  {
  	int status;
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
066d92dcb   Al Viro   convert ocfs2 to ...
1023
  	end_writeback(inode);
64f3b2692   Tao Ma   ocfs2: Remove mas...
1024
1025
  	trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
  				inode->i_nlink);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1026
1027
1028
1029
  
  	mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
  			"Inode=%lu
  ", inode->i_ino);
9f7547580   Christoph Hellwig   dquot: cleanup dq...
1030
  	dquot_drop(inode);
257ba15ce   Christoph Hellwig   dquot: move dquot...
1031

34d024f84   Mark Fasheh   ocfs2: Remove mou...
1032
1033
  	/* To preven remote deletes we hold open lock before, now it
  	 * is time to unlock PR and EX open locks. */
500086300   Tiger Yang   ocfs2: Remove del...
1034
  	ocfs2_open_unlock(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1035
  	/* Do these before all the other work so that we don't bounce
34d024f84   Mark Fasheh   ocfs2: Remove mou...
1036
  	 * the downconvert thread while waiting to destroy the locks. */
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1037
  	ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1038
  	ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
500086300   Tiger Yang   ocfs2: Remove del...
1039
  	ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1040

4fe370afa   Mark Fasheh   ocfs2: use alloca...
1041
1042
1043
  	ocfs2_resv_discard(&OCFS2_SB(inode->i_sb)->osb_la_resmap,
  			   &oi->ip_la_data_resv);
  	ocfs2_resv_init_once(&oi->ip_la_data_resv);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  	/* We very well may get a clear_inode before all an inodes
  	 * metadata has hit disk. Of course, we can't drop any cluster
  	 * locks until the journal has finished with it. The only
  	 * exception here are successfully wiped inodes - their
  	 * metadata can now be considered to be part of the system
  	 * inodes from which it came. */
  	if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
  		ocfs2_checkpoint_inode(inode);
  
  	mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
b0697053f   Mark Fasheh   ocfs2: don't use ...
1054
1055
1056
  			"Clear inode of %llu, inode has io markers
  ",
  			(unsigned long long)oi->ip_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1057

834189788   Mark Fasheh   ocfs2: Cache exte...
1058
  	ocfs2_extent_map_trunc(inode, 0);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1059
1060
1061
1062
1063
  	status = ocfs2_drop_inode_locks(inode);
  	if (status < 0)
  		mlog_errno(status);
  
  	ocfs2_lock_res_free(&oi->ip_rw_lockres);
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1064
  	ocfs2_lock_res_free(&oi->ip_inode_lockres);
500086300   Tiger Yang   ocfs2: Remove del...
1065
  	ocfs2_lock_res_free(&oi->ip_open_lockres);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1066

66fb345dd   Joel Becker   ocfs2: move ip_la...
1067
  	ocfs2_metadata_cache_exit(INODE_CACHE(inode));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1068

8cb471e8f   Joel Becker   ocfs2: Take the i...
1069
  	mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
b0697053f   Mark Fasheh   ocfs2: don't use ...
1070
1071
  			"Clear inode of %llu, inode has %u cache items
  ",
8cb471e8f   Joel Becker   ocfs2: Take the i...
1072
1073
  			(unsigned long long)oi->ip_blkno,
  			INODE_CACHE(inode)->ci_num_cached);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1074

8cb471e8f   Joel Becker   ocfs2: Take the i...
1075
  	mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
b0697053f   Mark Fasheh   ocfs2: don't use ...
1076
1077
1078
  			"Clear inode of %llu, inode has a bad flag
  ",
  			(unsigned long long)oi->ip_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1079
1080
  
  	mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
b0697053f   Mark Fasheh   ocfs2: don't use ...
1081
1082
1083
  			"Clear inode of %llu, inode is locked
  ",
  			(unsigned long long)oi->ip_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1084

251b6eccb   Mark Fasheh   [OCFS2] Make ip_i...
1085
  	mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
b0697053f   Mark Fasheh   ocfs2: don't use ...
1086
1087
1088
  			"Clear inode of %llu, io_mutex is locked
  ",
  			(unsigned long long)oi->ip_blkno);
251b6eccb   Mark Fasheh   [OCFS2] Make ip_i...
1089
  	mutex_unlock(&oi->ip_io_mutex);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1090
1091
1092
1093
1094
1095
  
  	/*
  	 * down_trylock() returns 0, down_write_trylock() returns 1
  	 * kernel 1, world 0
  	 */
  	mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
b0697053f   Mark Fasheh   ocfs2: don't use ...
1096
1097
1098
  			"Clear inode of %llu, alloc_sem is locked
  ",
  			(unsigned long long)oi->ip_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1099
1100
1101
  	up_write(&oi->ip_alloc_sem);
  
  	mlog_bug_on_msg(oi->ip_open_count,
b0697053f   Mark Fasheh   ocfs2: don't use ...
1102
1103
1104
  			"Clear inode of %llu has open count %d
  ",
  			(unsigned long long)oi->ip_blkno, oi->ip_open_count);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1105
1106
  
  	/* Clear all other flags. */
47460d65a   Joel Becker   ocfs2: Make the o...
1107
  	oi->ip_flags = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1108
1109
  	oi->ip_dir_start_lookup = 0;
  	oi->ip_blkno = 0ULL;
ae0dff683   Sunil Mushran   ocfs2: Set journa...
1110
1111
1112
1113
1114
1115
  
  	/*
  	 * ip_jinode is used to track txns against this inode. We ensure that
  	 * the journal is flushed before journal shutdown. Thus it is safe to
  	 * have inodes get cleaned up after journal shutdown.
  	 */
2b4e30fbd   Joel Becker   ocfs2: Switch ove...
1116
1117
  	jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
  				       &oi->ip_jinode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1118
  }
066d92dcb   Al Viro   convert ocfs2 to ...
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
  void ocfs2_evict_inode(struct inode *inode)
  {
  	if (!inode->i_nlink ||
  	    (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
  		ocfs2_delete_inode(inode);
  	} else {
  		truncate_inode_pages(&inode->i_data, 0);
  	}
  	ocfs2_clear_inode(inode);
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1129
1130
1131
  /* Called under inode_lock, with no more references on the
   * struct inode, so it's safe here to check the flags field
   * and to manipulate i_nlink without any other locks. */
45321ac54   Al Viro   Make ->drop_inode...
1132
  int ocfs2_drop_inode(struct inode *inode)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1133
1134
  {
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
45321ac54   Al Viro   Make ->drop_inode...
1135
  	int res;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1136

64f3b2692   Tao Ma   ocfs2: Remove mas...
1137
1138
  	trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno,
  				inode->i_nlink, oi->ip_flags);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1139

379dfe9d0   Mark Fasheh   ocfs2: Hook rest ...
1140
  	if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
45321ac54   Al Viro   Make ->drop_inode...
1141
  		res = 1;
379dfe9d0   Mark Fasheh   ocfs2: Hook rest ...
1142
  	else
45321ac54   Al Viro   Make ->drop_inode...
1143
  		res = generic_drop_inode(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1144

45321ac54   Al Viro   Make ->drop_inode...
1145
  	return res;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1146
1147
1148
  }
  
  /*
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1149
1150
1151
1152
1153
1154
   * This is called from our getattr.
   */
  int ocfs2_inode_revalidate(struct dentry *dentry)
  {
  	struct inode *inode = dentry->d_inode;
  	int status = 0;
64f3b2692   Tao Ma   ocfs2: Remove mas...
1155
1156
1157
  	trace_ocfs2_inode_revalidate(inode,
  		inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL,
  		inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1158
1159
  
  	if (!inode) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1160
1161
1162
1163
1164
1165
1166
  		status = -ENOENT;
  		goto bail;
  	}
  
  	spin_lock(&OCFS2_I(inode)->ip_lock);
  	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  		spin_unlock(&OCFS2_I(inode)->ip_lock);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1167
1168
1169
1170
  		status = -ENOENT;
  		goto bail;
  	}
  	spin_unlock(&OCFS2_I(inode)->ip_lock);
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1171
  	/* Let ocfs2_inode_lock do the work of updating our struct
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1172
  	 * inode for us. */
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1173
  	status = ocfs2_inode_lock(inode, NULL, 0);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1174
1175
1176
1177
1178
  	if (status < 0) {
  		if (status != -ENOENT)
  			mlog_errno(status);
  		goto bail;
  	}
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1179
  	ocfs2_inode_unlock(inode, 0);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1180
  bail:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1181
1182
1183
1184
1185
1186
1187
1188
  	return status;
  }
  
  /*
   * Updates a disk inode from a
   * struct inode.
   * Only takes ip_lock.
   */
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1189
  int ocfs2_mark_inode_dirty(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1190
1191
1192
1193
1194
  			   struct inode *inode,
  			   struct buffer_head *bh)
  {
  	int status;
  	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
64f3b2692   Tao Ma   ocfs2: Remove mas...
1195
  	trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1196

0cf2f7632   Joel Becker   ocfs2: Pass struc...
1197
  	status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
13723d00e   Joel Becker   ocfs2: Use metada...
1198
  					 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1199
1200
1201
1202
1203
1204
1205
  	if (status < 0) {
  		mlog_errno(status);
  		goto leave;
  	}
  
  	spin_lock(&OCFS2_I(inode)->ip_lock);
  	fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
6e4b0d569   Jan Kara   [PATCH] Copy i_fl...
1206
  	ocfs2_get_inode_flags(OCFS2_I(inode));
ca4d147e6   Herbert Poetzl   ocfs2: add ext2 a...
1207
  	fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
15b1e36bd   Mark Fasheh   ocfs2: Structure ...
1208
  	fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1209
1210
1211
  	spin_unlock(&OCFS2_I(inode)->ip_lock);
  
  	fe->i_size = cpu_to_le64(i_size_read(inode));
198a1ca3b   Mark Fasheh   ocfs2: Increase m...
1212
  	ocfs2_set_links_count(fe, inode->i_nlink);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1213
1214
1215
1216
1217
1218
1219
1220
1221
  	fe->i_uid = cpu_to_le32(inode->i_uid);
  	fe->i_gid = cpu_to_le32(inode->i_gid);
  	fe->i_mode = cpu_to_le16(inode->i_mode);
  	fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  	fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  	fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  	fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  	fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  	fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1222
  	ocfs2_journal_dirty(handle, bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1223
  leave:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
  	return status;
  }
  
  /*
   *
   * Updates a struct inode from a disk inode.
   * does no i/o, only takes ip_lock.
   */
  void ocfs2_refresh_inode(struct inode *inode,
  			 struct ocfs2_dinode *fe)
  {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1235
1236
1237
  	spin_lock(&OCFS2_I(inode)->ip_lock);
  
  	OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
ca4d147e6   Herbert Poetzl   ocfs2: add ext2 a...
1238
  	OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
15b1e36bd   Mark Fasheh   ocfs2: Structure ...
1239
  	OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
ca4d147e6   Herbert Poetzl   ocfs2: add ext2 a...
1240
  	ocfs2_set_inode_flags(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1241
  	i_size_write(inode, le64_to_cpu(fe->i_size));
bfe868486   Miklos Szeredi   filesystems: add ...
1242
  	set_nlink(inode, ocfs2_read_links_count(fe));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1243
1244
1245
  	inode->i_uid = le32_to_cpu(fe->i_uid);
  	inode->i_gid = le32_to_cpu(fe->i_gid);
  	inode->i_mode = le16_to_cpu(fe->i_mode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1246
1247
1248
  	if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  		inode->i_blocks = 0;
  	else
8110b073a   Mark Fasheh   ocfs2: Fix up i_b...
1249
  		inode->i_blocks = ocfs2_inode_sector_count(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1250
1251
1252
1253
1254
1255
1256
1257
1258
  	inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  	inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  	inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  	inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  	inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  	inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  
  	spin_unlock(&OCFS2_I(inode)->ip_lock);
  }
b657c95c1   Joel Becker   ocfs2: Wrap inode...
1259
1260
1261
1262
  
  int ocfs2_validate_inode_block(struct super_block *sb,
  			       struct buffer_head *bh)
  {
d6b32bbb3   Joel Becker   ocfs2: block read...
1263
  	int rc;
b657c95c1   Joel Becker   ocfs2: Wrap inode...
1264
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
64f3b2692   Tao Ma   ocfs2: Remove mas...
1265
  	trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
970e4936d   Joel Becker   ocfs2: Validate m...
1266

b657c95c1   Joel Becker   ocfs2: Wrap inode...
1267
  	BUG_ON(!buffer_uptodate(bh));
d6b32bbb3   Joel Becker   ocfs2: block read...
1268
1269
1270
1271
1272
1273
  	/*
  	 * If the ecc fails, we return the error but otherwise
  	 * leave the filesystem running.  We know any error is
  	 * local to this block.
  	 */
  	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
13723d00e   Joel Becker   ocfs2: Use metada...
1274
1275
1276
1277
  	if (rc) {
  		mlog(ML_ERROR, "Checksum failed for dinode %llu
  ",
  		     (unsigned long long)bh->b_blocknr);
d6b32bbb3   Joel Becker   ocfs2: block read...
1278
  		goto bail;
13723d00e   Joel Becker   ocfs2: Use metada...
1279
  	}
d6b32bbb3   Joel Becker   ocfs2: block read...
1280
1281
1282
1283
1284
1285
  
  	/*
  	 * Errors after here are fatal.
  	 */
  
  	rc = -EINVAL;
b657c95c1   Joel Becker   ocfs2: Wrap inode...
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
  	if (!OCFS2_IS_VALID_DINODE(di)) {
  		ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s
  ",
  			    (unsigned long long)bh->b_blocknr, 7,
  			    di->i_signature);
  		goto bail;
  	}
  
  	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  		ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu
  ",
  			    (unsigned long long)bh->b_blocknr,
  			    (unsigned long long)le64_to_cpu(di->i_blkno));
  		goto bail;
  	}
  
  	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  		ocfs2_error(sb,
  			    "Invalid dinode #%llu: OCFS2_VALID_FL not set
  ",
  			    (unsigned long long)bh->b_blocknr);
  		goto bail;
  	}
  
  	if (le32_to_cpu(di->i_fs_generation) !=
  	    OCFS2_SB(sb)->fs_generation) {
  		ocfs2_error(sb,
  			    "Invalid dinode #%llu: fs_generation is %u
  ",
  			    (unsigned long long)bh->b_blocknr,
  			    le32_to_cpu(di->i_fs_generation));
  		goto bail;
  	}
  
  	rc = 0;
  
  bail:
  	return rc;
  }
  
  int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
  				int flags)
  {
  	int rc;
  	struct buffer_head *tmp = *bh;
8cb471e8f   Joel Becker   ocfs2: Take the i...
1331
1332
  	rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
  			       1, &tmp, flags, ocfs2_validate_inode_block);
b657c95c1   Joel Becker   ocfs2: Wrap inode...
1333
1334
  
  	/* If ocfs2_read_blocks() got us a new bh, pass it up. */
970e4936d   Joel Becker   ocfs2: Validate m...
1335
  	if (!rc && !*bh)
b657c95c1   Joel Becker   ocfs2: Wrap inode...
1336
  		*bh = tmp;
b657c95c1   Joel Becker   ocfs2: Wrap inode...
1337
1338
1339
1340
1341
1342
1343
  	return rc;
  }
  
  int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
  {
  	return ocfs2_read_inode_block_full(inode, bh, 0);
  }
6e5a3d753   Joel Becker   ocfs2: Change met...
1344

6e5a3d753   Joel Becker   ocfs2: Change met...
1345
1346
1347
1348
1349
1350
1351
  
  static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
  {
  	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  
  	return oi->ip_blkno;
  }
8cb471e8f   Joel Becker   ocfs2: Take the i...
1352
1353
1354
1355
1356
1357
  static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
  {
  	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  
  	return oi->vfs_inode.i_sb;
  }
6e5a3d753   Joel Becker   ocfs2: Change met...
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
  static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
  {
  	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  
  	spin_lock(&oi->ip_lock);
  }
  
  static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
  {
  	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  
  	spin_unlock(&oi->ip_lock);
  }
  
  static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
  {
  	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  
  	mutex_lock(&oi->ip_io_mutex);
  }
  
  static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
  {
  	struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  
  	mutex_unlock(&oi->ip_io_mutex);
  }
  
  const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
  	.co_owner		= ocfs2_inode_cache_owner,
8cb471e8f   Joel Becker   ocfs2: Take the i...
1388
  	.co_get_super		= ocfs2_inode_cache_get_super,
6e5a3d753   Joel Becker   ocfs2: Change met...
1389
1390
1391
1392
1393
  	.co_cache_lock		= ocfs2_inode_cache_lock,
  	.co_cache_unlock	= ocfs2_inode_cache_unlock,
  	.co_io_lock		= ocfs2_inode_cache_io_lock,
  	.co_io_unlock		= ocfs2_inode_cache_io_unlock,
  };