1 /*
2  * Copyright (c) 2012-2013 Paulo Alcantara <pcacjr@zytor.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write the Free Software Foundation,
15  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17 
18 #include <cache.h>
19 #include <core.h>
20 #include <fs.h>
21 
22 #include "xfs_types.h"
23 #include "xfs_sb.h"
24 #include "xfs_ag.h"
25 #include "misc.h"
26 #include "xfs.h"
27 
28 #include "xfs_dinode.h"
29 
30 xfs_dinode_t *xfs_dinode_get_core(struct fs_info *fs, xfs_ino_t ino)
31 {
32     block_t blk;
33     xfs_dinode_t *core;
34     uint64_t offset;
35 
36     xfs_debug("fs %p ino %lu", fs, ino);
37 
38     blk = ino_to_bytes(fs, ino) >> BLOCK_SHIFT(fs);
39     offset = XFS_INO_TO_OFFSET(XFS_INFO(fs), ino) << XFS_INFO(fs)->inode_shift;
40     if (offset > BLOCK_SIZE(fs)) {
41         xfs_error("Invalid inode offset in block!");
42         xfs_debug("offset: 0x%llx", offset);
43         goto out;
44     }
45 
46     xfs_debug("blk %llu block offset 0x%llx", blk, blk << BLOCK_SHIFT(fs));
47     xfs_debug("inode offset in block (in bytes) is 0x%llx", offset);
48 
49     core = (xfs_dinode_t *)((uint8_t *)get_cache(fs->fs_dev, blk) + offset);
50     if (be16_to_cpu(core->di_magic) !=
51 	be16_to_cpu(*(uint16_t *)XFS_DINODE_MAGIC)) {
52 	xfs_error("Inode core's magic number does not match!");
53 	xfs_debug("magic number 0x%04x", (be16_to_cpu(core->di_magic)));
54 	goto out;
55     }
56 
57     return core;
58 
59 out:
60     return NULL;
61 }
62