blob: 288512c9dbc27fc87c94aaf3ee4fa374e1b504cc [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * inode.c
5 *
6 * vfs' aops, fops, dops and iops
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
30#include <linux/pagemap.h>
Jan Karaa90714c2008-10-09 19:38:40 +020031#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080032
33#include <asm/byteorder.h>
34
35#define MLOG_MASK_PREFIX ML_INODE
36#include <cluster/masklog.h>
37
38#include "ocfs2.h"
39
40#include "alloc.h"
41#include "dlmglue.h"
42#include "extent_map.h"
43#include "file.h"
Mark Fashehb4df6ed2006-02-22 17:35:08 -080044#include "heartbeat.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080045#include "inode.h"
46#include "journal.h"
47#include "namei.h"
48#include "suballoc.h"
49#include "super.h"
50#include "symlink.h"
51#include "sysfile.h"
52#include "uptodate.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080053#include "xattr.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080054
55#include "buffer_head_io.h"
56
Mark Fashehccd979b2005-12-15 14:31:24 -080057struct ocfs2_find_inode_args
58{
59 u64 fi_blkno;
60 unsigned long fi_ino;
61 unsigned int fi_flags;
Jan Kara5fa06132008-01-11 00:11:45 +010062 unsigned int fi_sysfile_type;
Mark Fashehccd979b2005-12-15 14:31:24 -080063};
64
Jan Kara5fa06132008-01-11 00:11:45 +010065static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
66
Mark Fashehccd979b2005-12-15 14:31:24 -080067static int ocfs2_read_locked_inode(struct inode *inode,
68 struct ocfs2_find_inode_args *args);
69static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
70static int ocfs2_find_actor(struct inode *inode, void *opaque);
71static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
72 struct inode *inode,
73 struct buffer_head *fe_bh);
74
Herbert Poetzlca4d1472006-07-03 17:27:12 -070075void ocfs2_set_inode_flags(struct inode *inode)
76{
77 unsigned int flags = OCFS2_I(inode)->ip_attr;
78
79 inode->i_flags &= ~(S_IMMUTABLE |
80 S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
81
82 if (flags & OCFS2_IMMUTABLE_FL)
83 inode->i_flags |= S_IMMUTABLE;
84
85 if (flags & OCFS2_SYNC_FL)
86 inode->i_flags |= S_SYNC;
87 if (flags & OCFS2_APPEND_FL)
88 inode->i_flags |= S_APPEND;
89 if (flags & OCFS2_NOATIME_FL)
90 inode->i_flags |= S_NOATIME;
91 if (flags & OCFS2_DIRSYNC_FL)
92 inode->i_flags |= S_DIRSYNC;
93}
94
Jan Kara6e4b0d52007-04-27 11:08:01 -070095/* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
96void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
97{
98 unsigned int flags = oi->vfs_inode.i_flags;
99
100 oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
101 OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
102 if (flags & S_SYNC)
103 oi->ip_attr |= OCFS2_SYNC_FL;
104 if (flags & S_APPEND)
105 oi->ip_attr |= OCFS2_APPEND_FL;
106 if (flags & S_IMMUTABLE)
107 oi->ip_attr |= OCFS2_IMMUTABLE_FL;
108 if (flags & S_NOATIME)
109 oi->ip_attr |= OCFS2_NOATIME_FL;
110 if (flags & S_DIRSYNC)
111 oi->ip_attr |= OCFS2_DIRSYNC_FL;
112}
113
Jan Kara5fa06132008-01-11 00:11:45 +0100114struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
115 int sysfile_type)
Mark Fashehccd979b2005-12-15 14:31:24 -0800116{
117 struct inode *inode = NULL;
118 struct super_block *sb = osb->sb;
119 struct ocfs2_find_inode_args args;
120
Mark Fashehb06970532006-03-03 10:24:33 -0800121 mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800122
123 /* Ok. By now we've either got the offsets passed to us by the
124 * caller, or we just pulled them off the bh. Lets do some
125 * sanity checks to make sure they're OK. */
126 if (blkno == 0) {
127 inode = ERR_PTR(-EINVAL);
128 mlog_errno(PTR_ERR(inode));
129 goto bail;
130 }
131
132 args.fi_blkno = blkno;
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700133 args.fi_flags = flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800134 args.fi_ino = ino_from_blkno(sb, blkno);
Jan Kara5fa06132008-01-11 00:11:45 +0100135 args.fi_sysfile_type = sysfile_type;
Mark Fashehccd979b2005-12-15 14:31:24 -0800136
137 inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
138 ocfs2_init_locked_inode, &args);
139 /* inode was *not* in the inode cache. 2.6.x requires
140 * us to do our own read_inode call and unlock it
141 * afterwards. */
142 if (inode && inode->i_state & I_NEW) {
143 mlog(0, "Inode was not in inode cache, reading it.\n");
144 ocfs2_read_locked_inode(inode, &args);
145 unlock_new_inode(inode);
146 }
147 if (inode == NULL) {
148 inode = ERR_PTR(-ENOMEM);
149 mlog_errno(PTR_ERR(inode));
150 goto bail;
151 }
152 if (is_bad_inode(inode)) {
153 iput(inode);
154 inode = ERR_PTR(-ESTALE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800155 goto bail;
156 }
157
158bail:
159 if (!IS_ERR(inode)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800160 mlog(0, "returning inode with number %llu\n",
161 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800162 mlog_exit_ptr(inode);
Mark Fasheh6a1bd4a2007-01-03 17:06:59 -0800163 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800164
165 return inode;
166}
167
168
169/*
170 * here's how inodes get read from disk:
171 * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
172 * found? : return the in-memory inode
173 * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
174 */
175
176static int ocfs2_find_actor(struct inode *inode, void *opaque)
177{
178 struct ocfs2_find_inode_args *args = NULL;
179 struct ocfs2_inode_info *oi = OCFS2_I(inode);
180 int ret = 0;
181
182 mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
183
184 args = opaque;
185
186 mlog_bug_on_msg(!inode, "No inode in find actor!\n");
187
188 if (oi->ip_blkno != args->fi_blkno)
189 goto bail;
190
Mark Fashehccd979b2005-12-15 14:31:24 -0800191 ret = 1;
192bail:
193 mlog_exit(ret);
194 return ret;
195}
196
197/*
198 * initialize the new inode, but don't do anything that would cause
199 * us to sleep.
200 * return 0 on success, 1 on failure
201 */
202static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
203{
204 struct ocfs2_find_inode_args *args = opaque;
205
206 mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
207
208 inode->i_ino = args->fi_ino;
209 OCFS2_I(inode)->ip_blkno = args->fi_blkno;
Jan Kara5fa06132008-01-11 00:11:45 +0100210 if (args->fi_sysfile_type != 0)
211 lockdep_set_class(&inode->i_mutex,
212 &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800213
214 mlog_exit(0);
215 return 0;
216}
217
Joel Beckerb657c952008-11-13 14:49:11 -0800218void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
219 int create_ino)
Mark Fashehccd979b2005-12-15 14:31:24 -0800220{
221 struct super_block *sb;
222 struct ocfs2_super *osb;
Mark Fasheh53da4932008-07-21 14:29:16 -0700223 int use_plocks = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800224
Mark Fashehb06970532006-03-03 10:24:33 -0800225 mlog_entry("(0x%p, size:%llu)\n", inode,
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700226 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -0800227
228 sb = inode->i_sb;
229 osb = OCFS2_SB(sb);
230
Mark Fasheh53da4932008-07-21 14:29:16 -0700231 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
232 ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
233 use_plocks = 0;
234
Joel Beckerb657c952008-11-13 14:49:11 -0800235 /*
236 * These have all been checked by ocfs2_read_inode_block() or set
237 * by ocfs2_mknod_locked(), so a failure is a code bug.
238 */
239 BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode
240 cannot create a superblock
241 inode today. change if
242 that is needed. */
243 BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
244 BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800245
Mark Fashehccd979b2005-12-15 14:31:24 -0800246
Mark Fasheh8110b072007-03-22 16:53:23 -0700247 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
248 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -0700249 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
Mark Fasheh8110b072007-03-22 16:53:23 -0700250
Mark Fashehccd979b2005-12-15 14:31:24 -0800251 inode->i_version = 1;
252 inode->i_generation = le32_to_cpu(fe->i_generation);
253 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
254 inode->i_mode = le16_to_cpu(fe->i_mode);
255 inode->i_uid = le32_to_cpu(fe->i_uid);
256 inode->i_gid = le32_to_cpu(fe->i_gid);
Mark Fashehccd979b2005-12-15 14:31:24 -0800257
258 /* Fast symlinks will have i_size but no allocated clusters. */
259 if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
260 inode->i_blocks = 0;
261 else
Mark Fasheh8110b072007-03-22 16:53:23 -0700262 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800263 inode->i_mapping->a_ops = &ocfs2_aops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800264 inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
265 inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
266 inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
267 inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
268 inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
269 inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
270
271 if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
272 mlog(ML_ERROR,
Mark Fashehb06970532006-03-03 10:24:33 -0800273 "ip_blkno %llu != i_blkno %llu!\n",
274 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700275 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800276
Mark Fashehccd979b2005-12-15 14:31:24 -0800277 inode->i_nlink = le16_to_cpu(fe->i_links_count);
278
Jan Karabbbd0eb2008-08-21 18:22:30 +0200279 if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700280 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
Jan Karabbbd0eb2008-08-21 18:22:30 +0200281 inode->i_flags |= S_NOQUOTA;
282 }
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700283
Mark Fashehccd979b2005-12-15 14:31:24 -0800284 if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
285 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
286 mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
287 } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
288 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
Jan Kara1a224ad2008-08-20 15:43:36 +0200289 } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
290 inode->i_flags |= S_NOQUOTA;
Mark Fashehccd979b2005-12-15 14:31:24 -0800291 } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
292 mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
293 /* we can't actually hit this as read_inode can't
294 * handle superblocks today ;-) */
295 BUG();
296 }
297
298 switch (inode->i_mode & S_IFMT) {
299 case S_IFREG:
Mark Fasheh53da4932008-07-21 14:29:16 -0700300 if (use_plocks)
301 inode->i_fop = &ocfs2_fops;
302 else
303 inode->i_fop = &ocfs2_fops_no_plocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800304 inode->i_op = &ocfs2_file_iops;
305 i_size_write(inode, le64_to_cpu(fe->i_size));
306 break;
307 case S_IFDIR:
308 inode->i_op = &ocfs2_dir_iops;
Mark Fasheh53da4932008-07-21 14:29:16 -0700309 if (use_plocks)
310 inode->i_fop = &ocfs2_dops;
311 else
312 inode->i_fop = &ocfs2_dops_no_plocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800313 i_size_write(inode, le64_to_cpu(fe->i_size));
314 break;
315 case S_IFLNK:
316 if (ocfs2_inode_is_fast_symlink(inode))
317 inode->i_op = &ocfs2_fast_symlink_inode_operations;
318 else
319 inode->i_op = &ocfs2_symlink_inode_operations;
320 i_size_write(inode, le64_to_cpu(fe->i_size));
321 break;
322 default:
323 inode->i_op = &ocfs2_special_file_iops;
324 init_special_inode(inode, inode->i_mode,
325 inode->i_rdev);
326 break;
327 }
328
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700329 if (create_ino) {
330 inode->i_ino = ino_from_blkno(inode->i_sb,
331 le64_to_cpu(fe->i_blkno));
332
333 /*
334 * If we ever want to create system files from kernel,
335 * the generation argument to
336 * ocfs2_inode_lock_res_init() will have to change.
337 */
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700338 BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700339
Mark Fashehe63aecb62007-10-18 15:30:42 -0700340 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700341 OCFS2_LOCK_TYPE_META, 0, inode);
Tiger Yang50008632007-03-20 16:01:38 -0700342
343 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
344 OCFS2_LOCK_TYPE_OPEN, 0, inode);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700345 }
346
Mark Fashehccd979b2005-12-15 14:31:24 -0800347 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700348 OCFS2_LOCK_TYPE_RW, inode->i_generation,
349 inode);
350
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700351 ocfs2_set_inode_flags(inode);
Herbert Poetzlca4d1472006-07-03 17:27:12 -0700352
Joel Beckerb657c952008-11-13 14:49:11 -0800353 mlog_exit_void();
Mark Fashehccd979b2005-12-15 14:31:24 -0800354}
355
356static int ocfs2_read_locked_inode(struct inode *inode,
357 struct ocfs2_find_inode_args *args)
358{
359 struct super_block *sb;
360 struct ocfs2_super *osb;
361 struct ocfs2_dinode *fe;
362 struct buffer_head *bh = NULL;
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700363 int status, can_lock;
364 u32 generation = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800365
366 mlog_entry("(0x%p, 0x%p)\n", inode, args);
367
368 status = -EINVAL;
369 if (inode == NULL || inode->i_sb == NULL) {
370 mlog(ML_ERROR, "bad inode\n");
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700371 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800372 }
373 sb = inode->i_sb;
374 osb = OCFS2_SB(sb);
375
376 if (!args) {
377 mlog(ML_ERROR, "bad inode args\n");
378 make_bad_inode(inode);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700379 return status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800380 }
381
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700382 /*
383 * To improve performance of cold-cache inode stats, we take
384 * the cluster lock here if possible.
385 *
386 * Generally, OCFS2 never trusts the contents of an inode
387 * unless it's holding a cluster lock, so taking it here isn't
388 * a correctness issue as much as it is a performance
389 * improvement.
390 *
391 * There are three times when taking the lock is not a good idea:
392 *
393 * 1) During startup, before we have initialized the DLM.
394 *
395 * 2) If we are reading certain system files which never get
396 * cluster locks (local alloc, truncate log).
397 *
398 * 3) If the process doing the iget() is responsible for
399 * orphan dir recovery. We're holding the orphan dir lock and
400 * can get into a deadlock with another process on another
401 * node in ->delete_inode().
402 *
403 * #1 and #2 can be simply solved by never taking the lock
404 * here for system files (which are the only type we read
405 * during mount). It's a heavier approach, but our main
406 * concern is user-accesible files anyway.
407 *
408 * #3 works itself out because we'll eventually take the
409 * cluster lock before trusting anything anyway.
410 */
411 can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
Tiger Yang50008632007-03-20 16:01:38 -0700412 && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800413 && !ocfs2_mount_local(osb);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700414
415 /*
416 * To maintain backwards compatibility with older versions of
417 * ocfs2-tools, we still store the generation value for system
418 * files. The only ones that actually matter to userspace are
419 * the journals, but it's easier and inexpensive to just flag
420 * all system files similarly.
421 */
422 if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
423 generation = osb->fs_generation;
424
Mark Fashehe63aecb62007-10-18 15:30:42 -0700425 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700426 OCFS2_LOCK_TYPE_META,
427 generation, inode);
428
Tiger Yang50008632007-03-20 16:01:38 -0700429 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
430 OCFS2_LOCK_TYPE_OPEN,
431 0, inode);
432
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700433 if (can_lock) {
Tiger Yang50008632007-03-20 16:01:38 -0700434 status = ocfs2_open_lock(inode);
435 if (status) {
436 make_bad_inode(inode);
437 mlog_errno(status);
438 return status;
439 }
Mark Fashehe63aecb62007-10-18 15:30:42 -0700440 status = ocfs2_inode_lock(inode, NULL, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700441 if (status) {
442 make_bad_inode(inode);
443 mlog_errno(status);
444 return status;
445 }
446 }
447
Tiger Yang50008632007-03-20 16:01:38 -0700448 if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
449 status = ocfs2_try_open_lock(inode, 0);
450 if (status) {
451 make_bad_inode(inode);
452 return status;
453 }
454 }
455
Joel Beckerb657c952008-11-13 14:49:11 -0800456 if (can_lock) {
457 status = ocfs2_read_inode_block_full(inode, &bh,
458 OCFS2_BH_IGNORE_CACHE);
459 } else {
Joel Beckerda1e9092008-10-09 17:20:29 -0700460 status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
Joel Beckerb657c952008-11-13 14:49:11 -0800461 if (!status)
462 status = ocfs2_validate_inode_block(osb->sb, bh);
463 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800464 if (status < 0) {
465 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800466 goto bail;
467 }
468
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700469 status = -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800470 fe = (struct ocfs2_dinode *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800471
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700472 /*
473 * This is a code bug. Right now the caller needs to
474 * understand whether it is asking for a system file inode or
475 * not so the proper lock names can be built.
476 */
477 mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
478 !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
479 "Inode %llu: system file state is ambigous\n",
480 (unsigned long long)args->fi_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800481
482 if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
483 S_ISBLK(le16_to_cpu(fe->i_mode)))
Joel Beckerb657c952008-11-13 14:49:11 -0800484 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
Mark Fashehccd979b2005-12-15 14:31:24 -0800485
Joel Beckerb657c952008-11-13 14:49:11 -0800486 ocfs2_populate_inode(inode, fe, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800487
488 BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
489
Mark Fashehccd979b2005-12-15 14:31:24 -0800490 status = 0;
491
492bail:
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700493 if (can_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700494 ocfs2_inode_unlock(inode, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700495
496 if (status < 0)
497 make_bad_inode(inode);
498
Mark Fashehccd979b2005-12-15 14:31:24 -0800499 if (args && bh)
500 brelse(bh);
501
502 mlog_exit(status);
503 return status;
504}
505
506void ocfs2_sync_blockdev(struct super_block *sb)
507{
508 sync_blockdev(sb->s_bdev);
509}
510
511static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
512 struct inode *inode,
513 struct buffer_head *fe_bh)
514{
515 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800516 struct ocfs2_truncate_context *tc = NULL;
517 struct ocfs2_dinode *fe;
Mark Fasheh60b11392007-02-16 11:46:50 -0800518 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800519
520 mlog_entry_void();
521
522 fe = (struct ocfs2_dinode *) fe_bh->b_data;
523
Mark Fasheh1afc32b2007-09-07 14:46:51 -0700524 /*
525 * This check will also skip truncate of inodes with inline
526 * data and fast symlinks.
527 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800528 if (fe->i_clusters) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700529 if (ocfs2_should_order_data(inode))
530 ocfs2_begin_ordered_truncate(inode, 0);
531
Mark Fasheh60b11392007-02-16 11:46:50 -0800532 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
533 if (IS_ERR(handle)) {
534 status = PTR_ERR(handle);
535 mlog_errno(status);
536 goto out;
537 }
538
539 status = ocfs2_journal_access(handle, inode, fe_bh,
540 OCFS2_JOURNAL_ACCESS_WRITE);
541 if (status < 0) {
542 mlog_errno(status);
543 goto out;
544 }
545
546 i_size_write(inode, 0);
547
548 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
549 if (status < 0) {
550 mlog_errno(status);
551 goto out;
552 }
553
554 ocfs2_commit_trans(osb, handle);
555 handle = NULL;
556
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800557 status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
558 if (status < 0) {
559 mlog_errno(status);
560 goto out;
561 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800562
Mark Fasheh3a0782d2007-01-17 12:53:31 -0800563 status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
564 if (status < 0) {
565 mlog_errno(status);
566 goto out;
567 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800568 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800569
Mark Fasheh60b11392007-02-16 11:46:50 -0800570out:
571 if (handle)
572 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800573 mlog_exit(status);
574 return status;
575}
576
577static int ocfs2_remove_inode(struct inode *inode,
578 struct buffer_head *di_bh,
579 struct inode *orphan_dir_inode,
580 struct buffer_head *orphan_dir_bh)
581{
582 int status;
583 struct inode *inode_alloc_inode = NULL;
584 struct buffer_head *inode_alloc_bh = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700585 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800586 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
587 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
588
589 inode_alloc_inode =
590 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
591 le16_to_cpu(di->i_suballoc_slot));
592 if (!inode_alloc_inode) {
593 status = -EEXIST;
594 mlog_errno(status);
595 goto bail;
596 }
597
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800598 mutex_lock(&inode_alloc_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700599 status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800600 if (status < 0) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800601 mutex_unlock(&inode_alloc_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800602
603 mlog_errno(status);
604 goto bail;
605 }
606
Jan Karaa90714c2008-10-09 19:38:40 +0200607 handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
608 ocfs2_quota_trans_credits(inode->i_sb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800609 if (IS_ERR(handle)) {
610 status = PTR_ERR(handle);
611 mlog_errno(status);
612 goto bail_unlock;
613 }
614
615 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
616 orphan_dir_bh);
617 if (status < 0) {
618 mlog_errno(status);
619 goto bail_commit;
620 }
621
622 /* set the inodes dtime */
623 status = ocfs2_journal_access(handle, inode, di_bh,
624 OCFS2_JOURNAL_ACCESS_WRITE);
625 if (status < 0) {
626 mlog_errno(status);
627 goto bail_commit;
628 }
629
630 di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
Marcin Slusarz4092d492007-12-25 15:52:59 +0100631 di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
Mark Fashehccd979b2005-12-15 14:31:24 -0800632
633 status = ocfs2_journal_dirty(handle, di_bh);
634 if (status < 0) {
635 mlog_errno(status);
636 goto bail_commit;
637 }
638
639 ocfs2_remove_from_cache(inode, di_bh);
Jan Karaa90714c2008-10-09 19:38:40 +0200640 vfs_dq_free_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800641
642 status = ocfs2_free_dinode(handle, inode_alloc_inode,
643 inode_alloc_bh, di);
644 if (status < 0)
645 mlog_errno(status);
646
647bail_commit:
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700648 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800649bail_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700650 ocfs2_inode_unlock(inode_alloc_inode, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800651 mutex_unlock(&inode_alloc_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800652 brelse(inode_alloc_bh);
653bail:
654 iput(inode_alloc_inode);
655
656 return status;
657}
658
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800659/*
660 * Serialize with orphan dir recovery. If the process doing
661 * recovery on this orphan dir does an iget() with the dir
662 * i_mutex held, we'll deadlock here. Instead we detect this
663 * and exit early - recovery will wipe this inode for us.
664 */
665static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
666 int slot)
667{
668 int ret = 0;
669
670 spin_lock(&osb->osb_lock);
671 if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
672 mlog(0, "Recovery is happening on orphan dir %d, will skip "
673 "this inode\n", slot);
674 ret = -EDEADLK;
675 goto out;
676 }
677 /* This signals to the orphan recovery process that it should
678 * wait for us to handle the wipe. */
679 osb->osb_orphan_wipes[slot]++;
680out:
681 spin_unlock(&osb->osb_lock);
682 return ret;
683}
684
685static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
686 int slot)
687{
688 spin_lock(&osb->osb_lock);
689 osb->osb_orphan_wipes[slot]--;
690 spin_unlock(&osb->osb_lock);
691
692 wake_up(&osb->osb_wipe_event);
693}
694
Mark Fashehccd979b2005-12-15 14:31:24 -0800695static int ocfs2_wipe_inode(struct inode *inode,
696 struct buffer_head *di_bh)
697{
698 int status, orphaned_slot;
699 struct inode *orphan_dir_inode = NULL;
700 struct buffer_head *orphan_dir_bh = NULL;
701 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tiger Yang50008632007-03-20 16:01:38 -0700702 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -0800703
Tiger Yang50008632007-03-20 16:01:38 -0700704 di = (struct ocfs2_dinode *) di_bh->b_data;
705 orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800706
707 status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
708 if (status)
709 return status;
710
Mark Fashehccd979b2005-12-15 14:31:24 -0800711 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
712 ORPHAN_DIR_SYSTEM_INODE,
713 orphaned_slot);
714 if (!orphan_dir_inode) {
715 status = -EEXIST;
716 mlog_errno(status);
717 goto bail;
718 }
719
720 /* Lock the orphan dir. The lock will be held for the entire
721 * delete_inode operation. We do this now to avoid races with
722 * recovery completion on other nodes. */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800723 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700724 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800725 if (status < 0) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800726 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800727
728 mlog_errno(status);
729 goto bail;
730 }
731
732 /* we do this while holding the orphan dir lock because we
Mark Fasheh34d024f2007-09-24 15:56:19 -0700733 * don't want recovery being run from another node to try an
734 * inode delete underneath us -- this will result in two nodes
Mark Fashehccd979b2005-12-15 14:31:24 -0800735 * truncating the same file! */
736 status = ocfs2_truncate_for_delete(osb, inode, di_bh);
737 if (status < 0) {
738 mlog_errno(status);
739 goto bail_unlock_dir;
740 }
741
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800742 /*Free extended attribute resources associated with this inode.*/
743 status = ocfs2_xattr_remove(inode, di_bh);
744 if (status < 0) {
745 mlog_errno(status);
746 goto bail_unlock_dir;
747 }
748
Mark Fashehccd979b2005-12-15 14:31:24 -0800749 status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
750 orphan_dir_bh);
751 if (status < 0)
752 mlog_errno(status);
753
754bail_unlock_dir:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700755 ocfs2_inode_unlock(orphan_dir_inode, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800756 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800757 brelse(orphan_dir_bh);
758bail:
759 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800760 ocfs2_signal_wipe_completion(osb, orphaned_slot);
Mark Fashehccd979b2005-12-15 14:31:24 -0800761
762 return status;
763}
764
765/* There is a series of simple checks that should be done before a
Mark Fasheh34d024f2007-09-24 15:56:19 -0700766 * trylock is even considered. Encapsulate those in this function. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800767static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
768{
769 int ret = 0;
770 struct ocfs2_inode_info *oi = OCFS2_I(inode);
771 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
772
773 /* We shouldn't be getting here for the root directory
774 * inode.. */
775 if (inode == osb->root_inode) {
776 mlog(ML_ERROR, "Skipping delete of root inode.\n");
777 goto bail;
778 }
779
Mark Fasheh34d024f2007-09-24 15:56:19 -0700780 /* If we're coming from downconvert_thread we can't go into our own
Mark Fashehccd979b2005-12-15 14:31:24 -0800781 * voting [hello, deadlock city!], so unforuntately we just
782 * have to skip deleting this guy. That's OK though because
783 * the node who's doing the actual deleting should handle it
784 * anyway. */
Mark Fasheh34d024f2007-09-24 15:56:19 -0700785 if (current == osb->dc_task) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800786 mlog(0, "Skipping delete of %lu because we're currently "
Mark Fasheh34d024f2007-09-24 15:56:19 -0700787 "in downconvert\n", inode->i_ino);
Mark Fashehccd979b2005-12-15 14:31:24 -0800788 goto bail;
789 }
790
791 spin_lock(&oi->ip_lock);
792 /* OCFS2 *never* deletes system files. This should technically
793 * never get here as system file inodes should always have a
794 * positive link count. */
795 if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
Mark Fashehb06970532006-03-03 10:24:33 -0800796 mlog(ML_ERROR, "Skipping delete of system file %llu\n",
797 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800798 goto bail_unlock;
799 }
800
Mark Fasheh34d024f2007-09-24 15:56:19 -0700801 /* If we have allowd wipe of this inode for another node, it
802 * will be marked here so we can safely skip it. Recovery will
803 * cleanup any inodes we might inadvertantly skip here. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800804 if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
805 mlog(0, "Skipping delete of %lu because another node "
806 "has done this for us.\n", inode->i_ino);
807 goto bail_unlock;
808 }
809
810 ret = 1;
811bail_unlock:
812 spin_unlock(&oi->ip_lock);
813bail:
814 return ret;
815}
816
817/* Query the cluster to determine whether we should wipe an inode from
818 * disk or not.
819 *
820 * Requires the inode to have the cluster lock. */
821static int ocfs2_query_inode_wipe(struct inode *inode,
822 struct buffer_head *di_bh,
823 int *wipe)
824{
825 int status = 0;
826 struct ocfs2_inode_info *oi = OCFS2_I(inode);
827 struct ocfs2_dinode *di;
828
829 *wipe = 0;
830
831 /* While we were waiting for the cluster lock in
832 * ocfs2_delete_inode, another node might have asked to delete
833 * the inode. Recheck our flags to catch this. */
834 if (!ocfs2_inode_is_valid_to_delete(inode)) {
Mark Fashehb06970532006-03-03 10:24:33 -0800835 mlog(0, "Skipping delete of %llu because flags changed\n",
836 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800837 goto bail;
838 }
839
840 /* Now that we have an up to date inode, we can double check
841 * the link count. */
842 if (inode->i_nlink) {
Mark Fashehb06970532006-03-03 10:24:33 -0800843 mlog(0, "Skipping delete of %llu because nlink = %u\n",
844 (unsigned long long)oi->ip_blkno, inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -0800845 goto bail;
846 }
847
848 /* Do some basic inode verification... */
849 di = (struct ocfs2_dinode *) di_bh->b_data;
850 if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
851 /* for lack of a better error? */
852 status = -EEXIST;
853 mlog(ML_ERROR,
Mark Fashehb06970532006-03-03 10:24:33 -0800854 "Inode %llu (on-disk %llu) not orphaned! "
Mark Fashehccd979b2005-12-15 14:31:24 -0800855 "Disk flags 0x%x, inode flags 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -0800856 (unsigned long long)oi->ip_blkno,
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700857 (unsigned long long)le64_to_cpu(di->i_blkno),
858 le32_to_cpu(di->i_flags), oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800859 goto bail;
860 }
861
862 /* has someone already deleted us?! baaad... */
863 if (di->i_dtime) {
864 status = -EEXIST;
865 mlog_errno(status);
866 goto bail;
867 }
868
Mark Fasheh6f16bf62007-03-20 17:17:54 -0700869 /*
870 * This is how ocfs2 determines whether an inode is still live
871 * within the cluster. Every node takes a shared read lock on
872 * the inode open lock in ocfs2_read_locked_inode(). When we
873 * get to ->delete_inode(), each node tries to convert it's
874 * lock to an exclusive. Trylocks are serialized by the inode
875 * meta data lock. If the upconvert suceeds, we know the inode
876 * is no longer live and can be deleted.
877 *
878 * Though we call this with the meta data lock held, the
879 * trylock keeps us from ABBA deadlock.
880 */
881 status = ocfs2_try_open_lock(inode, 1);
Tiger Yang50008632007-03-20 16:01:38 -0700882 if (status == -EAGAIN) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800883 status = 0;
Joe Perches27592362007-11-19 17:53:34 -0800884 mlog(0, "Skipping delete of %llu because it is in use on "
Mark Fashehb06970532006-03-03 10:24:33 -0800885 "other nodes\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800886 goto bail;
887 }
888 if (status < 0) {
889 mlog_errno(status);
890 goto bail;
891 }
892
Tiger Yang50008632007-03-20 16:01:38 -0700893 *wipe = 1;
894 mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
895 (unsigned long long)oi->ip_blkno,
896 le16_to_cpu(di->i_orphaned_slot));
Mark Fashehccd979b2005-12-15 14:31:24 -0800897
898bail:
899 return status;
900}
901
902/* Support function for ocfs2_delete_inode. Will help us keep the
903 * inode data in a consistent state for clear_inode. Always truncates
904 * pages, optionally sync's them first. */
905static void ocfs2_cleanup_delete_inode(struct inode *inode,
906 int sync_data)
907{
Mark Fashehb06970532006-03-03 10:24:33 -0800908 mlog(0, "Cleanup inode %llu, sync = %d\n",
909 (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 if (sync_data)
911 write_inode_now(inode, 1);
912 truncate_inode_pages(&inode->i_data, 0);
913}
914
915void ocfs2_delete_inode(struct inode *inode)
916{
917 int wipe, status;
918 sigset_t blocked, oldset;
919 struct buffer_head *di_bh = NULL;
920
921 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
922
Jan Karaa90714c2008-10-09 19:38:40 +0200923 /* When we fail in read_inode() we mark inode as bad. The second test
924 * catches the case when inode allocation fails before allocating
925 * a block for inode. */
926 if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800927 mlog(0, "Skipping delete of bad inode\n");
928 goto bail;
929 }
930
931 if (!ocfs2_inode_is_valid_to_delete(inode)) {
932 /* It's probably not necessary to truncate_inode_pages
933 * here but we do it for safety anyway (it will most
934 * likely be a no-op anyway) */
935 ocfs2_cleanup_delete_inode(inode, 0);
936 goto bail;
937 }
938
939 /* We want to block signals in delete_inode as the lock and
940 * messaging paths may return us -ERESTARTSYS. Which would
941 * cause us to exit early, resulting in inodes being orphaned
942 * forever. */
943 sigfillset(&blocked);
944 status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
945 if (status < 0) {
946 mlog_errno(status);
947 ocfs2_cleanup_delete_inode(inode, 1);
948 goto bail;
949 }
950
951 /* Lock down the inode. This gives us an up to date view of
952 * it's metadata (for verification), and allows us to
Mark Fasheh34d024f2007-09-24 15:56:19 -0700953 * serialize delete_inode on multiple nodes.
Mark Fashehccd979b2005-12-15 14:31:24 -0800954 *
955 * Even though we might be doing a truncate, we don't take the
956 * allocation lock here as it won't be needed - nobody will
957 * have the file open.
958 */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700959 status = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800960 if (status < 0) {
961 if (status != -ENOENT)
962 mlog_errno(status);
963 ocfs2_cleanup_delete_inode(inode, 0);
964 goto bail_unblock;
965 }
966
967 /* Query the cluster. This will be the final decision made
968 * before we go ahead and wipe the inode. */
969 status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
970 if (!wipe || status < 0) {
Mark Fasheh34d024f2007-09-24 15:56:19 -0700971 /* Error and remote inode busy both mean we won't be
Mark Fashehccd979b2005-12-15 14:31:24 -0800972 * removing the inode, so they take almost the same
973 * path. */
974 if (status < 0)
975 mlog_errno(status);
976
Mark Fasheh34d024f2007-09-24 15:56:19 -0700977 /* Someone in the cluster has disallowed a wipe of
978 * this inode, or it was never completely
979 * orphaned. Write out the pages and exit now. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 ocfs2_cleanup_delete_inode(inode, 1);
981 goto bail_unlock_inode;
982 }
983
984 ocfs2_cleanup_delete_inode(inode, 0);
985
986 status = ocfs2_wipe_inode(inode, di_bh);
987 if (status < 0) {
Mark Fashehb4df6ed2006-02-22 17:35:08 -0800988 if (status != -EDEADLK)
989 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800990 goto bail_unlock_inode;
991 }
992
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700993 /*
994 * Mark the inode as successfully deleted.
995 *
996 * This is important for ocfs2_clear_inode() as it will check
997 * this flag and skip any checkpointing work
998 *
999 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
1000 * the LVB for other nodes.
1001 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1003
1004bail_unlock_inode:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001005 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001006 brelse(di_bh);
1007bail_unblock:
1008 status = sigprocmask(SIG_SETMASK, &oldset, NULL);
1009 if (status < 0)
1010 mlog_errno(status);
1011bail:
1012 clear_inode(inode);
1013 mlog_exit_void();
1014}
1015
1016void ocfs2_clear_inode(struct inode *inode)
1017{
1018 int status;
1019 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1020
1021 mlog_entry_void();
1022
1023 if (!inode)
1024 goto bail;
1025
Mark Fashehb06970532006-03-03 10:24:33 -08001026 mlog(0, "Clearing inode: %llu, nlink = %u\n",
1027 (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
Mark Fashehccd979b2005-12-15 14:31:24 -08001028
1029 mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1030 "Inode=%lu\n", inode->i_ino);
1031
Mark Fasheh34d024f2007-09-24 15:56:19 -07001032 /* To preven remote deletes we hold open lock before, now it
1033 * is time to unlock PR and EX open locks. */
Tiger Yang50008632007-03-20 16:01:38 -07001034 ocfs2_open_unlock(inode);
1035
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 /* Do these before all the other work so that we don't bounce
Mark Fasheh34d024f2007-09-24 15:56:19 -07001037 * the downconvert thread while waiting to destroy the locks. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001038 ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001039 ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
Tiger Yang50008632007-03-20 16:01:38 -07001040 ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001041
1042 /* We very well may get a clear_inode before all an inodes
1043 * metadata has hit disk. Of course, we can't drop any cluster
1044 * locks until the journal has finished with it. The only
1045 * exception here are successfully wiped inodes - their
1046 * metadata can now be considered to be part of the system
1047 * inodes from which it came. */
1048 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
1049 ocfs2_checkpoint_inode(inode);
1050
1051 mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
Mark Fashehb06970532006-03-03 10:24:33 -08001052 "Clear inode of %llu, inode has io markers\n",
1053 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001054
Mark Fasheh83418972007-04-23 18:53:12 -07001055 ocfs2_extent_map_trunc(inode, 0);
1056
Mark Fashehccd979b2005-12-15 14:31:24 -08001057 status = ocfs2_drop_inode_locks(inode);
1058 if (status < 0)
1059 mlog_errno(status);
1060
1061 ocfs2_lock_res_free(&oi->ip_rw_lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001062 ocfs2_lock_res_free(&oi->ip_inode_lockres);
Tiger Yang50008632007-03-20 16:01:38 -07001063 ocfs2_lock_res_free(&oi->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001064
1065 ocfs2_metadata_cache_purge(inode);
1066
1067 mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
Mark Fashehb06970532006-03-03 10:24:33 -08001068 "Clear inode of %llu, inode has %u cache items\n",
1069 (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
Mark Fashehccd979b2005-12-15 14:31:24 -08001070
1071 mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
Mark Fashehb06970532006-03-03 10:24:33 -08001072 "Clear inode of %llu, inode has a bad flag\n",
1073 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001074
1075 mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
Mark Fashehb06970532006-03-03 10:24:33 -08001076 "Clear inode of %llu, inode is locked\n",
1077 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001078
Mark Fasheh251b6ec2006-01-10 15:41:43 -08001079 mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
Mark Fashehb06970532006-03-03 10:24:33 -08001080 "Clear inode of %llu, io_mutex is locked\n",
1081 (unsigned long long)oi->ip_blkno);
Mark Fasheh251b6ec2006-01-10 15:41:43 -08001082 mutex_unlock(&oi->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001083
1084 /*
1085 * down_trylock() returns 0, down_write_trylock() returns 1
1086 * kernel 1, world 0
1087 */
1088 mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
Mark Fashehb06970532006-03-03 10:24:33 -08001089 "Clear inode of %llu, alloc_sem is locked\n",
1090 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001091 up_write(&oi->ip_alloc_sem);
1092
1093 mlog_bug_on_msg(oi->ip_open_count,
Mark Fashehb06970532006-03-03 10:24:33 -08001094 "Clear inode of %llu has open count %d\n",
1095 (unsigned long long)oi->ip_blkno, oi->ip_open_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001096
1097 /* Clear all other flags. */
1098 oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
1099 oi->ip_created_trans = 0;
1100 oi->ip_last_trans = 0;
1101 oi->ip_dir_start_lookup = 0;
1102 oi->ip_blkno = 0ULL;
Sunil Mushranae0dff62008-10-22 13:24:29 -07001103
1104 /*
1105 * ip_jinode is used to track txns against this inode. We ensure that
1106 * the journal is flushed before journal shutdown. Thus it is safe to
1107 * have inodes get cleaned up after journal shutdown.
1108 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07001109 jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
1110 &oi->ip_jinode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001111
1112bail:
1113 mlog_exit_void();
1114}
1115
1116/* Called under inode_lock, with no more references on the
1117 * struct inode, so it's safe here to check the flags field
1118 * and to manipulate i_nlink without any other locks. */
1119void ocfs2_drop_inode(struct inode *inode)
1120{
1121 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1122
1123 mlog_entry_void();
1124
Mark Fashehb06970532006-03-03 10:24:33 -08001125 mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
1126 (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001127
Mark Fasheh379dfe92006-09-08 14:21:03 -07001128 if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
1129 generic_delete_inode(inode);
1130 else
1131 generic_drop_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001132
1133 mlog_exit_void();
1134}
1135
1136/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001137 * This is called from our getattr.
1138 */
1139int ocfs2_inode_revalidate(struct dentry *dentry)
1140{
1141 struct inode *inode = dentry->d_inode;
1142 int status = 0;
1143
Mark Fashehb06970532006-03-03 10:24:33 -08001144 mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
1145 inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001146
1147 if (!inode) {
1148 mlog(0, "eep, no inode!\n");
1149 status = -ENOENT;
1150 goto bail;
1151 }
1152
1153 spin_lock(&OCFS2_I(inode)->ip_lock);
1154 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1155 spin_unlock(&OCFS2_I(inode)->ip_lock);
1156 mlog(0, "inode deleted!\n");
1157 status = -ENOENT;
1158 goto bail;
1159 }
1160 spin_unlock(&OCFS2_I(inode)->ip_lock);
1161
Mark Fashehe63aecb62007-10-18 15:30:42 -07001162 /* Let ocfs2_inode_lock do the work of updating our struct
Mark Fashehccd979b2005-12-15 14:31:24 -08001163 * inode for us. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001164 status = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 if (status < 0) {
1166 if (status != -ENOENT)
1167 mlog_errno(status);
1168 goto bail;
1169 }
Mark Fashehe63aecb62007-10-18 15:30:42 -07001170 ocfs2_inode_unlock(inode, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001171bail:
1172 mlog_exit(status);
1173
1174 return status;
1175}
1176
1177/*
1178 * Updates a disk inode from a
1179 * struct inode.
1180 * Only takes ip_lock.
1181 */
Mark Fasheh1fabe142006-10-09 18:11:45 -07001182int ocfs2_mark_inode_dirty(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001183 struct inode *inode,
1184 struct buffer_head *bh)
1185{
1186 int status;
1187 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1188
Mark Fashehb06970532006-03-03 10:24:33 -08001189 mlog_entry("(inode %llu)\n",
1190 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001191
1192 status = ocfs2_journal_access(handle, inode, bh,
1193 OCFS2_JOURNAL_ACCESS_WRITE);
1194 if (status < 0) {
1195 mlog_errno(status);
1196 goto leave;
1197 }
1198
1199 spin_lock(&OCFS2_I(inode)->ip_lock);
1200 fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
Jan Kara6e4b0d52007-04-27 11:08:01 -07001201 ocfs2_get_inode_flags(OCFS2_I(inode));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001202 fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001203 fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
Mark Fashehccd979b2005-12-15 14:31:24 -08001204 spin_unlock(&OCFS2_I(inode)->ip_lock);
1205
1206 fe->i_size = cpu_to_le64(i_size_read(inode));
1207 fe->i_links_count = cpu_to_le16(inode->i_nlink);
1208 fe->i_uid = cpu_to_le32(inode->i_uid);
1209 fe->i_gid = cpu_to_le32(inode->i_gid);
1210 fe->i_mode = cpu_to_le16(inode->i_mode);
1211 fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
1212 fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
1213 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1214 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1215 fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
1216 fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1217
1218 status = ocfs2_journal_dirty(handle, bh);
1219 if (status < 0)
1220 mlog_errno(status);
1221
1222 status = 0;
1223leave:
1224
1225 mlog_exit(status);
1226 return status;
1227}
1228
1229/*
1230 *
1231 * Updates a struct inode from a disk inode.
1232 * does no i/o, only takes ip_lock.
1233 */
1234void ocfs2_refresh_inode(struct inode *inode,
1235 struct ocfs2_dinode *fe)
1236{
Mark Fashehccd979b2005-12-15 14:31:24 -08001237 spin_lock(&OCFS2_I(inode)->ip_lock);
1238
1239 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001240 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001241 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001242 ocfs2_set_inode_flags(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001243 i_size_write(inode, le64_to_cpu(fe->i_size));
1244 inode->i_nlink = le16_to_cpu(fe->i_links_count);
1245 inode->i_uid = le32_to_cpu(fe->i_uid);
1246 inode->i_gid = le32_to_cpu(fe->i_gid);
1247 inode->i_mode = le16_to_cpu(fe->i_mode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001248 if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1249 inode->i_blocks = 0;
1250 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001251 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001252 inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
1253 inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
1254 inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
1255 inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
1256 inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
1257 inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
1258
1259 spin_unlock(&OCFS2_I(inode)->ip_lock);
1260}
Joel Beckerb657c952008-11-13 14:49:11 -08001261
1262int ocfs2_validate_inode_block(struct super_block *sb,
1263 struct buffer_head *bh)
1264{
1265 int rc = -EINVAL;
1266 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1267
Joel Becker970e4932008-11-13 14:49:19 -08001268 mlog(0, "Validating dinode %llu\n",
1269 (unsigned long long)bh->b_blocknr);
1270
Joel Beckerb657c952008-11-13 14:49:11 -08001271 BUG_ON(!buffer_uptodate(bh));
1272
1273 if (!OCFS2_IS_VALID_DINODE(di)) {
1274 ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
1275 (unsigned long long)bh->b_blocknr, 7,
1276 di->i_signature);
1277 goto bail;
1278 }
1279
1280 if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
1281 ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
1282 (unsigned long long)bh->b_blocknr,
1283 (unsigned long long)le64_to_cpu(di->i_blkno));
1284 goto bail;
1285 }
1286
1287 if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
1288 ocfs2_error(sb,
1289 "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
1290 (unsigned long long)bh->b_blocknr);
1291 goto bail;
1292 }
1293
1294 if (le32_to_cpu(di->i_fs_generation) !=
1295 OCFS2_SB(sb)->fs_generation) {
1296 ocfs2_error(sb,
1297 "Invalid dinode #%llu: fs_generation is %u\n",
1298 (unsigned long long)bh->b_blocknr,
1299 le32_to_cpu(di->i_fs_generation));
1300 goto bail;
1301 }
1302
1303 rc = 0;
1304
1305bail:
1306 return rc;
1307}
1308
1309int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
1310 int flags)
1311{
1312 int rc;
1313 struct buffer_head *tmp = *bh;
1314
1315 rc = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -08001316 flags, ocfs2_validate_inode_block);
Joel Beckerb657c952008-11-13 14:49:11 -08001317
1318 /* If ocfs2_read_blocks() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -08001319 if (!rc && !*bh)
Joel Beckerb657c952008-11-13 14:49:11 -08001320 *bh = tmp;
1321
Joel Beckerb657c952008-11-13 14:49:11 -08001322 return rc;
1323}
1324
1325int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
1326{
1327 return ocfs2_read_inode_block_full(inode, bh, 0);
1328}