File: inode.c

package info (click to toggle)
ocfs2-tools 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 6,232 kB
  • sloc: ansic: 86,865; sh: 5,781; python: 2,380; makefile: 1,305
file content (496 lines) | stat: -rw-r--r-- 13,334 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * inode.c
 *
 * Inode operations for the OCFS2 userspace library.
 *
 * Copyright (C) 2004 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License, version 2,  as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301 USA.
 *
 * Ideas taken from e2fsprogs/lib/ext2fs/inode.c
 *   Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
 */

#define _XOPEN_SOURCE 600  /* Triggers XOPEN2K in features.h */
#define _LARGEFILE64_SOURCE

#include <string.h>
#include <inttypes.h>

#include "ocfs2/byteorder.h"
#include "ocfs2/ocfs2.h"


errcode_t ocfs2_check_directory(ocfs2_filesys *fs, uint64_t dir)
{
	struct ocfs2_dinode *inode;
	char *buf;
	errcode_t ret;

	if ((dir < OCFS2_SUPER_BLOCK_BLKNO) ||
	    (dir > fs->fs_blocks))
		return OCFS2_ET_BAD_BLKNO;

	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret)
		return ret;

	ret = ocfs2_read_inode(fs, dir, buf);
	if (ret)
		goto out_buf;

	inode = (struct ocfs2_dinode *)buf;
	if (!S_ISDIR(inode->i_mode))
		ret = OCFS2_ET_NO_DIRECTORY;

out_buf:
	ocfs2_free(&buf);

	return ret;
}

static void ocfs2_swap_inode_third(ocfs2_filesys *fs, struct ocfs2_dinode *di)
{

	if (di->i_flags & OCFS2_CHAIN_FL) {
		struct ocfs2_chain_list *cl = &di->id2.i_chain;
		uint16_t i;

		for (i = 0; i < cl->cl_next_free_rec; i++) {
			struct ocfs2_chain_rec *rec = &cl->cl_recs[i];

			if (ocfs2_swap_barrier(fs, di, rec,
					       sizeof(struct ocfs2_chain_rec)))
				break;

			rec->c_free  = bswap_32(rec->c_free);
			rec->c_total = bswap_32(rec->c_total);
			rec->c_blkno = bswap_64(rec->c_blkno);
		}

	} else if (di->i_flags & OCFS2_DEALLOC_FL) {
		struct ocfs2_truncate_log *tl = &di->id2.i_dealloc;
		uint16_t i;

		for(i = 0; i < tl->tl_count; i++) {
			struct ocfs2_truncate_rec *rec =
				&tl->tl_recs[i];

			if (ocfs2_swap_barrier(fs, di, rec,
					     sizeof(struct ocfs2_truncate_rec)))
				break;

			rec->t_start    = bswap_32(rec->t_start);
			rec->t_clusters = bswap_32(rec->t_clusters);
		}
	}
}

static void ocfs2_swap_inode_second(struct ocfs2_dinode *di)
{
	if (S_ISCHR(di->i_mode) || S_ISBLK(di->i_mode))
		di->id1.dev1.i_rdev = bswap_64(di->id1.dev1.i_rdev);
	else if (di->i_flags & OCFS2_BITMAP_FL) {
		di->id1.bitmap1.i_used = bswap_32(di->id1.bitmap1.i_used);
		di->id1.bitmap1.i_total = bswap_32(di->id1.bitmap1.i_total);
	} else if (di->i_flags & OCFS2_JOURNAL_FL) {
		di->id1.journal1.ij_flags = bswap_32(di->id1.journal1.ij_flags);
		di->id1.journal1.ij_recovery_generation =
			bswap_32(di->id1.journal1.ij_recovery_generation);
	}

	/* we need to be careful to swap the union member that is in use.
	 * first the ones that are explicitly marked with flags.. */ 
	if (di->i_flags & OCFS2_SUPER_BLOCK_FL) {
		struct ocfs2_super_block *sb = &di->id2.i_super;

		sb->s_major_rev_level     = bswap_16(sb->s_major_rev_level);
		sb->s_minor_rev_level     = bswap_16(sb->s_minor_rev_level);
		sb->s_mnt_count           = bswap_16(sb->s_mnt_count);
		sb->s_max_mnt_count       = bswap_16(sb->s_max_mnt_count);
		sb->s_state               = bswap_16(sb->s_state);
		sb->s_errors              = bswap_16(sb->s_errors);
		sb->s_checkinterval       = bswap_32(sb->s_checkinterval);
		sb->s_lastcheck           = bswap_64(sb->s_lastcheck);
		sb->s_creator_os          = bswap_32(sb->s_creator_os);
		sb->s_feature_compat      = bswap_32(sb->s_feature_compat);
		sb->s_feature_ro_compat   = bswap_32(sb->s_feature_ro_compat);
		sb->s_feature_incompat    = bswap_32(sb->s_feature_incompat);
		sb->s_root_blkno          = bswap_64(sb->s_root_blkno);
		sb->s_system_dir_blkno    = bswap_64(sb->s_system_dir_blkno);
		sb->s_blocksize_bits      = bswap_32(sb->s_blocksize_bits);
		sb->s_clustersize_bits    = bswap_32(sb->s_clustersize_bits);
		sb->s_max_slots           = bswap_16(sb->s_max_slots);
		sb->s_tunefs_flag         = bswap_16(sb->s_tunefs_flag);
		sb->s_uuid_hash           = bswap_32(sb->s_uuid_hash);
		sb->s_first_cluster_group = bswap_64(sb->s_first_cluster_group);
		sb->s_xattr_inline_size   = bswap_16(sb->s_xattr_inline_size);
		sb->s_dx_seed[0]          = bswap_32(sb->s_dx_seed[0]);
		sb->s_dx_seed[1]          = bswap_32(sb->s_dx_seed[1]);
		sb->s_dx_seed[2]          = bswap_32(sb->s_dx_seed[2]);

	} else if (di->i_flags & OCFS2_LOCAL_ALLOC_FL) {
		struct ocfs2_local_alloc *la = &di->id2.i_lab;

		la->la_bm_off = bswap_32(la->la_bm_off);
		la->la_size   = bswap_16(la->la_size);

	} else if (di->i_flags & OCFS2_CHAIN_FL) {
		struct ocfs2_chain_list *cl = &di->id2.i_chain;

		cl->cl_cpg           = bswap_16(cl->cl_cpg);
		cl->cl_bpc           = bswap_16(cl->cl_bpc);
		cl->cl_count         = bswap_16(cl->cl_count);
		cl->cl_next_free_rec = bswap_16(cl->cl_next_free_rec);

	} else if (di->i_flags & OCFS2_DEALLOC_FL) {
		struct ocfs2_truncate_log *tl = &di->id2.i_dealloc;

		tl->tl_count = bswap_16(tl->tl_count);
		tl->tl_used  = bswap_16(tl->tl_used);
	} else if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) {
		struct ocfs2_inline_data *id = &di->id2.i_data;

		id->id_count = bswap_16(id->id_count);
	} else if (di->i_dyn_features & OCFS2_INDEXED_DIR_FL) {
		di->i_dx_root = bswap_64(di->i_dx_root);
	}
}

static void ocfs2_swap_inode_first(struct ocfs2_dinode *di)
{
	di->i_generation    = bswap_32(di->i_generation);
	di->i_suballoc_slot = bswap_16(di->i_suballoc_slot);
	di->i_suballoc_bit  = bswap_16(di->i_suballoc_bit);
	di->i_xattr_inline_size = bswap_16(di->i_xattr_inline_size);
	di->i_clusters      = bswap_32(di->i_clusters);
	di->i_uid           = bswap_32(di->i_uid);
	di->i_gid           = bswap_32(di->i_gid);
	di->i_size          = bswap_64(di->i_size);
	di->i_mode          = bswap_16(di->i_mode);
	di->i_links_count   = bswap_16(di->i_links_count);
	di->i_flags         = bswap_32(di->i_flags);
	di->i_atime         = bswap_64(di->i_atime);
	di->i_ctime         = bswap_64(di->i_ctime);
	di->i_mtime         = bswap_64(di->i_mtime);
	di->i_dtime         = bswap_64(di->i_dtime);
	di->i_blkno         = bswap_64(di->i_blkno);
	di->i_last_eb_blk   = bswap_64(di->i_last_eb_blk);
	di->i_fs_generation = bswap_32(di->i_fs_generation);
	di->i_atime_nsec    = bswap_32(di->i_atime_nsec);
	di->i_ctime_nsec    = bswap_32(di->i_ctime_nsec);
	di->i_mtime_nsec    = bswap_32(di->i_mtime_nsec);
	di->i_attr          = bswap_32(di->i_attr);
	di->i_orphaned_slot = bswap_16(di->i_orphaned_slot);
	di->i_dyn_features  = bswap_16(di->i_dyn_features);
	di->i_xattr_loc     = bswap_64(di->i_xattr_loc);
	di->i_refcount_loc  = bswap_64(di->i_refcount_loc);
	di->i_suballoc_loc  = bswap_64(di->i_suballoc_loc);
}

static int has_extents(struct ocfs2_dinode *di)
{
	/* inodes flagged with other stuff in id2 */
	if (di->i_flags & (OCFS2_SUPER_BLOCK_FL | OCFS2_LOCAL_ALLOC_FL |
			   OCFS2_CHAIN_FL | OCFS2_DEALLOC_FL))
		return 0;
	/* i_flags doesn't indicate when id2 is a fast symlink */
	if (S_ISLNK(di->i_mode) && di->i_size && di->i_clusters == 0)
		return 0;
	if (di->i_dyn_features & OCFS2_INLINE_DATA_FL)
		return 0;

	return 1;
}

static inline void ocfs2_swap_inline_dir(ocfs2_filesys *fs,
					 struct ocfs2_dinode *di, int to_cpu)
{
	void *de_buf = di->id2.i_data.id_data;
	uint64_t bytes = di->id2.i_data.id_count;
	int max_inline = ocfs2_max_inline_data_with_xattr(fs->fs_blocksize, di);

	/* Just in case i_xattr_inline_size is garbage */
	if (max_inline < 0)
		max_inline = 0;

	if (bytes > max_inline)
	    bytes = max_inline;

	/* if inode is empty do not swap */
	if (di->i_size == 0)
		return;

	if (to_cpu)
		ocfs2_swap_dir_entries_to_cpu(de_buf, bytes);
	else
		ocfs2_swap_dir_entries_from_cpu(de_buf, bytes);
}

void ocfs2_swap_inode_from_cpu(ocfs2_filesys *fs, struct ocfs2_dinode *di)
{
	if (cpu_is_little_endian)
		return;

	if (di->i_dyn_features & OCFS2_INLINE_XATTR_FL) {
		struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
			((void *)di + fs->fs_blocksize -
			 di->i_xattr_inline_size);
		ocfs2_swap_xattrs_from_cpu(fs, di, xh);
	}
	if (has_extents(di))
		ocfs2_swap_extent_list_from_cpu(fs, di, &di->id2.i_list);
	if (di->i_dyn_features & OCFS2_INLINE_DATA_FL && S_ISDIR(di->i_mode))
		ocfs2_swap_inline_dir(fs, di, 0);
	ocfs2_swap_inode_third(fs, di);
	ocfs2_swap_inode_second(di);
	ocfs2_swap_inode_first(di);
}

void ocfs2_swap_inode_to_cpu(ocfs2_filesys *fs, struct ocfs2_dinode *di)
{
	if (cpu_is_little_endian)
		return;

	ocfs2_swap_inode_first(di);
	ocfs2_swap_inode_second(di);
	ocfs2_swap_inode_third(fs, di);
	if (di->i_dyn_features & OCFS2_INLINE_DATA_FL && S_ISDIR(di->i_mode))
		ocfs2_swap_inline_dir(fs, di, 1);
	if (has_extents(di))
		ocfs2_swap_extent_list_to_cpu(fs, di, &di->id2.i_list);
	if (di->i_dyn_features & OCFS2_INLINE_XATTR_FL) {
		struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
			((void *)di + fs->fs_blocksize -
			 di->i_xattr_inline_size);
		ocfs2_swap_xattrs_to_cpu(fs, di, xh);
	}
}

errcode_t ocfs2_read_inode(ocfs2_filesys *fs, uint64_t blkno,
			   char *inode_buf)
{
	errcode_t ret;
	char *blk;
	struct ocfs2_dinode *di;

	if ((blkno < OCFS2_SUPER_BLOCK_BLKNO) ||
	    (blkno > fs->fs_blocks))
		return OCFS2_ET_BAD_BLKNO;

	ret = ocfs2_malloc_block(fs->fs_io, &blk);
	if (ret)
		return ret;

	ret = ocfs2_read_blocks(fs, blkno, 1, blk);
	if (ret)
		goto out;

	di = (struct ocfs2_dinode *)blk;

	ret = OCFS2_ET_BAD_INODE_MAGIC;
	if (memcmp(di->i_signature, OCFS2_INODE_SIGNATURE,
		   strlen(OCFS2_INODE_SIGNATURE)))
		goto out;

	memcpy(inode_buf, blk, fs->fs_blocksize);

	ret = ocfs2_validate_meta_ecc(fs, blk, &di->i_check);
	if (ret)
		goto out;

	di = (struct ocfs2_dinode *) inode_buf;
	ocfs2_swap_inode_to_cpu(fs, di);

	ret = 0;
out:
	ocfs2_free(&blk);

	return ret;
}

errcode_t ocfs2_write_inode(ocfs2_filesys *fs, uint64_t blkno,
			    char *inode_buf)
{
	errcode_t ret;
	char *blk;
	struct ocfs2_dinode *di;

	if (!(fs->fs_flags & OCFS2_FLAG_RW))
		return OCFS2_ET_RO_FILESYS;

	if ((blkno < OCFS2_SUPER_BLOCK_BLKNO) ||
	    (blkno > fs->fs_blocks))
		return OCFS2_ET_BAD_BLKNO;

	ret = ocfs2_malloc_block(fs->fs_io, &blk);
	if (ret)
		return ret;

	memcpy(blk, inode_buf, fs->fs_blocksize);

	di = (struct ocfs2_dinode *)blk;
	ocfs2_swap_inode_from_cpu(fs, di);

	ocfs2_compute_meta_ecc(fs, blk, &di->i_check);
	ret = io_write_block(fs->fs_io, blkno, 1, blk);
	if (ret)
		goto out;

	fs->fs_flags |= OCFS2_FLAG_CHANGED;
	ret = 0;

out:
	ocfs2_free(&blk);

	return ret;
}

/**
 * Same as ocfs2_write_inode, but without ocfs2_compute_meta_ecc function so
 * that the inode meta CRC and ECC value wouldn't be overwritten after corrupted
 * in fswreck.
 */
errcode_t ocfs2_write_inode_without_meta_ecc(ocfs2_filesys *fs, uint64_t blkno,
			    char *inode_buf)
{
	errcode_t ret;
	char *blk;
	struct ocfs2_dinode *di;

	if (!(fs->fs_flags & OCFS2_FLAG_RW))
		return OCFS2_ET_RO_FILESYS;

	if ((blkno < OCFS2_SUPER_BLOCK_BLKNO) ||
	    (blkno > fs->fs_blocks))
		return OCFS2_ET_BAD_BLKNO;

	ret = ocfs2_malloc_block(fs->fs_io, &blk);
	if (ret)
		return ret;

	memcpy(blk, inode_buf, fs->fs_blocksize);

	di = (struct ocfs2_dinode *)blk;
	ocfs2_swap_inode_from_cpu(fs, di);

	ret = io_write_block(fs->fs_io, blkno, 1, blk);
	if (ret)
		goto out;

	fs->fs_flags |= OCFS2_FLAG_CHANGED;
	ret = 0;

out:
	ocfs2_free(&blk);

	return ret;
}

#ifdef DEBUG_EXE
#include <stdlib.h>

static uint64_t read_number(const char *num)
{
	uint64_t val;
	char *ptr;

	val = strtoull(num, &ptr, 0);
	if (!ptr || *ptr)
		return 0;

	return val;
}

static void print_usage(void)
{
	fprintf(stderr,
		"Usage: inode <filename> <inode_num>\n");
}

extern int opterr, optind;
extern char *optarg;

int main(int argc, char *argv[])
{
	errcode_t ret;
	uint64_t blkno;
	char *filename, *buf;
	ocfs2_filesys *fs;
	struct ocfs2_dinode *di;

	blkno = OCFS2_SUPER_BLOCK_BLKNO;

	initialize_ocfs_error_table();

	if (argc < 2) {
		fprintf(stderr, "Missing filename\n");
		print_usage();
		return 1;
	}
	filename = argv[1];

	if (argc > 2) {
		blkno = read_number(argv[2]);
		if (blkno < OCFS2_SUPER_BLOCK_BLKNO) {
			fprintf(stderr, "Invalid blockno: %"PRIu64"\n", blkno);
			print_usage();
			return 1;
		}
	}

	ret = ocfs2_open(filename, OCFS2_FLAG_RO, 0, 0, &fs);
	if (ret) {
		com_err(argv[0], ret,
			"while opening file \"%s\"", filename);
		goto out;
	}

	ret = ocfs2_malloc_block(fs->fs_io, &buf);
	if (ret) {
		com_err(argv[0], ret,
			"while allocating inode buffer");
		goto out_close;
	}


	ret = ocfs2_read_inode(fs, blkno, buf);
	if (ret) {
		com_err(argv[0], ret, "while reading inode %"PRIu64, blkno);
		goto out_free;
	}

	di = (struct ocfs2_dinode *)buf;

	fprintf(stdout, "OCFS2 inode %"PRIu64" on \"%s\"\n", blkno,
		filename);


out_free:
	ocfs2_free(&buf);

out_close:
	ret = ocfs2_close(fs);
	if (ret) {
		com_err(argv[0], ret,
			"while closing file \"%s\"", filename);
	}

out:
	return 0;
}
#endif  /* DEBUG_EXE */