fileio.c revision 56fa48879641603f2a3814fbe130ab2a29dcf411
1/*
2 * fileio.c --- Simple file I/O routines
3 *
4 * Copyright (C) 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12#include "config.h"
13#include <stdio.h>
14#include <string.h>
15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18
19#include "ext2_fs.h"
20#include "ext2fs.h"
21
22struct ext2_file {
23	errcode_t		magic;
24	ext2_filsys 		fs;
25	ext2_ino_t		ino;
26	struct ext2_inode	inode;
27	int 			flags;
28	__u64			pos;
29	blk64_t			blockno;
30	blk64_t			physblock;
31	char 			*buf;
32};
33
34#define BMAP_BUFFER (file->buf + fs->blocksize)
35
36errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
37			    struct ext2_inode *inode,
38			    int flags, ext2_file_t *ret)
39{
40	ext2_file_t 	file;
41	errcode_t	retval;
42
43	/*
44	 * Don't let caller create or open a file for writing if the
45	 * filesystem is read-only.
46	 */
47	if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
48	    !(fs->flags & EXT2_FLAG_RW))
49		return EXT2_ET_RO_FILSYS;
50
51	retval = ext2fs_get_mem(sizeof(struct ext2_file), &file);
52	if (retval)
53		return retval;
54
55	memset(file, 0, sizeof(struct ext2_file));
56	file->magic = EXT2_ET_MAGIC_EXT2_FILE;
57	file->fs = fs;
58	file->ino = ino;
59	file->flags = flags & EXT2_FILE_MASK;
60
61	if (inode) {
62		memcpy(&file->inode, inode, sizeof(struct ext2_inode));
63	} else {
64		retval = ext2fs_read_inode(fs, ino, &file->inode);
65		if (retval)
66			goto fail;
67	}
68
69	retval = ext2fs_get_array(3, fs->blocksize, &file->buf);
70	if (retval)
71		goto fail;
72
73	*ret = file;
74	return 0;
75
76fail:
77	if (file->buf)
78		ext2fs_free_mem(&file->buf);
79	ext2fs_free_mem(&file);
80	return retval;
81}
82
83errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
84			   int flags, ext2_file_t *ret)
85{
86	return ext2fs_file_open2(fs, ino, NULL, flags, ret);
87}
88
89/*
90 * This function returns the filesystem handle of a file from the structure
91 */
92ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
93{
94	if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
95		return 0;
96	return file->fs;
97}
98
99/*
100 * This function returns the pointer to the inode of a file from the structure
101 */
102struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file)
103{
104	if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
105		return NULL;
106	return &file->inode;
107}
108
109/* This function returns the inode number from the structure */
110ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file)
111{
112	if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
113		return 0;
114	return file->ino;
115}
116
117/*
118 * This function flushes the dirty block buffer out to disk if
119 * necessary.
120 */
121errcode_t ext2fs_file_flush(ext2_file_t file)
122{
123	errcode_t	retval;
124	ext2_filsys fs;
125
126	EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
127	fs = file->fs;
128
129	if (!(file->flags & EXT2_FILE_BUF_VALID) ||
130	    !(file->flags & EXT2_FILE_BUF_DIRTY))
131		return 0;
132
133	/*
134	 * OK, the physical block hasn't been allocated yet.
135	 * Allocate it.
136	 */
137	if (!file->physblock) {
138		retval = ext2fs_bmap2(fs, file->ino, &file->inode,
139				     BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
140				     file->blockno, 0, &file->physblock);
141		if (retval)
142			return retval;
143	}
144
145	retval = io_channel_write_blk64(fs->io, file->physblock, 1, file->buf);
146	if (retval)
147		return retval;
148
149	file->flags &= ~EXT2_FILE_BUF_DIRTY;
150
151	return retval;
152}
153
154/*
155 * This function synchronizes the file's block buffer and the current
156 * file position, possibly invalidating block buffer if necessary
157 */
158static errcode_t sync_buffer_position(ext2_file_t file)
159{
160	blk64_t	b;
161	errcode_t	retval;
162
163	b = file->pos / file->fs->blocksize;
164	if (b != file->blockno) {
165		retval = ext2fs_file_flush(file);
166		if (retval)
167			return retval;
168		file->flags &= ~EXT2_FILE_BUF_VALID;
169	}
170	file->blockno = b;
171	return 0;
172}
173
174/*
175 * This function loads the file's block buffer with valid data from
176 * the disk as necessary.
177 *
178 * If dontfill is true, then skip initializing the buffer since we're
179 * going to be replacing its entire contents anyway.  If set, then the
180 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
181 */
182#define DONTFILL 1
183static errcode_t load_buffer(ext2_file_t file, int dontfill)
184{
185	ext2_filsys	fs = file->fs;
186	errcode_t	retval;
187
188	if (!(file->flags & EXT2_FILE_BUF_VALID)) {
189		retval = ext2fs_bmap2(fs, file->ino, &file->inode,
190				     BMAP_BUFFER, 0, file->blockno, 0,
191				     &file->physblock);
192		if (retval)
193			return retval;
194		if (!dontfill) {
195			if (file->physblock) {
196				retval = io_channel_read_blk64(fs->io,
197							       file->physblock,
198							       1, file->buf);
199				if (retval)
200					return retval;
201			} else
202				memset(file->buf, 0, fs->blocksize);
203		}
204		file->flags |= EXT2_FILE_BUF_VALID;
205	}
206	return 0;
207}
208
209
210errcode_t ext2fs_file_close(ext2_file_t file)
211{
212	errcode_t	retval;
213
214	EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
215
216	retval = ext2fs_file_flush(file);
217
218	if (file->buf)
219		ext2fs_free_mem(&file->buf);
220	ext2fs_free_mem(&file);
221
222	return retval;
223}
224
225
226errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
227			   unsigned int wanted, unsigned int *got)
228{
229	ext2_filsys	fs;
230	errcode_t	retval = 0;
231	unsigned int	start, c, count = 0;
232	__u64		left;
233	char		*ptr = (char *) buf;
234
235	EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
236	fs = file->fs;
237
238	while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
239		retval = sync_buffer_position(file);
240		if (retval)
241			goto fail;
242		retval = load_buffer(file, 0);
243		if (retval)
244			goto fail;
245
246		start = file->pos % fs->blocksize;
247		c = fs->blocksize - start;
248		if (c > wanted)
249			c = wanted;
250		left = EXT2_I_SIZE(&file->inode) - file->pos ;
251		if (c > left)
252			c = left;
253
254		memcpy(ptr, file->buf+start, c);
255		file->pos += c;
256		ptr += c;
257		count += c;
258		wanted -= c;
259	}
260
261fail:
262	if (got)
263		*got = count;
264	return retval;
265}
266
267
268errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
269			    unsigned int nbytes, unsigned int *written)
270{
271	ext2_filsys	fs;
272	errcode_t	retval = 0;
273	unsigned int	start, c, count = 0;
274	const char	*ptr = (const char *) buf;
275
276	EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
277	fs = file->fs;
278
279	if (!(file->flags & EXT2_FILE_WRITE))
280		return EXT2_ET_FILE_RO;
281
282	while (nbytes > 0) {
283		retval = sync_buffer_position(file);
284		if (retval)
285			goto fail;
286
287		start = file->pos % fs->blocksize;
288		c = fs->blocksize - start;
289		if (c > nbytes)
290			c = nbytes;
291
292		/*
293		 * We only need to do a read-modify-update cycle if
294		 * we're doing a partial write.
295		 */
296		retval = load_buffer(file, (c == fs->blocksize));
297		if (retval)
298			goto fail;
299
300		file->flags |= EXT2_FILE_BUF_DIRTY;
301		memcpy(file->buf+start, ptr, c);
302		file->pos += c;
303		ptr += c;
304		count += c;
305		nbytes -= c;
306	}
307
308fail:
309	/* Update inode size */
310	if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
311		errcode_t	rc;
312
313		rc = ext2fs_file_set_size2(file, file->pos);
314		if (retval == 0)
315			retval = rc;
316	}
317
318	if (written)
319		*written = count;
320	return retval;
321}
322
323errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
324			    int whence, __u64 *ret_pos)
325{
326	EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
327
328	if (whence == EXT2_SEEK_SET)
329		file->pos = offset;
330	else if (whence == EXT2_SEEK_CUR)
331		file->pos += offset;
332	else if (whence == EXT2_SEEK_END)
333		file->pos = EXT2_I_SIZE(&file->inode) + offset;
334	else
335		return EXT2_ET_INVALID_ARGUMENT;
336
337	if (ret_pos)
338		*ret_pos = file->pos;
339
340	return 0;
341}
342
343errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
344			    int whence, ext2_off_t *ret_pos)
345{
346	__u64		loffset, ret_loffset;
347	errcode_t	retval;
348
349	loffset = offset;
350	retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
351	if (ret_pos)
352		*ret_pos = (ext2_off_t) ret_loffset;
353	return retval;
354}
355
356
357/*
358 * This function returns the size of the file, according to the inode
359 */
360errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
361{
362	if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
363		return EXT2_ET_MAGIC_EXT2_FILE;
364	*ret_size = EXT2_I_SIZE(&file->inode);
365	return 0;
366}
367
368/*
369 * This function returns the size of the file, according to the inode
370 */
371ext2_off_t ext2fs_file_get_size(ext2_file_t file)
372{
373	__u64	size;
374
375	if (ext2fs_file_get_lsize(file, &size))
376		return 0;
377	if ((size >> 32) != 0)
378		return 0;
379	return size;
380}
381
382/*
383 * This function sets the size of the file, truncating it if necessary
384 *
385 */
386errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size)
387{
388	ext2_off64_t	old_size;
389	errcode_t	retval;
390	blk64_t		old_truncate, truncate_block;
391
392	EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
393
394	if (size && ext2fs_file_block_offset_too_big(file->fs, &file->inode,
395					(size - 1) / file->fs->blocksize))
396		return EXT2_ET_FILE_TOO_BIG;
397	truncate_block = ((size + file->fs->blocksize - 1) >>
398			  EXT2_BLOCK_SIZE_BITS(file->fs->super));
399	old_size = EXT2_I_SIZE(&file->inode);
400	old_truncate = ((old_size + file->fs->blocksize - 1) >>
401		      EXT2_BLOCK_SIZE_BITS(file->fs->super));
402
403	/* If we're writing a large file, set the large_file flag */
404	if (LINUX_S_ISREG(file->inode.i_mode) &&
405	    ext2fs_needs_large_file_feature(EXT2_I_SIZE(&file->inode)) &&
406	    (!EXT2_HAS_RO_COMPAT_FEATURE(file->fs->super,
407					 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
408	     file->fs->super->s_rev_level == EXT2_GOOD_OLD_REV)) {
409		file->fs->super->s_feature_ro_compat |=
410				EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
411		ext2fs_update_dynamic_rev(file->fs);
412		ext2fs_mark_super_dirty(file->fs);
413	}
414
415	file->inode.i_size = size & 0xffffffff;
416	file->inode.i_size_high = (size >> 32);
417	if (file->ino) {
418		retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
419		if (retval)
420			return retval;
421	}
422
423	if (truncate_block >= old_truncate)
424		return 0;
425
426	return ext2fs_punch(file->fs, file->ino, &file->inode, 0,
427			    truncate_block, ~0ULL);
428}
429
430errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
431{
432	return ext2fs_file_set_size2(file, size);
433}
434