]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ocfs2/inode.c
ocfs2: Add dentry tracking API
[net-next-2.6.git] / fs / ocfs2 / inode.c
CommitLineData
ccd979bd
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * inode.c
5 *
6 * vfs' aops, fops, dops and iops
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
30#include <linux/pagemap.h>
31#include <linux/smp_lock.h>
32
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"
b4df6ed8 44#include "heartbeat.h"
ccd979bd
MF
45#include "inode.h"
46#include "journal.h"
47#include "namei.h"
48#include "suballoc.h"
49#include "super.h"
50#include "symlink.h"
51#include "sysfile.h"
52#include "uptodate.h"
53#include "vote.h"
54
55#include "buffer_head_io.h"
56
57#define OCFS2_FI_FLAG_NOWAIT 0x1
58#define OCFS2_FI_FLAG_DELETE 0x2
59struct ocfs2_find_inode_args
60{
61 u64 fi_blkno;
62 unsigned long fi_ino;
63 unsigned int fi_flags;
64};
65
66static int ocfs2_read_locked_inode(struct inode *inode,
67 struct ocfs2_find_inode_args *args);
68static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
69static int ocfs2_find_actor(struct inode *inode, void *opaque);
70static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
71 struct inode *inode,
72 struct buffer_head *fe_bh);
73
ca4d147e
HP
74void ocfs2_set_inode_flags(struct inode *inode)
75{
76 unsigned int flags = OCFS2_I(inode)->ip_attr;
77
78 inode->i_flags &= ~(S_IMMUTABLE |
79 S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
80
81 if (flags & OCFS2_IMMUTABLE_FL)
82 inode->i_flags |= S_IMMUTABLE;
83
84 if (flags & OCFS2_SYNC_FL)
85 inode->i_flags |= S_SYNC;
86 if (flags & OCFS2_APPEND_FL)
87 inode->i_flags |= S_APPEND;
88 if (flags & OCFS2_NOATIME_FL)
89 inode->i_flags |= S_NOATIME;
90 if (flags & OCFS2_DIRSYNC_FL)
91 inode->i_flags |= S_DIRSYNC;
92}
93
ccd979bd
MF
94struct inode *ocfs2_ilookup_for_vote(struct ocfs2_super *osb,
95 u64 blkno,
96 int delete_vote)
97{
98 struct ocfs2_find_inode_args args;
99
100 /* ocfs2_ilookup_for_vote should *only* be called from the
101 * vote thread */
102 BUG_ON(current != osb->vote_task);
103
104 args.fi_blkno = blkno;
105 args.fi_flags = OCFS2_FI_FLAG_NOWAIT;
106 if (delete_vote)
107 args.fi_flags |= OCFS2_FI_FLAG_DELETE;
108 args.fi_ino = ino_from_blkno(osb->sb, blkno);
109 return ilookup5(osb->sb, args.fi_ino, ocfs2_find_actor, &args);
110}
111
112struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno)
113{
114 struct inode *inode = NULL;
115 struct super_block *sb = osb->sb;
116 struct ocfs2_find_inode_args args;
117
b0697053 118 mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
ccd979bd
MF
119
120 /* Ok. By now we've either got the offsets passed to us by the
121 * caller, or we just pulled them off the bh. Lets do some
122 * sanity checks to make sure they're OK. */
123 if (blkno == 0) {
124 inode = ERR_PTR(-EINVAL);
125 mlog_errno(PTR_ERR(inode));
126 goto bail;
127 }
128
129 args.fi_blkno = blkno;
130 args.fi_flags = 0;
131 args.fi_ino = ino_from_blkno(sb, blkno);
132
133 inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
134 ocfs2_init_locked_inode, &args);
135 /* inode was *not* in the inode cache. 2.6.x requires
136 * us to do our own read_inode call and unlock it
137 * afterwards. */
138 if (inode && inode->i_state & I_NEW) {
139 mlog(0, "Inode was not in inode cache, reading it.\n");
140 ocfs2_read_locked_inode(inode, &args);
141 unlock_new_inode(inode);
142 }
143 if (inode == NULL) {
144 inode = ERR_PTR(-ENOMEM);
145 mlog_errno(PTR_ERR(inode));
146 goto bail;
147 }
148 if (is_bad_inode(inode)) {
149 iput(inode);
150 inode = ERR_PTR(-ESTALE);
151 mlog_errno(PTR_ERR(inode));
152 goto bail;
153 }
154
155bail:
156 if (!IS_ERR(inode)) {
b0697053
MF
157 mlog(0, "returning inode with number %llu\n",
158 (unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd
MF
159 mlog_exit_ptr(inode);
160 } else
161 mlog_errno(PTR_ERR(inode));
162
163 return inode;
164}
165
166
167/*
168 * here's how inodes get read from disk:
169 * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
170 * found? : return the in-memory inode
171 * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
172 */
173
174static int ocfs2_find_actor(struct inode *inode, void *opaque)
175{
176 struct ocfs2_find_inode_args *args = NULL;
177 struct ocfs2_inode_info *oi = OCFS2_I(inode);
178 int ret = 0;
179
180 mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
181
182 args = opaque;
183
184 mlog_bug_on_msg(!inode, "No inode in find actor!\n");
185
186 if (oi->ip_blkno != args->fi_blkno)
187 goto bail;
188
189 /* OCFS2_FI_FLAG_NOWAIT is *only* set from
190 * ocfs2_ilookup_for_vote which won't create an inode for one
191 * that isn't found. The vote thread which doesn't want to get
192 * an inode which is in the process of going away - otherwise
193 * the call to __wait_on_freeing_inode in find_inode_fast will
194 * cause it to deadlock on an inode which may be waiting on a
195 * vote (or lock release) in delete_inode */
196 if ((args->fi_flags & OCFS2_FI_FLAG_NOWAIT) &&
197 (inode->i_state & (I_FREEING|I_CLEAR))) {
198 /* As stated above, we're not going to return an
199 * inode. In the case of a delete vote, the voting
200 * code is going to signal the other node to go
201 * ahead. Mark that state here, so this freeing inode
202 * has the state when it gets to delete_inode. */
203 if (args->fi_flags & OCFS2_FI_FLAG_DELETE) {
204 spin_lock(&oi->ip_lock);
205 ocfs2_mark_inode_remotely_deleted(inode);
206 spin_unlock(&oi->ip_lock);
207 }
208 goto bail;
209 }
210
211 ret = 1;
212bail:
213 mlog_exit(ret);
214 return ret;
215}
216
217/*
218 * initialize the new inode, but don't do anything that would cause
219 * us to sleep.
220 * return 0 on success, 1 on failure
221 */
222static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
223{
224 struct ocfs2_find_inode_args *args = opaque;
225
226 mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
227
228 inode->i_ino = args->fi_ino;
229 OCFS2_I(inode)->ip_blkno = args->fi_blkno;
230
231 mlog_exit(0);
232 return 0;
233}
234
235int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
236 int create_ino)
237{
238 struct super_block *sb;
239 struct ocfs2_super *osb;
240 int status = -EINVAL;
241
b0697053
MF
242 mlog_entry("(0x%p, size:%llu)\n", inode,
243 (unsigned long long)fe->i_size);
ccd979bd
MF
244
245 sb = inode->i_sb;
246 osb = OCFS2_SB(sb);
247
248 /* this means that read_inode cannot create a superblock inode
249 * today. change if needed. */
250 if (!OCFS2_IS_VALID_DINODE(fe) ||
251 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
b0697053 252 mlog(ML_ERROR, "Invalid dinode: i_ino=%lu, i_blkno=%llu, "
ccd979bd 253 "signature = %.*s, flags = 0x%x\n",
b0697053
MF
254 inode->i_ino,
255 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
ccd979bd
MF
256 fe->i_signature, le32_to_cpu(fe->i_flags));
257 goto bail;
258 }
259
260 if (le32_to_cpu(fe->i_fs_generation) != osb->fs_generation) {
261 mlog(ML_ERROR, "file entry generation does not match "
262 "superblock! osb->fs_generation=%x, "
263 "fe->i_fs_generation=%x\n",
264 osb->fs_generation, le32_to_cpu(fe->i_fs_generation));
265 goto bail;
266 }
267
268 inode->i_version = 1;
269 inode->i_generation = le32_to_cpu(fe->i_generation);
270 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
271 inode->i_mode = le16_to_cpu(fe->i_mode);
272 inode->i_uid = le32_to_cpu(fe->i_uid);
273 inode->i_gid = le32_to_cpu(fe->i_gid);
274 inode->i_blksize = (u32)osb->s_clustersize;
275
276 /* Fast symlinks will have i_size but no allocated clusters. */
277 if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
278 inode->i_blocks = 0;
279 else
280 inode->i_blocks =
281 ocfs2_align_bytes_to_sectors(le64_to_cpu(fe->i_size));
282 inode->i_mapping->a_ops = &ocfs2_aops;
ccd979bd
MF
283 inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
284 inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
285 inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
286 inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
287 inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
288 inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
289
290 if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
291 mlog(ML_ERROR,
b0697053
MF
292 "ip_blkno %llu != i_blkno %llu!\n",
293 (unsigned long long)OCFS2_I(inode)->ip_blkno,
294 (unsigned long long)fe->i_blkno);
ccd979bd
MF
295
296 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
297 OCFS2_I(inode)->ip_orphaned_slot = OCFS2_INVALID_SLOT;
ca4d147e 298 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
ccd979bd
MF
299
300 if (create_ino)
301 inode->i_ino = ino_from_blkno(inode->i_sb,
302 le64_to_cpu(fe->i_blkno));
303
b0697053
MF
304 mlog(0, "blkno = %llu, ino = %lu, create_ino = %s\n",
305 (unsigned long long)fe->i_blkno, inode->i_ino, create_ino ? "true" : "false");
ccd979bd
MF
306
307 inode->i_nlink = le16_to_cpu(fe->i_links_count);
308
309 if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
310 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
311 mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
312 } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
313 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
314 } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
315 mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
316 /* we can't actually hit this as read_inode can't
317 * handle superblocks today ;-) */
318 BUG();
319 }
320
321 switch (inode->i_mode & S_IFMT) {
322 case S_IFREG:
323 inode->i_fop = &ocfs2_fops;
324 inode->i_op = &ocfs2_file_iops;
325 i_size_write(inode, le64_to_cpu(fe->i_size));
326 break;
327 case S_IFDIR:
328 inode->i_op = &ocfs2_dir_iops;
329 inode->i_fop = &ocfs2_dops;
330 i_size_write(inode, le64_to_cpu(fe->i_size));
331 break;
332 case S_IFLNK:
333 if (ocfs2_inode_is_fast_symlink(inode))
334 inode->i_op = &ocfs2_fast_symlink_inode_operations;
335 else
336 inode->i_op = &ocfs2_symlink_inode_operations;
337 i_size_write(inode, le64_to_cpu(fe->i_size));
338 break;
339 default:
340 inode->i_op = &ocfs2_special_file_iops;
341 init_special_inode(inode, inode->i_mode,
342 inode->i_rdev);
343 break;
344 }
345
346 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
347 OCFS2_LOCK_TYPE_RW, inode);
348 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_meta_lockres,
349 OCFS2_LOCK_TYPE_META, inode);
350 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_data_lockres,
351 OCFS2_LOCK_TYPE_DATA, inode);
352
ca4d147e
HP
353 ocfs2_set_inode_flags(inode);
354 inode->i_flags |= S_NOATIME;
355
ccd979bd
MF
356 status = 0;
357bail:
358 mlog_exit(status);
359 return status;
360}
361
362static int ocfs2_read_locked_inode(struct inode *inode,
363 struct ocfs2_find_inode_args *args)
364{
365 struct super_block *sb;
366 struct ocfs2_super *osb;
367 struct ocfs2_dinode *fe;
368 struct buffer_head *bh = NULL;
369 int status;
370 int sysfile = 0;
371
372 mlog_entry("(0x%p, 0x%p)\n", inode, args);
373
374 status = -EINVAL;
375 if (inode == NULL || inode->i_sb == NULL) {
376 mlog(ML_ERROR, "bad inode\n");
377 goto bail;
378 }
379 sb = inode->i_sb;
380 osb = OCFS2_SB(sb);
381
382 if (!args) {
383 mlog(ML_ERROR, "bad inode args\n");
384 make_bad_inode(inode);
385 goto bail;
386 }
387
388 /* Read the FE off disk. This is safe because the kernel only
389 * does one read_inode2 for a new inode, and if it doesn't
390 * exist yet then nobody can be working on it! */
391 status = ocfs2_read_block(osb, args->fi_blkno, &bh, 0, NULL);
392 if (status < 0) {
393 mlog_errno(status);
394 make_bad_inode(inode);
395 goto bail;
396 }
397
398 fe = (struct ocfs2_dinode *) bh->b_data;
399 if (!OCFS2_IS_VALID_DINODE(fe)) {
b0697053
MF
400 mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
401 (unsigned long long)fe->i_blkno, 7, fe->i_signature);
ccd979bd
MF
402 make_bad_inode(inode);
403 goto bail;
404 }
405
406 if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL))
407 sysfile = 1;
408
409 if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
410 S_ISBLK(le16_to_cpu(fe->i_mode)))
411 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
412
413 status = -EINVAL;
414 if (ocfs2_populate_inode(inode, fe, 0) < 0) {
b0697053
MF
415 mlog(ML_ERROR, "populate failed! i_blkno=%llu, i_ino=%lu\n",
416 (unsigned long long)fe->i_blkno, inode->i_ino);
ccd979bd
MF
417 make_bad_inode(inode);
418 goto bail;
419 }
420
421 BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
422
423 if (sysfile)
424 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
425
426 status = 0;
427
428bail:
429 if (args && bh)
430 brelse(bh);
431
432 mlog_exit(status);
433 return status;
434}
435
436void ocfs2_sync_blockdev(struct super_block *sb)
437{
438 sync_blockdev(sb->s_bdev);
439}
440
441static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
442 struct inode *inode,
443 struct buffer_head *fe_bh)
444{
445 int status = 0;
446 struct ocfs2_journal_handle *handle = NULL;
447 struct ocfs2_truncate_context *tc = NULL;
448 struct ocfs2_dinode *fe;
449
450 mlog_entry_void();
451
452 fe = (struct ocfs2_dinode *) fe_bh->b_data;
453
454 /* zero allocation, zero truncate :) */
455 if (!fe->i_clusters)
456 goto bail;
457
458 handle = ocfs2_start_trans(osb, handle, OCFS2_INODE_UPDATE_CREDITS);
459 if (IS_ERR(handle)) {
460 status = PTR_ERR(handle);
461 handle = NULL;
462 mlog_errno(status);
463 goto bail;
464 }
465
466 status = ocfs2_set_inode_size(handle, inode, fe_bh, 0ULL);
467 if (status < 0) {
468 mlog_errno(status);
469 goto bail;
470 }
471
472 ocfs2_commit_trans(handle);
473 handle = NULL;
474
475 status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
476 if (status < 0) {
477 mlog_errno(status);
478 goto bail;
479 }
480
481 status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
482 if (status < 0) {
483 mlog_errno(status);
484 goto bail;
485 }
486bail:
487 if (handle)
488 ocfs2_commit_trans(handle);
489
490 mlog_exit(status);
491 return status;
492}
493
494static int ocfs2_remove_inode(struct inode *inode,
495 struct buffer_head *di_bh,
496 struct inode *orphan_dir_inode,
497 struct buffer_head *orphan_dir_bh)
498{
499 int status;
500 struct inode *inode_alloc_inode = NULL;
501 struct buffer_head *inode_alloc_bh = NULL;
502 struct ocfs2_journal_handle *handle;
503 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
504 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
505
506 inode_alloc_inode =
507 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
508 le16_to_cpu(di->i_suballoc_slot));
509 if (!inode_alloc_inode) {
510 status = -EEXIST;
511 mlog_errno(status);
512 goto bail;
513 }
514
1b1dcc1b 515 mutex_lock(&inode_alloc_inode->i_mutex);
ccd979bd
MF
516 status = ocfs2_meta_lock(inode_alloc_inode, NULL, &inode_alloc_bh, 1);
517 if (status < 0) {
1b1dcc1b 518 mutex_unlock(&inode_alloc_inode->i_mutex);
ccd979bd
MF
519
520 mlog_errno(status);
521 goto bail;
522 }
523
524 handle = ocfs2_start_trans(osb, NULL, OCFS2_DELETE_INODE_CREDITS);
525 if (IS_ERR(handle)) {
526 status = PTR_ERR(handle);
527 mlog_errno(status);
528 goto bail_unlock;
529 }
530
531 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
532 orphan_dir_bh);
533 if (status < 0) {
534 mlog_errno(status);
535 goto bail_commit;
536 }
537
538 /* set the inodes dtime */
539 status = ocfs2_journal_access(handle, inode, di_bh,
540 OCFS2_JOURNAL_ACCESS_WRITE);
541 if (status < 0) {
542 mlog_errno(status);
543 goto bail_commit;
544 }
545
546 di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
547 le32_and_cpu(&di->i_flags, ~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
548
549 status = ocfs2_journal_dirty(handle, di_bh);
550 if (status < 0) {
551 mlog_errno(status);
552 goto bail_commit;
553 }
554
555 ocfs2_remove_from_cache(inode, di_bh);
556
557 status = ocfs2_free_dinode(handle, inode_alloc_inode,
558 inode_alloc_bh, di);
559 if (status < 0)
560 mlog_errno(status);
561
562bail_commit:
563 ocfs2_commit_trans(handle);
564bail_unlock:
565 ocfs2_meta_unlock(inode_alloc_inode, 1);
1b1dcc1b 566 mutex_unlock(&inode_alloc_inode->i_mutex);
ccd979bd
MF
567 brelse(inode_alloc_bh);
568bail:
569 iput(inode_alloc_inode);
570
571 return status;
572}
573
b4df6ed8
MF
574/*
575 * Serialize with orphan dir recovery. If the process doing
576 * recovery on this orphan dir does an iget() with the dir
577 * i_mutex held, we'll deadlock here. Instead we detect this
578 * and exit early - recovery will wipe this inode for us.
579 */
580static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
581 int slot)
582{
583 int ret = 0;
584
585 spin_lock(&osb->osb_lock);
586 if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
587 mlog(0, "Recovery is happening on orphan dir %d, will skip "
588 "this inode\n", slot);
589 ret = -EDEADLK;
590 goto out;
591 }
592 /* This signals to the orphan recovery process that it should
593 * wait for us to handle the wipe. */
594 osb->osb_orphan_wipes[slot]++;
595out:
596 spin_unlock(&osb->osb_lock);
597 return ret;
598}
599
600static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
601 int slot)
602{
603 spin_lock(&osb->osb_lock);
604 osb->osb_orphan_wipes[slot]--;
605 spin_unlock(&osb->osb_lock);
606
607 wake_up(&osb->osb_wipe_event);
608}
609
ccd979bd
MF
610static int ocfs2_wipe_inode(struct inode *inode,
611 struct buffer_head *di_bh)
612{
613 int status, orphaned_slot;
614 struct inode *orphan_dir_inode = NULL;
615 struct buffer_head *orphan_dir_bh = NULL;
616 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
617
618 /* We've already voted on this so it should be readonly - no
619 * spinlock needed. */
620 orphaned_slot = OCFS2_I(inode)->ip_orphaned_slot;
b4df6ed8
MF
621
622 status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
623 if (status)
624 return status;
625
ccd979bd
MF
626 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
627 ORPHAN_DIR_SYSTEM_INODE,
628 orphaned_slot);
629 if (!orphan_dir_inode) {
630 status = -EEXIST;
631 mlog_errno(status);
632 goto bail;
633 }
634
635 /* Lock the orphan dir. The lock will be held for the entire
636 * delete_inode operation. We do this now to avoid races with
637 * recovery completion on other nodes. */
1b1dcc1b 638 mutex_lock(&orphan_dir_inode->i_mutex);
ccd979bd
MF
639 status = ocfs2_meta_lock(orphan_dir_inode, NULL, &orphan_dir_bh, 1);
640 if (status < 0) {
1b1dcc1b 641 mutex_unlock(&orphan_dir_inode->i_mutex);
ccd979bd
MF
642
643 mlog_errno(status);
644 goto bail;
645 }
646
647 /* we do this while holding the orphan dir lock because we
648 * don't want recovery being run from another node to vote for
649 * an inode delete on us -- this will result in two nodes
650 * truncating the same file! */
651 status = ocfs2_truncate_for_delete(osb, inode, di_bh);
652 if (status < 0) {
653 mlog_errno(status);
654 goto bail_unlock_dir;
655 }
656
657 status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
658 orphan_dir_bh);
659 if (status < 0)
660 mlog_errno(status);
661
662bail_unlock_dir:
663 ocfs2_meta_unlock(orphan_dir_inode, 1);
1b1dcc1b 664 mutex_unlock(&orphan_dir_inode->i_mutex);
ccd979bd
MF
665 brelse(orphan_dir_bh);
666bail:
667 iput(orphan_dir_inode);
b4df6ed8 668 ocfs2_signal_wipe_completion(osb, orphaned_slot);
ccd979bd
MF
669
670 return status;
671}
672
673/* There is a series of simple checks that should be done before a
674 * vote is even considered. Encapsulate those in this function. */
675static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
676{
677 int ret = 0;
678 struct ocfs2_inode_info *oi = OCFS2_I(inode);
679 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
680
681 /* We shouldn't be getting here for the root directory
682 * inode.. */
683 if (inode == osb->root_inode) {
684 mlog(ML_ERROR, "Skipping delete of root inode.\n");
685 goto bail;
686 }
687
688 /* If we're coming from process_vote we can't go into our own
689 * voting [hello, deadlock city!], so unforuntately we just
690 * have to skip deleting this guy. That's OK though because
691 * the node who's doing the actual deleting should handle it
692 * anyway. */
693 if (current == osb->vote_task) {
694 mlog(0, "Skipping delete of %lu because we're currently "
695 "in process_vote\n", inode->i_ino);
696 goto bail;
697 }
698
699 spin_lock(&oi->ip_lock);
700 /* OCFS2 *never* deletes system files. This should technically
701 * never get here as system file inodes should always have a
702 * positive link count. */
703 if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
b0697053
MF
704 mlog(ML_ERROR, "Skipping delete of system file %llu\n",
705 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
706 goto bail_unlock;
707 }
708
709 /* If we have voted "yes" on the wipe of this inode for
710 * another node, it will be marked here so we can safely skip
711 * it. Recovery will cleanup any inodes we might inadvertantly
712 * skip here. */
713 if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
714 mlog(0, "Skipping delete of %lu because another node "
715 "has done this for us.\n", inode->i_ino);
716 goto bail_unlock;
717 }
718
719 ret = 1;
720bail_unlock:
721 spin_unlock(&oi->ip_lock);
722bail:
723 return ret;
724}
725
726/* Query the cluster to determine whether we should wipe an inode from
727 * disk or not.
728 *
729 * Requires the inode to have the cluster lock. */
730static int ocfs2_query_inode_wipe(struct inode *inode,
731 struct buffer_head *di_bh,
732 int *wipe)
733{
734 int status = 0;
735 struct ocfs2_inode_info *oi = OCFS2_I(inode);
736 struct ocfs2_dinode *di;
737
738 *wipe = 0;
739
740 /* While we were waiting for the cluster lock in
741 * ocfs2_delete_inode, another node might have asked to delete
742 * the inode. Recheck our flags to catch this. */
743 if (!ocfs2_inode_is_valid_to_delete(inode)) {
b0697053
MF
744 mlog(0, "Skipping delete of %llu because flags changed\n",
745 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
746 goto bail;
747 }
748
749 /* Now that we have an up to date inode, we can double check
750 * the link count. */
751 if (inode->i_nlink) {
b0697053
MF
752 mlog(0, "Skipping delete of %llu because nlink = %u\n",
753 (unsigned long long)oi->ip_blkno, inode->i_nlink);
ccd979bd
MF
754 goto bail;
755 }
756
757 /* Do some basic inode verification... */
758 di = (struct ocfs2_dinode *) di_bh->b_data;
759 if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
760 /* for lack of a better error? */
761 status = -EEXIST;
762 mlog(ML_ERROR,
b0697053 763 "Inode %llu (on-disk %llu) not orphaned! "
ccd979bd 764 "Disk flags 0x%x, inode flags 0x%x\n",
b0697053
MF
765 (unsigned long long)oi->ip_blkno,
766 (unsigned long long)di->i_blkno, di->i_flags,
767 oi->ip_flags);
ccd979bd
MF
768 goto bail;
769 }
770
771 /* has someone already deleted us?! baaad... */
772 if (di->i_dtime) {
773 status = -EEXIST;
774 mlog_errno(status);
775 goto bail;
776 }
777
778 status = ocfs2_request_delete_vote(inode);
779 /* -EBUSY means that other nodes are still using the
780 * inode. We're done here though, so avoid doing anything on
781 * disk and let them worry about deleting it. */
782 if (status == -EBUSY) {
783 status = 0;
b0697053
MF
784 mlog(0, "Skipping delete of %llu because it is in use on"
785 "other nodes\n", (unsigned long long)oi->ip_blkno);
ccd979bd
MF
786 goto bail;
787 }
788 if (status < 0) {
789 mlog_errno(status);
790 goto bail;
791 }
792
793 spin_lock(&oi->ip_lock);
794 if (oi->ip_orphaned_slot == OCFS2_INVALID_SLOT) {
795 /* Nobody knew which slot this inode was orphaned
796 * into. This may happen during node death and
797 * recovery knows how to clean it up so we can safely
798 * ignore this inode for now on. */
b0697053
MF
799 mlog(0, "Nobody knew where inode %llu was orphaned!\n",
800 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
801 } else {
802 *wipe = 1;
803
b0697053
MF
804 mlog(0, "Inode %llu is ok to wipe from orphan dir %d\n",
805 (unsigned long long)oi->ip_blkno, oi->ip_orphaned_slot);
ccd979bd
MF
806 }
807 spin_unlock(&oi->ip_lock);
808
809bail:
810 return status;
811}
812
813/* Support function for ocfs2_delete_inode. Will help us keep the
814 * inode data in a consistent state for clear_inode. Always truncates
815 * pages, optionally sync's them first. */
816static void ocfs2_cleanup_delete_inode(struct inode *inode,
817 int sync_data)
818{
b0697053
MF
819 mlog(0, "Cleanup inode %llu, sync = %d\n",
820 (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
ccd979bd
MF
821 if (sync_data)
822 write_inode_now(inode, 1);
823 truncate_inode_pages(&inode->i_data, 0);
824}
825
826void ocfs2_delete_inode(struct inode *inode)
827{
828 int wipe, status;
829 sigset_t blocked, oldset;
830 struct buffer_head *di_bh = NULL;
831
832 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
833
834 if (is_bad_inode(inode)) {
835 mlog(0, "Skipping delete of bad inode\n");
836 goto bail;
837 }
838
839 if (!ocfs2_inode_is_valid_to_delete(inode)) {
840 /* It's probably not necessary to truncate_inode_pages
841 * here but we do it for safety anyway (it will most
842 * likely be a no-op anyway) */
843 ocfs2_cleanup_delete_inode(inode, 0);
844 goto bail;
845 }
846
847 /* We want to block signals in delete_inode as the lock and
848 * messaging paths may return us -ERESTARTSYS. Which would
849 * cause us to exit early, resulting in inodes being orphaned
850 * forever. */
851 sigfillset(&blocked);
852 status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
853 if (status < 0) {
854 mlog_errno(status);
855 ocfs2_cleanup_delete_inode(inode, 1);
856 goto bail;
857 }
858
859 /* Lock down the inode. This gives us an up to date view of
860 * it's metadata (for verification), and allows us to
861 * serialize delete_inode votes.
862 *
863 * Even though we might be doing a truncate, we don't take the
864 * allocation lock here as it won't be needed - nobody will
865 * have the file open.
866 */
867 status = ocfs2_meta_lock(inode, NULL, &di_bh, 1);
868 if (status < 0) {
869 if (status != -ENOENT)
870 mlog_errno(status);
871 ocfs2_cleanup_delete_inode(inode, 0);
872 goto bail_unblock;
873 }
874
875 /* Query the cluster. This will be the final decision made
876 * before we go ahead and wipe the inode. */
877 status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
878 if (!wipe || status < 0) {
879 /* Error and inode busy vote both mean we won't be
880 * removing the inode, so they take almost the same
881 * path. */
882 if (status < 0)
883 mlog_errno(status);
884
885 /* Someone in the cluster has voted to not wipe this
886 * inode, or it was never completely orphaned. Write
887 * out the pages and exit now. */
888 ocfs2_cleanup_delete_inode(inode, 1);
889 goto bail_unlock_inode;
890 }
891
892 ocfs2_cleanup_delete_inode(inode, 0);
893
894 status = ocfs2_wipe_inode(inode, di_bh);
895 if (status < 0) {
b4df6ed8
MF
896 if (status != -EDEADLK)
897 mlog_errno(status);
ccd979bd
MF
898 goto bail_unlock_inode;
899 }
900
901 /* Mark the inode as successfully deleted. This is important
902 * for ocfs2_clear_inode as it will check this flag and skip
903 * any checkpointing work */
904 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
905
906bail_unlock_inode:
907 ocfs2_meta_unlock(inode, 1);
908 brelse(di_bh);
909bail_unblock:
910 status = sigprocmask(SIG_SETMASK, &oldset, NULL);
911 if (status < 0)
912 mlog_errno(status);
913bail:
914 clear_inode(inode);
915 mlog_exit_void();
916}
917
918void ocfs2_clear_inode(struct inode *inode)
919{
920 int status;
921 struct ocfs2_inode_info *oi = OCFS2_I(inode);
922
923 mlog_entry_void();
924
925 if (!inode)
926 goto bail;
927
b0697053
MF
928 mlog(0, "Clearing inode: %llu, nlink = %u\n",
929 (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
ccd979bd
MF
930
931 mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
932 "Inode=%lu\n", inode->i_ino);
933
934 /* Do these before all the other work so that we don't bounce
935 * the vote thread while waiting to destroy the locks. */
936 ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
937 ocfs2_mark_lockres_freeing(&oi->ip_meta_lockres);
938 ocfs2_mark_lockres_freeing(&oi->ip_data_lockres);
939
940 /* We very well may get a clear_inode before all an inodes
941 * metadata has hit disk. Of course, we can't drop any cluster
942 * locks until the journal has finished with it. The only
943 * exception here are successfully wiped inodes - their
944 * metadata can now be considered to be part of the system
945 * inodes from which it came. */
946 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
947 ocfs2_checkpoint_inode(inode);
948
949 mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
b0697053
MF
950 "Clear inode of %llu, inode has io markers\n",
951 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
952
953 ocfs2_extent_map_drop(inode, 0);
954 ocfs2_extent_map_init(inode);
955
956 status = ocfs2_drop_inode_locks(inode);
957 if (status < 0)
958 mlog_errno(status);
959
960 ocfs2_lock_res_free(&oi->ip_rw_lockres);
961 ocfs2_lock_res_free(&oi->ip_meta_lockres);
962 ocfs2_lock_res_free(&oi->ip_data_lockres);
963
964 ocfs2_metadata_cache_purge(inode);
965
966 mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
b0697053
MF
967 "Clear inode of %llu, inode has %u cache items\n",
968 (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
ccd979bd
MF
969
970 mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
b0697053
MF
971 "Clear inode of %llu, inode has a bad flag\n",
972 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
973
974 mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
b0697053
MF
975 "Clear inode of %llu, inode is locked\n",
976 (unsigned long long)oi->ip_blkno);
ccd979bd 977
251b6ecc 978 mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
b0697053
MF
979 "Clear inode of %llu, io_mutex is locked\n",
980 (unsigned long long)oi->ip_blkno);
251b6ecc 981 mutex_unlock(&oi->ip_io_mutex);
ccd979bd
MF
982
983 /*
984 * down_trylock() returns 0, down_write_trylock() returns 1
985 * kernel 1, world 0
986 */
987 mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
b0697053
MF
988 "Clear inode of %llu, alloc_sem is locked\n",
989 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
990 up_write(&oi->ip_alloc_sem);
991
992 mlog_bug_on_msg(oi->ip_open_count,
b0697053
MF
993 "Clear inode of %llu has open count %d\n",
994 (unsigned long long)oi->ip_blkno, oi->ip_open_count);
ccd979bd 995 mlog_bug_on_msg(!list_empty(&oi->ip_handle_list),
b0697053
MF
996 "Clear inode of %llu has non empty handle list\n",
997 (unsigned long long)oi->ip_blkno);
ccd979bd 998 mlog_bug_on_msg(oi->ip_handle,
b0697053
MF
999 "Clear inode of %llu has non empty handle pointer\n",
1000 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
1001
1002 /* Clear all other flags. */
1003 oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
1004 oi->ip_created_trans = 0;
1005 oi->ip_last_trans = 0;
1006 oi->ip_dir_start_lookup = 0;
1007 oi->ip_blkno = 0ULL;
1008
1009bail:
1010 mlog_exit_void();
1011}
1012
1013/* Called under inode_lock, with no more references on the
1014 * struct inode, so it's safe here to check the flags field
1015 * and to manipulate i_nlink without any other locks. */
1016void ocfs2_drop_inode(struct inode *inode)
1017{
1018 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1019
1020 mlog_entry_void();
1021
b0697053
MF
1022 mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
1023 (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
ccd979bd
MF
1024
1025 /* Testing ip_orphaned_slot here wouldn't work because we may
1026 * not have gotten a delete_inode vote from any other nodes
1027 * yet. */
1028 if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED) {
1029 mlog(0, "Inode was orphaned on another node, clearing nlink.\n");
1030 inode->i_nlink = 0;
1031 }
1032
1033 generic_drop_inode(inode);
1034
1035 mlog_exit_void();
1036}
1037
1038/*
1039 * TODO: this should probably be merged into ocfs2_get_block
1040 *
1041 * However, you now need to pay attention to the cont_prepare_write()
1042 * stuff in ocfs2_get_block (that is, ocfs2_get_block pretty much
1043 * expects never to extend).
1044 */
1045struct buffer_head *ocfs2_bread(struct inode *inode,
1046 int block, int *err, int reada)
1047{
1048 struct buffer_head *bh = NULL;
1049 int tmperr;
1050 u64 p_blkno;
1051 int readflags = OCFS2_BH_CACHED;
1052
ccd979bd
MF
1053 if (reada)
1054 readflags |= OCFS2_BH_READAHEAD;
ccd979bd
MF
1055
1056 if (((u64)block << inode->i_sb->s_blocksize_bits) >=
1057 i_size_read(inode)) {
1058 BUG_ON(!reada);
1059 return NULL;
1060 }
1061
1062 tmperr = ocfs2_extent_map_get_blocks(inode, block, 1,
1063 &p_blkno, NULL);
1064 if (tmperr < 0) {
1065 mlog_errno(tmperr);
1066 goto fail;
1067 }
1068
1069 tmperr = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, &bh,
1070 readflags, inode);
1071 if (tmperr < 0)
1072 goto fail;
1073
1074 tmperr = 0;
1075
1076 *err = 0;
1077 return bh;
1078
1079fail:
1080 if (bh) {
1081 brelse(bh);
1082 bh = NULL;
1083 }
1084 *err = -EIO;
1085 return NULL;
1086}
1087
1088/*
1089 * This is called from our getattr.
1090 */
1091int ocfs2_inode_revalidate(struct dentry *dentry)
1092{
1093 struct inode *inode = dentry->d_inode;
1094 int status = 0;
1095
b0697053
MF
1096 mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
1097 inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
ccd979bd
MF
1098
1099 if (!inode) {
1100 mlog(0, "eep, no inode!\n");
1101 status = -ENOENT;
1102 goto bail;
1103 }
1104
1105 spin_lock(&OCFS2_I(inode)->ip_lock);
1106 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
1107 spin_unlock(&OCFS2_I(inode)->ip_lock);
1108 mlog(0, "inode deleted!\n");
1109 status = -ENOENT;
1110 goto bail;
1111 }
1112 spin_unlock(&OCFS2_I(inode)->ip_lock);
1113
1114 /* Let ocfs2_meta_lock do the work of updating our struct
1115 * inode for us. */
1116 status = ocfs2_meta_lock(inode, NULL, NULL, 0);
1117 if (status < 0) {
1118 if (status != -ENOENT)
1119 mlog_errno(status);
1120 goto bail;
1121 }
1122 ocfs2_meta_unlock(inode, 0);
1123bail:
1124 mlog_exit(status);
1125
1126 return status;
1127}
1128
1129/*
1130 * Updates a disk inode from a
1131 * struct inode.
1132 * Only takes ip_lock.
1133 */
1134int ocfs2_mark_inode_dirty(struct ocfs2_journal_handle *handle,
1135 struct inode *inode,
1136 struct buffer_head *bh)
1137{
1138 int status;
1139 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
1140
b0697053
MF
1141 mlog_entry("(inode %llu)\n",
1142 (unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd
MF
1143
1144 status = ocfs2_journal_access(handle, inode, bh,
1145 OCFS2_JOURNAL_ACCESS_WRITE);
1146 if (status < 0) {
1147 mlog_errno(status);
1148 goto leave;
1149 }
1150
1151 spin_lock(&OCFS2_I(inode)->ip_lock);
1152 fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
ca4d147e 1153 fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
ccd979bd
MF
1154 spin_unlock(&OCFS2_I(inode)->ip_lock);
1155
1156 fe->i_size = cpu_to_le64(i_size_read(inode));
1157 fe->i_links_count = cpu_to_le16(inode->i_nlink);
1158 fe->i_uid = cpu_to_le32(inode->i_uid);
1159 fe->i_gid = cpu_to_le32(inode->i_gid);
1160 fe->i_mode = cpu_to_le16(inode->i_mode);
1161 fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
1162 fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
1163 fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1164 fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1165 fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
1166 fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1167
1168 status = ocfs2_journal_dirty(handle, bh);
1169 if (status < 0)
1170 mlog_errno(status);
1171
1172 status = 0;
1173leave:
1174
1175 mlog_exit(status);
1176 return status;
1177}
1178
1179/*
1180 *
1181 * Updates a struct inode from a disk inode.
1182 * does no i/o, only takes ip_lock.
1183 */
1184void ocfs2_refresh_inode(struct inode *inode,
1185 struct ocfs2_dinode *fe)
1186{
1187 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1188
1189 spin_lock(&OCFS2_I(inode)->ip_lock);
1190
1191 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
ca4d147e
HP
1192 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
1193 ocfs2_set_inode_flags(inode);
ccd979bd
MF
1194 i_size_write(inode, le64_to_cpu(fe->i_size));
1195 inode->i_nlink = le16_to_cpu(fe->i_links_count);
1196 inode->i_uid = le32_to_cpu(fe->i_uid);
1197 inode->i_gid = le32_to_cpu(fe->i_gid);
1198 inode->i_mode = le16_to_cpu(fe->i_mode);
1199 inode->i_blksize = (u32) osb->s_clustersize;
1200 if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
1201 inode->i_blocks = 0;
1202 else
1203 inode->i_blocks = ocfs2_align_bytes_to_sectors(i_size_read(inode));
1204 inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
1205 inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
1206 inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
1207 inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
1208 inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
1209 inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
1210
1211 spin_unlock(&OCFS2_I(inode)->ip_lock);
1212}