OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [fs/] [ext2/] [file.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 *  linux/fs/ext2/file.c
3
 *
4
 * Copyright (C) 1992, 1993, 1994, 1995
5
 * Remy Card (card@masi.ibp.fr)
6
 * Laboratoire MASI - Institut Blaise Pascal
7
 * Universite Pierre et Marie Curie (Paris VI)
8
 *
9
 *  from
10
 *
11
 *  linux/fs/minix/file.c
12
 *
13
 *  Copyright (C) 1991, 1992  Linus Torvalds
14
 *
15
 *  ext2 fs regular file handling primitives
16
 */
17
 
18
#include <asm/segment.h>
19
#include <asm/system.h>
20
 
21
#include <linux/errno.h>
22
#include <linux/fs.h>
23
#include <linux/ext2_fs.h>
24
#include <linux/fcntl.h>
25
#include <linux/sched.h>
26
#include <linux/stat.h>
27
#include <linux/locks.h>
28
#include <linux/mm.h>
29
#include <linux/pagemap.h>
30
 
31
#define NBUF    32
32
 
33
#define MIN(a,b) (((a)<(b))?(a):(b))
34
#define MAX(a,b) (((a)>(b))?(a):(b))
35
 
36
static int ext2_file_write (struct inode *, struct file *, const char *, int);
37
static void ext2_release_file (struct inode *, struct file *);
38
 
39
/*
40
 * We have mostly NULL's here: the current defaults are ok for
41
 * the ext2 filesystem.
42
 */
43
static struct file_operations ext2_file_operations = {
44
        NULL,                   /* lseek - default */
45
        generic_file_read,      /* read */
46
        ext2_file_write,        /* write */
47
        NULL,                   /* readdir - bad */
48
        NULL,                   /* select - default */
49
        ext2_ioctl,             /* ioctl */
50
        generic_file_mmap,      /* mmap */
51
        NULL,                   /* no special open is needed */
52
        ext2_release_file,      /* release */
53
        ext2_sync_file,         /* fsync */
54
        NULL,                   /* fasync */
55
        NULL,                   /* check_media_change */
56
        NULL                    /* revalidate */
57
};
58
 
59
struct inode_operations ext2_file_inode_operations = {
60
        &ext2_file_operations,/* default file operations */
61
        NULL,                   /* create */
62
        NULL,                   /* lookup */
63
        NULL,                   /* link */
64
        NULL,                   /* unlink */
65
        NULL,                   /* symlink */
66
        NULL,                   /* mkdir */
67
        NULL,                   /* rmdir */
68
        NULL,                   /* mknod */
69
        NULL,                   /* rename */
70
        NULL,                   /* readlink */
71
        NULL,                   /* follow_link */
72
        generic_readpage,       /* readpage */
73
        NULL,                   /* writepage */
74
        ext2_bmap,              /* bmap */
75
        ext2_truncate,          /* truncate */
76
        ext2_permission,        /* permission */
77
        NULL                    /* smap */
78
};
79
 
80
static int ext2_file_write (struct inode * inode, struct file * filp,
81
                            const char * buf, int count)
82
{
83
        const loff_t two_gb = 2147483647;
84
        loff_t pos;
85
        off_t pos2;
86
        long block;
87
        int offset;
88
        int written, c;
89
        struct buffer_head * bh, *bufferlist[NBUF];
90
        struct super_block * sb;
91
        int err;
92
        int i,buffercount,write_error;
93
 
94
        write_error = buffercount = 0;
95
        if (!inode) {
96
                printk("ext2_file_write: inode = NULL\n");
97
                return -EINVAL;
98
        }
99
        sb = inode->i_sb;
100
        if (sb->s_flags & MS_RDONLY)
101
                /*
102
                 * This fs has been automatically remounted ro because of errors
103
                 */
104
                return -ENOSPC;
105
 
106
        if (!S_ISREG(inode->i_mode)) {
107
                ext2_warning (sb, "ext2_file_write", "mode = %07o",
108
                              inode->i_mode);
109
                return -EINVAL;
110
        }
111
        if (filp->f_flags & O_APPEND)
112
                pos = inode->i_size;
113
        else
114
                pos = filp->f_pos;
115
        pos2 = (off_t) pos;
116
        /*
117
         * If a file has been opened in synchronous mode, we have to ensure
118
         * that meta-data will also be written synchronously.  Thus, we
119
         * set the i_osync field.  This field is tested by the allocation
120
         * routines.
121
         */
122
        if (filp->f_flags & O_SYNC)
123
                inode->u.ext2_i.i_osync++;
124
        block = pos2 >> EXT2_BLOCK_SIZE_BITS(sb);
125
        offset = pos2 & (sb->s_blocksize - 1);
126
        c = sb->s_blocksize - offset;
127
        written = 0;
128
        while (count > 0) {
129
                if (pos > two_gb) {
130
                        if (!written)
131
                                written = -EFBIG;
132
                        break;
133
                }
134
                bh = ext2_getblk (inode, block, 1, &err);
135
                if (!bh) {
136
                        if (!written)
137
                                written = err;
138
                        break;
139
                }
140
                count -= c;
141
                if (count < 0)
142
                        c += count;
143
                if (c != sb->s_blocksize && !buffer_uptodate(bh)) {
144
                        ll_rw_block (READ, 1, &bh);
145
                        wait_on_buffer (bh);
146
                        if (!buffer_uptodate(bh)) {
147
                                brelse (bh);
148
                                if (!written)
149
                                        written = -EIO;
150
                                break;
151
                        }
152
                }
153
                memcpy_fromfs (bh->b_data + offset, buf, c);
154
                update_vm_cache(inode, pos, bh->b_data + offset, c);
155
                pos2 += c;
156
                pos += c;
157
                written += c;
158
                buf += c;
159
                mark_buffer_uptodate(bh, 1);
160
                mark_buffer_dirty(bh, 0);
161
                if (filp->f_flags & O_SYNC)
162
                        bufferlist[buffercount++] = bh;
163
                else
164
                        brelse(bh);
165
                if (buffercount == NBUF){
166
                        ll_rw_block(WRITE, buffercount, bufferlist);
167
                        for(i=0; i<buffercount; i++){
168
                                wait_on_buffer(bufferlist[i]);
169
                                if (!buffer_uptodate(bufferlist[i]))
170
                                        write_error=1;
171
                                brelse(bufferlist[i]);
172
                        }
173
                        buffercount=0;
174
                }
175
                if(write_error)
176
                        break;
177
                block++;
178
                offset = 0;
179
                c = sb->s_blocksize;
180
        }
181
        if ( buffercount ){
182
                ll_rw_block(WRITE, buffercount, bufferlist);
183
                for(i=0; i<buffercount; i++){
184
                        wait_on_buffer(bufferlist[i]);
185
                        if (!buffer_uptodate(bufferlist[i]))
186
                                write_error=1;
187
                        brelse(bufferlist[i]);
188
                }
189
        }
190
        if (pos > inode->i_size)
191
                inode->i_size = pos;
192
        if (filp->f_flags & O_SYNC)
193
                inode->u.ext2_i.i_osync--;
194
        inode->i_ctime = inode->i_mtime = CURRENT_TIME;
195
        filp->f_pos = pos;
196
        inode->i_dirt = 1;
197
        return written;
198
}
199
 
200
/*
201
 * Called when a inode is released. Note that this is different
202
 * from ext2_open: open gets called at every open, but release
203
 * gets called only when /all/ the files are closed.
204
 */
205
static void ext2_release_file (struct inode * inode, struct file * filp)
206
{
207
        if (filp->f_mode & 2)
208
                ext2_discard_prealloc (inode);
209
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.