ocfs2: Add quota calls for allocation and freeing of inodes and space
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / inode.c
blob288512c9dbc27fc87c94aaf3ee4fa374e1b504cc
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * inode.c
6 * vfs' aops, fops, dops and iops
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
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.
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.
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.
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>
31 #include <linux/quotaops.h>
33 #include <asm/byteorder.h>
35 #define MLOG_MASK_PREFIX ML_INODE
36 #include <cluster/masklog.h>
38 #include "ocfs2.h"
40 #include "alloc.h"
41 #include "dlmglue.h"
42 #include "extent_map.h"
43 #include "file.h"
44 #include "heartbeat.h"
45 #include "inode.h"
46 #include "journal.h"
47 #include "namei.h"
48 #include "suballoc.h"
49 #include "super.h"
50 #include "symlink.h"
51 #include "sysfile.h"
52 #include "uptodate.h"
53 #include "xattr.h"
55 #include "buffer_head_io.h"
57 struct ocfs2_find_inode_args
59 u64 fi_blkno;
60 unsigned long fi_ino;
61 unsigned int fi_flags;
62 unsigned int fi_sysfile_type;
65 static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
67 static int ocfs2_read_locked_inode(struct inode *inode,
68 struct ocfs2_find_inode_args *args);
69 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
70 static int ocfs2_find_actor(struct inode *inode, void *opaque);
71 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
72 struct inode *inode,
73 struct buffer_head *fe_bh);
75 void ocfs2_set_inode_flags(struct inode *inode)
77 unsigned int flags = OCFS2_I(inode)->ip_attr;
79 inode->i_flags &= ~(S_IMMUTABLE |
80 S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
82 if (flags & OCFS2_IMMUTABLE_FL)
83 inode->i_flags |= S_IMMUTABLE;
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;
95 /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
96 void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
98 unsigned int flags = oi->vfs_inode.i_flags;
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;
114 struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
115 int sysfile_type)
117 struct inode *inode = NULL;
118 struct super_block *sb = osb->sb;
119 struct ocfs2_find_inode_args args;
121 mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
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;
132 args.fi_blkno = blkno;
133 args.fi_flags = flags;
134 args.fi_ino = ino_from_blkno(sb, blkno);
135 args.fi_sysfile_type = sysfile_type;
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);
147 if (inode == NULL) {
148 inode = ERR_PTR(-ENOMEM);
149 mlog_errno(PTR_ERR(inode));
150 goto bail;
152 if (is_bad_inode(inode)) {
153 iput(inode);
154 inode = ERR_PTR(-ESTALE);
155 goto bail;
158 bail:
159 if (!IS_ERR(inode)) {
160 mlog(0, "returning inode with number %llu\n",
161 (unsigned long long)OCFS2_I(inode)->ip_blkno);
162 mlog_exit_ptr(inode);
165 return inode;
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
176 static int ocfs2_find_actor(struct inode *inode, void *opaque)
178 struct ocfs2_find_inode_args *args = NULL;
179 struct ocfs2_inode_info *oi = OCFS2_I(inode);
180 int ret = 0;
182 mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
184 args = opaque;
186 mlog_bug_on_msg(!inode, "No inode in find actor!\n");
188 if (oi->ip_blkno != args->fi_blkno)
189 goto bail;
191 ret = 1;
192 bail:
193 mlog_exit(ret);
194 return ret;
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
202 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
204 struct ocfs2_find_inode_args *args = opaque;
206 mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
208 inode->i_ino = args->fi_ino;
209 OCFS2_I(inode)->ip_blkno = args->fi_blkno;
210 if (args->fi_sysfile_type != 0)
211 lockdep_set_class(&inode->i_mutex,
212 &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
214 mlog_exit(0);
215 return 0;
218 void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
219 int create_ino)
221 struct super_block *sb;
222 struct ocfs2_super *osb;
223 int use_plocks = 1;
225 mlog_entry("(0x%p, size:%llu)\n", inode,
226 (unsigned long long)le64_to_cpu(fe->i_size));
228 sb = inode->i_sb;
229 osb = OCFS2_SB(sb);
231 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
232 ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
233 use_plocks = 0;
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.
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);
247 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
248 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
249 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
251 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);
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
262 inode->i_blocks = ocfs2_inode_sector_count(inode);
263 inode->i_mapping->a_ops = &ocfs2_aops;
264 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);
271 if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
272 mlog(ML_ERROR,
273 "ip_blkno %llu != i_blkno %llu!\n",
274 (unsigned long long)OCFS2_I(inode)->ip_blkno,
275 (unsigned long long)le64_to_cpu(fe->i_blkno));
277 inode->i_nlink = le16_to_cpu(fe->i_links_count);
279 if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
280 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
281 inode->i_flags |= S_NOQUOTA;
284 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;
289 } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
290 inode->i_flags |= S_NOQUOTA;
291 } 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();
298 switch (inode->i_mode & S_IFMT) {
299 case S_IFREG:
300 if (use_plocks)
301 inode->i_fop = &ocfs2_fops;
302 else
303 inode->i_fop = &ocfs2_fops_no_plocks;
304 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;
309 if (use_plocks)
310 inode->i_fop = &ocfs2_dops;
311 else
312 inode->i_fop = &ocfs2_dops_no_plocks;
313 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;
329 if (create_ino) {
330 inode->i_ino = ino_from_blkno(inode->i_sb,
331 le64_to_cpu(fe->i_blkno));
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.
338 BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
340 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
341 OCFS2_LOCK_TYPE_META, 0, inode);
343 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
344 OCFS2_LOCK_TYPE_OPEN, 0, inode);
347 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
348 OCFS2_LOCK_TYPE_RW, inode->i_generation,
349 inode);
351 ocfs2_set_inode_flags(inode);
353 mlog_exit_void();
356 static int ocfs2_read_locked_inode(struct inode *inode,
357 struct ocfs2_find_inode_args *args)
359 struct super_block *sb;
360 struct ocfs2_super *osb;
361 struct ocfs2_dinode *fe;
362 struct buffer_head *bh = NULL;
363 int status, can_lock;
364 u32 generation = 0;
366 mlog_entry("(0x%p, 0x%p)\n", inode, args);
368 status = -EINVAL;
369 if (inode == NULL || inode->i_sb == NULL) {
370 mlog(ML_ERROR, "bad inode\n");
371 return status;
373 sb = inode->i_sb;
374 osb = OCFS2_SB(sb);
376 if (!args) {
377 mlog(ML_ERROR, "bad inode args\n");
378 make_bad_inode(inode);
379 return status;
383 * To improve performance of cold-cache inode stats, we take
384 * the cluster lock here if possible.
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.
391 * There are three times when taking the lock is not a good idea:
393 * 1) During startup, before we have initialized the DLM.
395 * 2) If we are reading certain system files which never get
396 * cluster locks (local alloc, truncate log).
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().
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.
408 * #3 works itself out because we'll eventually take the
409 * cluster lock before trusting anything anyway.
411 can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
412 && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
413 && !ocfs2_mount_local(osb);
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.
422 if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
423 generation = osb->fs_generation;
425 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
426 OCFS2_LOCK_TYPE_META,
427 generation, inode);
429 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
430 OCFS2_LOCK_TYPE_OPEN,
431 0, inode);
433 if (can_lock) {
434 status = ocfs2_open_lock(inode);
435 if (status) {
436 make_bad_inode(inode);
437 mlog_errno(status);
438 return status;
440 status = ocfs2_inode_lock(inode, NULL, 0);
441 if (status) {
442 make_bad_inode(inode);
443 mlog_errno(status);
444 return status;
448 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;
456 if (can_lock) {
457 status = ocfs2_read_inode_block_full(inode, &bh,
458 OCFS2_BH_IGNORE_CACHE);
459 } else {
460 status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
461 if (!status)
462 status = ocfs2_validate_inode_block(osb->sb, bh);
464 if (status < 0) {
465 mlog_errno(status);
466 goto bail;
469 status = -EINVAL;
470 fe = (struct ocfs2_dinode *) bh->b_data;
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.
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);
482 if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
483 S_ISBLK(le16_to_cpu(fe->i_mode)))
484 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
486 ocfs2_populate_inode(inode, fe, 0);
488 BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
490 status = 0;
492 bail:
493 if (can_lock)
494 ocfs2_inode_unlock(inode, 0);
496 if (status < 0)
497 make_bad_inode(inode);
499 if (args && bh)
500 brelse(bh);
502 mlog_exit(status);
503 return status;
506 void ocfs2_sync_blockdev(struct super_block *sb)
508 sync_blockdev(sb->s_bdev);
511 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
512 struct inode *inode,
513 struct buffer_head *fe_bh)
515 int status = 0;
516 struct ocfs2_truncate_context *tc = NULL;
517 struct ocfs2_dinode *fe;
518 handle_t *handle = NULL;
520 mlog_entry_void();
522 fe = (struct ocfs2_dinode *) fe_bh->b_data;
525 * This check will also skip truncate of inodes with inline
526 * data and fast symlinks.
528 if (fe->i_clusters) {
529 if (ocfs2_should_order_data(inode))
530 ocfs2_begin_ordered_truncate(inode, 0);
532 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;
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;
546 i_size_write(inode, 0);
548 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
549 if (status < 0) {
550 mlog_errno(status);
551 goto out;
554 ocfs2_commit_trans(osb, handle);
555 handle = NULL;
557 status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
558 if (status < 0) {
559 mlog_errno(status);
560 goto out;
563 status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
564 if (status < 0) {
565 mlog_errno(status);
566 goto out;
570 out:
571 if (handle)
572 ocfs2_commit_trans(osb, handle);
573 mlog_exit(status);
574 return status;
577 static 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)
582 int status;
583 struct inode *inode_alloc_inode = NULL;
584 struct buffer_head *inode_alloc_bh = NULL;
585 handle_t *handle;
586 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
587 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
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;
598 mutex_lock(&inode_alloc_inode->i_mutex);
599 status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
600 if (status < 0) {
601 mutex_unlock(&inode_alloc_inode->i_mutex);
603 mlog_errno(status);
604 goto bail;
607 handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
608 ocfs2_quota_trans_credits(inode->i_sb));
609 if (IS_ERR(handle)) {
610 status = PTR_ERR(handle);
611 mlog_errno(status);
612 goto bail_unlock;
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;
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;
630 di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
631 di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
633 status = ocfs2_journal_dirty(handle, di_bh);
634 if (status < 0) {
635 mlog_errno(status);
636 goto bail_commit;
639 ocfs2_remove_from_cache(inode, di_bh);
640 vfs_dq_free_inode(inode);
642 status = ocfs2_free_dinode(handle, inode_alloc_inode,
643 inode_alloc_bh, di);
644 if (status < 0)
645 mlog_errno(status);
647 bail_commit:
648 ocfs2_commit_trans(osb, handle);
649 bail_unlock:
650 ocfs2_inode_unlock(inode_alloc_inode, 1);
651 mutex_unlock(&inode_alloc_inode->i_mutex);
652 brelse(inode_alloc_bh);
653 bail:
654 iput(inode_alloc_inode);
656 return status;
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.
665 static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
666 int slot)
668 int ret = 0;
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;
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]++;
680 out:
681 spin_unlock(&osb->osb_lock);
682 return ret;
685 static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
686 int slot)
688 spin_lock(&osb->osb_lock);
689 osb->osb_orphan_wipes[slot]--;
690 spin_unlock(&osb->osb_lock);
692 wake_up(&osb->osb_wipe_event);
695 static int ocfs2_wipe_inode(struct inode *inode,
696 struct buffer_head *di_bh)
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);
702 struct ocfs2_dinode *di;
704 di = (struct ocfs2_dinode *) di_bh->b_data;
705 orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
707 status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
708 if (status)
709 return status;
711 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;
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. */
723 mutex_lock(&orphan_dir_inode->i_mutex);
724 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
725 if (status < 0) {
726 mutex_unlock(&orphan_dir_inode->i_mutex);
728 mlog_errno(status);
729 goto bail;
732 /* we do this while holding the orphan dir lock because we
733 * don't want recovery being run from another node to try an
734 * inode delete underneath us -- this will result in two nodes
735 * 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;
742 /*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;
749 status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
750 orphan_dir_bh);
751 if (status < 0)
752 mlog_errno(status);
754 bail_unlock_dir:
755 ocfs2_inode_unlock(orphan_dir_inode, 1);
756 mutex_unlock(&orphan_dir_inode->i_mutex);
757 brelse(orphan_dir_bh);
758 bail:
759 iput(orphan_dir_inode);
760 ocfs2_signal_wipe_completion(osb, orphaned_slot);
762 return status;
765 /* There is a series of simple checks that should be done before a
766 * trylock is even considered. Encapsulate those in this function. */
767 static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
769 int ret = 0;
770 struct ocfs2_inode_info *oi = OCFS2_I(inode);
771 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
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;
780 /* If we're coming from downconvert_thread we can't go into our own
781 * 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. */
785 if (current == osb->dc_task) {
786 mlog(0, "Skipping delete of %lu because we're currently "
787 "in downconvert\n", inode->i_ino);
788 goto bail;
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) {
796 mlog(ML_ERROR, "Skipping delete of system file %llu\n",
797 (unsigned long long)oi->ip_blkno);
798 goto bail_unlock;
801 /* 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. */
804 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;
810 ret = 1;
811 bail_unlock:
812 spin_unlock(&oi->ip_lock);
813 bail:
814 return ret;
817 /* Query the cluster to determine whether we should wipe an inode from
818 * disk or not.
820 * Requires the inode to have the cluster lock. */
821 static int ocfs2_query_inode_wipe(struct inode *inode,
822 struct buffer_head *di_bh,
823 int *wipe)
825 int status = 0;
826 struct ocfs2_inode_info *oi = OCFS2_I(inode);
827 struct ocfs2_dinode *di;
829 *wipe = 0;
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)) {
835 mlog(0, "Skipping delete of %llu because flags changed\n",
836 (unsigned long long)oi->ip_blkno);
837 goto bail;
840 /* Now that we have an up to date inode, we can double check
841 * the link count. */
842 if (inode->i_nlink) {
843 mlog(0, "Skipping delete of %llu because nlink = %u\n",
844 (unsigned long long)oi->ip_blkno, inode->i_nlink);
845 goto bail;
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,
854 "Inode %llu (on-disk %llu) not orphaned! "
855 "Disk flags 0x%x, inode flags 0x%x\n",
856 (unsigned long long)oi->ip_blkno,
857 (unsigned long long)le64_to_cpu(di->i_blkno),
858 le32_to_cpu(di->i_flags), oi->ip_flags);
859 goto bail;
862 /* has someone already deleted us?! baaad... */
863 if (di->i_dtime) {
864 status = -EEXIST;
865 mlog_errno(status);
866 goto bail;
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.
878 * Though we call this with the meta data lock held, the
879 * trylock keeps us from ABBA deadlock.
881 status = ocfs2_try_open_lock(inode, 1);
882 if (status == -EAGAIN) {
883 status = 0;
884 mlog(0, "Skipping delete of %llu because it is in use on "
885 "other nodes\n", (unsigned long long)oi->ip_blkno);
886 goto bail;
888 if (status < 0) {
889 mlog_errno(status);
890 goto bail;
893 *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));
898 bail:
899 return status;
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. */
905 static void ocfs2_cleanup_delete_inode(struct inode *inode,
906 int sync_data)
908 mlog(0, "Cleanup inode %llu, sync = %d\n",
909 (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
910 if (sync_data)
911 write_inode_now(inode, 1);
912 truncate_inode_pages(&inode->i_data, 0);
915 void ocfs2_delete_inode(struct inode *inode)
917 int wipe, status;
918 sigset_t blocked, oldset;
919 struct buffer_head *di_bh = NULL;
921 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
923 /* 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) {
927 mlog(0, "Skipping delete of bad inode\n");
928 goto bail;
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;
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;
951 /* Lock down the inode. This gives us an up to date view of
952 * it's metadata (for verification), and allows us to
953 * serialize delete_inode on multiple nodes.
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.
959 status = ocfs2_inode_lock(inode, &di_bh, 1);
960 if (status < 0) {
961 if (status != -ENOENT)
962 mlog_errno(status);
963 ocfs2_cleanup_delete_inode(inode, 0);
964 goto bail_unblock;
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) {
971 /* Error and remote inode busy both mean we won't be
972 * removing the inode, so they take almost the same
973 * path. */
974 if (status < 0)
975 mlog_errno(status);
977 /* 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. */
980 ocfs2_cleanup_delete_inode(inode, 1);
981 goto bail_unlock_inode;
984 ocfs2_cleanup_delete_inode(inode, 0);
986 status = ocfs2_wipe_inode(inode, di_bh);
987 if (status < 0) {
988 if (status != -EDEADLK)
989 mlog_errno(status);
990 goto bail_unlock_inode;
994 * Mark the inode as successfully deleted.
996 * This is important for ocfs2_clear_inode() as it will check
997 * this flag and skip any checkpointing work
999 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
1000 * the LVB for other nodes.
1002 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
1004 bail_unlock_inode:
1005 ocfs2_inode_unlock(inode, 1);
1006 brelse(di_bh);
1007 bail_unblock:
1008 status = sigprocmask(SIG_SETMASK, &oldset, NULL);
1009 if (status < 0)
1010 mlog_errno(status);
1011 bail:
1012 clear_inode(inode);
1013 mlog_exit_void();
1016 void ocfs2_clear_inode(struct inode *inode)
1018 int status;
1019 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1021 mlog_entry_void();
1023 if (!inode)
1024 goto bail;
1026 mlog(0, "Clearing inode: %llu, nlink = %u\n",
1027 (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
1029 mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
1030 "Inode=%lu\n", inode->i_ino);
1032 /* To preven remote deletes we hold open lock before, now it
1033 * is time to unlock PR and EX open locks. */
1034 ocfs2_open_unlock(inode);
1036 /* Do these before all the other work so that we don't bounce
1037 * the downconvert thread while waiting to destroy the locks. */
1038 ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
1039 ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
1040 ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
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);
1051 mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
1052 "Clear inode of %llu, inode has io markers\n",
1053 (unsigned long long)oi->ip_blkno);
1055 ocfs2_extent_map_trunc(inode, 0);
1057 status = ocfs2_drop_inode_locks(inode);
1058 if (status < 0)
1059 mlog_errno(status);
1061 ocfs2_lock_res_free(&oi->ip_rw_lockres);
1062 ocfs2_lock_res_free(&oi->ip_inode_lockres);
1063 ocfs2_lock_res_free(&oi->ip_open_lockres);
1065 ocfs2_metadata_cache_purge(inode);
1067 mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
1068 "Clear inode of %llu, inode has %u cache items\n",
1069 (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
1071 mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
1072 "Clear inode of %llu, inode has a bad flag\n",
1073 (unsigned long long)oi->ip_blkno);
1075 mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
1076 "Clear inode of %llu, inode is locked\n",
1077 (unsigned long long)oi->ip_blkno);
1079 mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
1080 "Clear inode of %llu, io_mutex is locked\n",
1081 (unsigned long long)oi->ip_blkno);
1082 mutex_unlock(&oi->ip_io_mutex);
1085 * down_trylock() returns 0, down_write_trylock() returns 1
1086 * kernel 1, world 0
1088 mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
1089 "Clear inode of %llu, alloc_sem is locked\n",
1090 (unsigned long long)oi->ip_blkno);
1091 up_write(&oi->ip_alloc_sem);
1093 mlog_bug_on_msg(oi->ip_open_count,
1094 "Clear inode of %llu has open count %d\n",
1095 (unsigned long long)oi->ip_blkno, oi->ip_open_count);
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;
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.
1109 jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
1110 &oi->ip_jinode);
1112 bail:
1113 mlog_exit_void();
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. */
1119 void ocfs2_drop_inode(struct inode *inode)
1121 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1123 mlog_entry_void();
1125 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);
1128 if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
1129 generic_delete_inode(inode);
1130 else
1131 generic_drop_inode(inode);
1133 mlog_exit_void();
1137 * This is called from our getattr.
1139 int ocfs2_inode_revalidate(struct dentry *dentry)
1141 struct inode *inode = dentry->d_inode;
1142 int status = 0;
1144 mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
1145 inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
1147 if (!inode) {
1148 mlog(0, "eep, no inode!\n");
1149 status = -ENOENT;
1150 goto bail;
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;
1160 spin_unlock(&OCFS2_I(inode)->ip_lock);
1162 /* Let ocfs2_inode_lock do the work of updating our struct
1163 * inode for us. */
1164 status = ocfs2_inode_lock(inode, NULL, 0);
1165 if (status < 0) {
1166 if (status != -ENOENT)
1167 mlog_errno(status);
1168 goto bail;
1170 ocfs2_inode_unlock(inode, 0);
1171 bail:
1172 mlog_exit(status);
1174 return status;
1178 * Updates a disk inode from a
1179 * struct inode.
1180 * Only takes ip_lock.
1182 int ocfs2_mark_inode_dirty(handle_t *handle,
1183 struct inode *inode,
1184 struct buffer_head *bh)
1186 int status;
1187 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1189 mlog_entry("(inode %llu)\n",
1190 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1192 status = ocfs2_journal_access(handle, inode, bh,
1193 OCFS2_JOURNAL_ACCESS_WRITE);
1194 if (status < 0) {
1195 mlog_errno(status);
1196 goto leave;
1199 spin_lock(&OCFS2_I(inode)->ip_lock);
1200 fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
1201 ocfs2_get_inode_flags(OCFS2_I(inode));
1202 fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
1203 fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
1204 spin_unlock(&OCFS2_I(inode)->ip_lock);
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);
1218 status = ocfs2_journal_dirty(handle, bh);
1219 if (status < 0)
1220 mlog_errno(status);
1222 status = 0;
1223 leave:
1225 mlog_exit(status);
1226 return status;
1231 * Updates a struct inode from a disk inode.
1232 * does no i/o, only takes ip_lock.
1234 void ocfs2_refresh_inode(struct inode *inode,
1235 struct ocfs2_dinode *fe)
1237 spin_lock(&OCFS2_I(inode)->ip_lock);
1239 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1240 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
1241 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
1242 ocfs2_set_inode_flags(inode);
1243 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);
1248 if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1249 inode->i_blocks = 0;
1250 else
1251 inode->i_blocks = ocfs2_inode_sector_count(inode);
1252 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);
1259 spin_unlock(&OCFS2_I(inode)->ip_lock);
1262 int ocfs2_validate_inode_block(struct super_block *sb,
1263 struct buffer_head *bh)
1265 int rc = -EINVAL;
1266 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1268 mlog(0, "Validating dinode %llu\n",
1269 (unsigned long long)bh->b_blocknr);
1271 BUG_ON(!buffer_uptodate(bh));
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;
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;
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;
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;
1303 rc = 0;
1305 bail:
1306 return rc;
1309 int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
1310 int flags)
1312 int rc;
1313 struct buffer_head *tmp = *bh;
1315 rc = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, &tmp,
1316 flags, ocfs2_validate_inode_block);
1318 /* If ocfs2_read_blocks() got us a new bh, pass it up. */
1319 if (!rc && !*bh)
1320 *bh = tmp;
1322 return rc;
1325 int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
1327 return ocfs2_read_inode_block_full(inode, bh, 0);