aboutsummaryrefslogtreecommitdiffstats
path: root/fs/gfs2/jdata.c
blob: e43eaf133f10e0ce9200500de568eab1e31f1afc (plain) (blame)
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
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
 * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU General Public License v.2.
 */

#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>

#include "gfs2.h"
#include "bmap.h"
#include "inode.h"
#include "jdata.h"
#include "meta_io.h"
#include "trans.h"

int gfs2_internal_read(struct gfs2_inode *ip,
                       struct file_ra_state *ra_state,
                       char *buf, loff_t *pos, unsigned size)
{
	return gfs2_jdata_read_mem(ip, buf, *pos, size);
}

int gfs2_jdata_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
			  struct buffer_head **bhp)
{
	struct buffer_head *bh;
	int error = 0;

	if (new) {
		bh = gfs2_meta_new(ip->i_gl, block);
		gfs2_trans_add_bh(ip->i_gl, bh, 1);
		gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
		gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
	} else {
		error = gfs2_meta_read(ip->i_gl, block,
				       DIO_START | DIO_WAIT, &bh);
		if (error)
			return error;
		if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
			brelse(bh);
			return -EIO;
		}
	}

	*bhp = bh;

	return 0;
}

/**
 * gfs2_copy2mem - Trivial copy function for gfs2_jdata_read()
 * @bh: The buffer to copy from, or NULL meaning zero the buffer
 * @buf: The buffer to copy/zero
 * @offset: The offset in the buffer to copy from
 * @size: The amount of data to copy/zero
 *
 * Returns: errno
 */

int gfs2_copy2mem(struct buffer_head *bh, char **buf, unsigned int offset,
		  unsigned int size)
{
	if (bh)
		memcpy(*buf, bh->b_data + offset, size);
	else
		memset(*buf, 0, size);
	*buf += size;
	return 0;
}

/**
 * gfs2_copy2user - Copy bytes to user space for gfs2_jdata_read()
 * @bh: The buffer
 * @buf: The destination of the data
 * @offset: The offset into the buffer
 * @size: The amount of data to copy
 *
 * Returns: errno
 */

int gfs2_copy2user(struct buffer_head *bh, char **buf, unsigned int offset,
		   unsigned int size)
{
	int error;

	if (bh)
		error = copy_to_user(*buf, bh->b_data + offset, size);
	else
		error = clear_user(*buf, size);

	if (error)
		error = -EFAULT;
	else
		*buf += size;

	return error;
}

static int jdata_read_stuffed(struct gfs2_inode *ip, char *buf,
			      unsigned int offset, unsigned int size,
			      read_copy_fn_t copy_fn)
{
	struct buffer_head *dibh;
	int error;

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (!error) {
		error = copy_fn(dibh, &buf,
				offset + sizeof(struct gfs2_dinode), size);
		brelse(dibh);
	}

	return (error) ? error : size;
}

/**
 * gfs2_jdata_read - Read a jdata file
 * @ip: The GFS2 Inode
 * @buf: The buffer to place result into
 * @offset: File offset to begin jdata_readng from
 * @size: Amount of data to transfer
 * @copy_fn: Function to actually perform the copy
 *
 * The @copy_fn only copies a maximum of a single block at once so
 * we are safe calling it with int arguments. It is done so that
 * we don't needlessly put 64bit arguments on the stack and it
 * also makes the code in the @copy_fn nicer too.
 *
 * Returns: The amount of data actually copied or the error
 */

int gfs2_jdata_read(struct gfs2_inode *ip, char __user *buf, uint64_t offset,
		    unsigned int size, read_copy_fn_t copy_fn)
{
	struct gfs2_sbd *sdp = ip->i_sbd;
	uint64_t lblock, dblock;
	uint32_t extlen = 0;
	unsigned int o;
	int copied = 0;
	int error = 0;

	if (offset >= ip->i_di.di_size)
		return 0;

	if ((offset + size) > ip->i_di.di_size)
		size = ip->i_di.di_size - offset;

	if (!size)
		return 0;

	if (gfs2_is_stuffed(ip))
		return jdata_read_stuffed(ip, buf, (unsigned int)offset, size,
					  copy_fn);

	if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
		return -EINVAL;

	lblock = offset;
	o = do_div(lblock, sdp->sd_jbsize) +
		sizeof(struct gfs2_meta_header);

	while (copied < size) {
		unsigned int amount;
		struct buffer_head *bh;
		int new;

		amount = size - copied;
		if (amount > sdp->sd_sb.sb_bsize - o)
			amount = sdp->sd_sb.sb_bsize - o;

		if (!extlen) {
			new = 0;
			error = gfs2_block_map(ip, lblock, &new,
					       &dblock, &extlen);
			if (error)
				goto fail;
		}

		if (extlen > 1)
			gfs2_meta_ra(ip->i_gl, dblock, extlen);

		if (dblock) {
			error = gfs2_jdata_get_buffer(ip, dblock, new, &bh);
			if (error)
				goto fail;
			dblock++;
			extlen--;
		} else
			bh = NULL;

		error = copy_fn(bh, &buf, o, amount);
		brelse(bh);
		if (error)
			goto fail;

		copied += amount;
		lblock++;

		o = sizeof(struct gfs2_meta_header);
	}

	return copied;

 fail:
	return (copied) ? copied : error;
}

/**
 * gfs2_copy_from_mem - Trivial copy function for gfs2_jdata_write()
 * @bh: The buffer to copy to or clear
 * @buf: The buffer to copy from
 * @offset: The offset in the buffer to write to
 * @size: The amount of data to write
 *
 * Returns: errno
 */

int gfs2_copy_from_mem(struct gfs2_inode *ip, struct buffer_head *bh,
		       const char **buf, unsigned int offset, unsigned int size)
{
	gfs2_trans_add_bh(ip->i_gl, bh, 1);
	memcpy(bh->b_data + offset, *buf, size);

	*buf += size;

	return 0;
}

/**
 * gfs2_copy_from_user - Copy bytes from user space for gfs2_jdata_write()
 * @bh: The buffer to copy to or clear
 * @buf: The buffer to copy from
 * @offset: The offset in the buffer to write to
 * @size: The amount of data to write
 *
 * Returns: errno
 */

int gfs2_copy_from_user(struct gfs2_inode *ip, struct buffer_head *bh,
			const char __user **buf, unsigned int offset, unsigned int size)
{
	int error = 0;

	gfs2_trans_add_bh(ip->i_gl, bh, 1);
	if (copy_from_user(bh->b_data + offset, *buf, size))
		error = -EFAULT;
	else
		*buf += size;

	return error;
}

static int jdata_write_stuffed(struct gfs2_inode *ip, char *buf,
			       unsigned int offset, unsigned int size,
			       write_copy_fn_t copy_fn)
{
	struct buffer_head *dibh;
	int error;

	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (error)
		return error;

	error = copy_fn(ip,
			dibh, &buf,
			offset + sizeof(struct gfs2_dinode), size);
	if (!error) {
		if (ip->i_di.di_size < offset + size)
			ip->i_di.di_size = offset + size;
		ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
		gfs2_dinode_out(&ip->i_di, dibh->b_data);
	}

	brelse(dibh);

	return (error) ? error : size;
}

/**
 * gfs2_jdata_write - Write bytes to a file
 * @ip: The GFS2 inode
 * @buf: The buffer containing information to be written
 * @offset: The file offset to start writing at
 * @size: The amount of data to write
 * @copy_fn: Function to do the actual copying
 *
 * Returns: The number of bytes correctly written or error code
 */

int gfs2_jdata_write(struct gfs2_inode *ip, const char __user *buf, uint64_t offset,
		     unsigned int size, write_copy_fn_t copy_fn)
{
	struct gfs2_sbd *sdp = ip->i_sbd;
	struct buffer_head *dibh;
	uint64_t lblock, dblock;
	uint32_t extlen = 0;
	unsigned int o;
	int copied = 0;
	int error = 0;

	if (!size)
		return 0;

	if (gfs2_is_stuffed(ip) &&
	    offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
		return jdata_write_stuffed(ip, buf, (unsigned int)offset, size,
					   copy_fn);

	if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
		return -EINVAL;

	if (gfs2_is_stuffed(ip)) {
		error = gfs2_unstuff_dinode(ip, NULL, NULL);
		if (error)
			return error;
	}

	lblock = offset;
	o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);

	while (copied < size) {
		unsigned int amount;
		struct buffer_head *bh;
		int new;

		amount = size - copied;
		if (amount > sdp->sd_sb.sb_bsize - o)
			amount = sdp->sd_sb.sb_bsize - o;

		if (!extlen) {
			new = 1;
			error = gfs2_block_map(ip, lblock, &new,
					       &dblock, &extlen);
			if (error)
				goto fail;
			error = -EIO;
			if (gfs2_assert_withdraw(sdp, dblock))
				goto fail;
		}

		error = gfs2_jdata_get_buffer(ip, dblock,
				(amount == sdp->sd_jbsize) ? 1 : new,
				&bh);
		if (error)
			goto fail;

		error = copy_fn(ip, bh, &buf, o, amount);
		brelse(bh);
		if (error)
			goto fail;

		copied += amount;
		lblock++;
		dblock++;
		extlen--;

		o = sizeof(struct gfs2_meta_header);
	}

 out:
	error = gfs2_meta_inode_buffer(ip, &dibh);
	if (error)
		return error;

	if (ip->i_di.di_size < offset + copied)
		ip->i_di.di_size = offset + copied;
	ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();

	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
	gfs2_dinode_out(&ip->i_di, dibh->b_data);
	brelse(dibh);

	return copied;

 fail:
	if (copied)
		goto out;
	return error;
}