2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <asm/semaphore.h>
16 #include <asm/uaccess.h>
25 int gfs2_internal_read(struct gfs2_inode *ip,
26 struct file_ra_state *ra_state,
27 char *buf, loff_t *pos, unsigned size)
29 return gfs2_jdata_read_mem(ip, buf, *pos, size);
32 int gfs2_jdata_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
33 struct buffer_head **bhp)
35 struct buffer_head *bh;
39 bh = gfs2_meta_new(ip->i_gl, block);
40 gfs2_trans_add_bh(ip->i_gl, bh, 1);
41 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
42 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
44 error = gfs2_meta_read(ip->i_gl, block,
45 DIO_START | DIO_WAIT, &bh);
48 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
60 * gfs2_copy2mem - Trivial copy function for gfs2_jdata_read()
61 * @bh: The buffer to copy from, or NULL meaning zero the buffer
62 * @buf: The buffer to copy/zero
63 * @offset: The offset in the buffer to copy from
64 * @size: The amount of data to copy/zero
69 int gfs2_copy2mem(struct buffer_head *bh, char **buf, unsigned int offset,
73 memcpy(*buf, bh->b_data + offset, size);
75 memset(*buf, 0, size);
81 * gfs2_copy2user - Copy bytes to user space for gfs2_jdata_read()
83 * @buf: The destination of the data
84 * @offset: The offset into the buffer
85 * @size: The amount of data to copy
90 int gfs2_copy2user(struct buffer_head *bh, char **buf, unsigned int offset,
96 error = copy_to_user(*buf, bh->b_data + offset, size);
98 error = clear_user(*buf, size);
108 static int jdata_read_stuffed(struct gfs2_inode *ip, char *buf,
109 unsigned int offset, unsigned int size,
110 read_copy_fn_t copy_fn)
112 struct buffer_head *dibh;
115 error = gfs2_meta_inode_buffer(ip, &dibh);
117 error = copy_fn(dibh, &buf,
118 offset + sizeof(struct gfs2_dinode), size);
122 return (error) ? error : size;
126 * gfs2_jdata_read - Read a jdata file
127 * @ip: The GFS2 Inode
128 * @buf: The buffer to place result into
129 * @offset: File offset to begin jdata_readng from
130 * @size: Amount of data to transfer
131 * @copy_fn: Function to actually perform the copy
133 * The @copy_fn only copies a maximum of a single block at once so
134 * we are safe calling it with int arguments. It is done so that
135 * we don't needlessly put 64bit arguments on the stack and it
136 * also makes the code in the @copy_fn nicer too.
138 * Returns: The amount of data actually copied or the error
141 int gfs2_jdata_read(struct gfs2_inode *ip, char __user *buf, uint64_t offset,
142 unsigned int size, read_copy_fn_t copy_fn)
144 struct gfs2_sbd *sdp = ip->i_sbd;
145 uint64_t lblock, dblock;
151 if (offset >= ip->i_di.di_size)
154 if ((offset + size) > ip->i_di.di_size)
155 size = ip->i_di.di_size - offset;
160 if (gfs2_is_stuffed(ip))
161 return jdata_read_stuffed(ip, buf, (unsigned int)offset, size,
164 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
168 o = do_div(lblock, sdp->sd_jbsize) +
169 sizeof(struct gfs2_meta_header);
171 while (copied < size) {
173 struct buffer_head *bh;
176 amount = size - copied;
177 if (amount > sdp->sd_sb.sb_bsize - o)
178 amount = sdp->sd_sb.sb_bsize - o;
182 error = gfs2_block_map(ip, lblock, &new,
189 gfs2_meta_ra(ip->i_gl, dblock, extlen);
192 error = gfs2_jdata_get_buffer(ip, dblock, new, &bh);
200 error = copy_fn(bh, &buf, o, amount);
208 o = sizeof(struct gfs2_meta_header);
214 return (copied) ? copied : error;
218 * gfs2_copy_from_mem - Trivial copy function for gfs2_jdata_write()
219 * @bh: The buffer to copy to or clear
220 * @buf: The buffer to copy from
221 * @offset: The offset in the buffer to write to
222 * @size: The amount of data to write
227 int gfs2_copy_from_mem(struct gfs2_inode *ip, struct buffer_head *bh,
228 const char **buf, unsigned int offset, unsigned int size)
230 gfs2_trans_add_bh(ip->i_gl, bh, 1);
231 memcpy(bh->b_data + offset, *buf, size);
239 * gfs2_copy_from_user - Copy bytes from user space for gfs2_jdata_write()
240 * @bh: The buffer to copy to or clear
241 * @buf: The buffer to copy from
242 * @offset: The offset in the buffer to write to
243 * @size: The amount of data to write
248 int gfs2_copy_from_user(struct gfs2_inode *ip, struct buffer_head *bh,
249 const char __user **buf, unsigned int offset, unsigned int size)
253 gfs2_trans_add_bh(ip->i_gl, bh, 1);
254 if (copy_from_user(bh->b_data + offset, *buf, size))
262 static int jdata_write_stuffed(struct gfs2_inode *ip, char *buf,
263 unsigned int offset, unsigned int size,
264 write_copy_fn_t copy_fn)
266 struct buffer_head *dibh;
269 error = gfs2_meta_inode_buffer(ip, &dibh);
275 offset + sizeof(struct gfs2_dinode), size);
277 if (ip->i_di.di_size < offset + size)
278 ip->i_di.di_size = offset + size;
279 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
280 gfs2_dinode_out(&ip->i_di, dibh->b_data);
285 return (error) ? error : size;
289 * gfs2_jdata_write - Write bytes to a file
290 * @ip: The GFS2 inode
291 * @buf: The buffer containing information to be written
292 * @offset: The file offset to start writing at
293 * @size: The amount of data to write
294 * @copy_fn: Function to do the actual copying
296 * Returns: The number of bytes correctly written or error code
299 int gfs2_jdata_write(struct gfs2_inode *ip, const char __user *buf, uint64_t offset,
300 unsigned int size, write_copy_fn_t copy_fn)
302 struct gfs2_sbd *sdp = ip->i_sbd;
303 struct buffer_head *dibh;
304 uint64_t lblock, dblock;
313 if (gfs2_is_stuffed(ip) &&
314 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
315 return jdata_write_stuffed(ip, buf, (unsigned int)offset, size,
318 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
321 if (gfs2_is_stuffed(ip)) {
322 error = gfs2_unstuff_dinode(ip, NULL, NULL);
328 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
330 while (copied < size) {
332 struct buffer_head *bh;
335 amount = size - copied;
336 if (amount > sdp->sd_sb.sb_bsize - o)
337 amount = sdp->sd_sb.sb_bsize - o;
341 error = gfs2_block_map(ip, lblock, &new,
346 if (gfs2_assert_withdraw(sdp, dblock))
350 error = gfs2_jdata_get_buffer(ip, dblock,
351 (amount == sdp->sd_jbsize) ? 1 : new,
356 error = copy_fn(ip, bh, &buf, o, amount);
366 o = sizeof(struct gfs2_meta_header);
370 error = gfs2_meta_inode_buffer(ip, &dibh);
374 if (ip->i_di.di_size < offset + copied)
375 ip->i_di.di_size = offset + copied;
376 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
378 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
379 gfs2_dinode_out(&ip->i_di, dibh->b_data);