Skip to content

Commit b2c2803

Browse files
dgchinnerdchinner
authored andcommitted
xfs: hide log iovec alignment constraints
Callers currently have to round out the size of buffers to match the aligment constraints of log iovecs and xlog_write(). They should not need to know this detail, so introduce a new function to calculate the iovec length (for use in ->iop_size implementations). Also modify xlog_finish_iovec() to round up the length to the correct alignment so the callers don't need to do this, either. Convert the only user - inode forks - of this alignment rounding to use the new interface. Signed-off-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Allison Henderson <allison.henderson@oracle.com> Reviewed-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Dave Chinner <david@fromorbit.com>
1 parent c230a4a commit b2c2803

4 files changed

Lines changed: 52 additions & 39 deletions

File tree

fs/xfs/libxfs/xfs_inode_fork.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ xfs_init_local_fork(
3636
int64_t size)
3737
{
3838
struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
39-
int mem_size = size, real_size = 0;
39+
int mem_size = size;
4040
bool zero_terminate;
4141

4242
/*
@@ -50,13 +50,7 @@ xfs_init_local_fork(
5050
mem_size++;
5151

5252
if (size) {
53-
/*
54-
* As we round up the allocation here, we need to ensure the
55-
* bytes we don't copy data into are zeroed because the log
56-
* vectors still copy them into the journal.
57-
*/
58-
real_size = roundup(mem_size, 4);
59-
ifp->if_u1.if_data = kmem_zalloc(real_size, KM_NOFS);
53+
ifp->if_u1.if_data = kmem_alloc(mem_size, KM_NOFS);
6054
memcpy(ifp->if_u1.if_data, data, size);
6155
if (zero_terminate)
6256
ifp->if_u1.if_data[size] = '\0';
@@ -502,14 +496,8 @@ xfs_idata_realloc(
502496
return;
503497
}
504498

505-
/*
506-
* For inline data, the underlying buffer must be a multiple of 4 bytes
507-
* in size so that it can be logged and stay on word boundaries.
508-
* We enforce that here, and use __GFP_ZERO to ensure that size
509-
* extensions always zero the unused roundup area.
510-
*/
511-
ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
512-
GFP_NOFS | __GFP_NOFAIL | __GFP_ZERO);
499+
ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, new_size,
500+
GFP_NOFS | __GFP_NOFAIL);
513501
ifp->if_bytes = new_size;
514502
}
515503

fs/xfs/xfs_inode_item.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ xfs_inode_item_data_fork_size(
7171
case XFS_DINODE_FMT_LOCAL:
7272
if ((iip->ili_fields & XFS_ILOG_DDATA) &&
7373
ip->i_df.if_bytes > 0) {
74-
*nbytes += roundup(ip->i_df.if_bytes, 4);
74+
*nbytes += xlog_calc_iovec_len(ip->i_df.if_bytes);
7575
*nvecs += 1;
7676
}
7777
break;
@@ -112,7 +112,7 @@ xfs_inode_item_attr_fork_size(
112112
case XFS_DINODE_FMT_LOCAL:
113113
if ((iip->ili_fields & XFS_ILOG_ADATA) &&
114114
ip->i_afp->if_bytes > 0) {
115-
*nbytes += roundup(ip->i_afp->if_bytes, 4);
115+
*nbytes += xlog_calc_iovec_len(ip->i_afp->if_bytes);
116116
*nvecs += 1;
117117
}
118118
break;
@@ -204,17 +204,12 @@ xfs_inode_item_format_data_fork(
204204
~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
205205
if ((iip->ili_fields & XFS_ILOG_DDATA) &&
206206
ip->i_df.if_bytes > 0) {
207-
/*
208-
* Round i_bytes up to a word boundary.
209-
* The underlying memory is guaranteed
210-
* to be there by xfs_idata_realloc().
211-
*/
212-
data_bytes = roundup(ip->i_df.if_bytes, 4);
213207
ASSERT(ip->i_df.if_u1.if_data != NULL);
214208
ASSERT(ip->i_disk_size > 0);
215209
xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
216-
ip->i_df.if_u1.if_data, data_bytes);
217-
ilf->ilf_dsize = (unsigned)data_bytes;
210+
ip->i_df.if_u1.if_data,
211+
ip->i_df.if_bytes);
212+
ilf->ilf_dsize = (unsigned)ip->i_df.if_bytes;
218213
ilf->ilf_size++;
219214
} else {
220215
iip->ili_fields &= ~XFS_ILOG_DDATA;
@@ -288,17 +283,11 @@ xfs_inode_item_format_attr_fork(
288283

289284
if ((iip->ili_fields & XFS_ILOG_ADATA) &&
290285
ip->i_afp->if_bytes > 0) {
291-
/*
292-
* Round i_bytes up to a word boundary.
293-
* The underlying memory is guaranteed
294-
* to be there by xfs_idata_realloc().
295-
*/
296-
data_bytes = roundup(ip->i_afp->if_bytes, 4);
297286
ASSERT(ip->i_afp->if_u1.if_data != NULL);
298287
xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
299288
ip->i_afp->if_u1.if_data,
300-
data_bytes);
301-
ilf->ilf_asize = (unsigned)data_bytes;
289+
ip->i_afp->if_bytes);
290+
ilf->ilf_asize = (unsigned)ip->i_afp->if_bytes;
302291
ilf->ilf_size++;
303292
} else {
304293
iip->ili_fields &= ~XFS_ILOG_ADATA;

fs/xfs/xfs_inode_item_recover.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ xlog_recover_inode_commit_pass2(
462462
ASSERT(in_f->ilf_size <= 4);
463463
ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
464464
ASSERT(!(fields & XFS_ILOG_DFORK) ||
465-
(len == in_f->ilf_dsize));
465+
(len == xlog_calc_iovec_len(in_f->ilf_dsize)));
466466

467467
switch (fields & XFS_ILOG_DFORK) {
468468
case XFS_ILOG_DDATA:
@@ -497,7 +497,7 @@ xlog_recover_inode_commit_pass2(
497497
}
498498
len = item->ri_buf[attr_index].i_len;
499499
src = item->ri_buf[attr_index].i_addr;
500-
ASSERT(len == in_f->ilf_asize);
500+
ASSERT(len == xlog_calc_iovec_len(in_f->ilf_asize));
501501

502502
switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
503503
case XFS_ILOG_ADATA:

fs/xfs/xfs_log.h

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,59 @@ struct xfs_log_vec {
2121

2222
#define XFS_LOG_VEC_ORDERED (-1)
2323

24+
/*
25+
* Calculate the log iovec length for a given user buffer length. Intended to be
26+
* used by ->iop_size implementations when sizing buffers of arbitrary
27+
* alignments.
28+
*/
29+
static inline int
30+
xlog_calc_iovec_len(int len)
31+
{
32+
return roundup(len, sizeof(uint32_t));
33+
}
34+
2435
void *xlog_prepare_iovec(struct xfs_log_vec *lv, struct xfs_log_iovec **vecp,
2536
uint type);
2637

2738
static inline void
28-
xlog_finish_iovec(struct xfs_log_vec *lv, struct xfs_log_iovec *vec, int len)
39+
xlog_finish_iovec(struct xfs_log_vec *lv, struct xfs_log_iovec *vec,
40+
int data_len)
2941
{
3042
struct xlog_op_header *oph = vec->i_addr;
31-
32-
/* opheader tracks payload length, logvec tracks region length */
43+
int len;
44+
45+
/*
46+
* Always round up the length to the correct alignment so callers don't
47+
* need to know anything about this log vec layout requirement. This
48+
* means we have to zero the area the data to be written does not cover.
49+
* This is complicated by fact the payload region is offset into the
50+
* logvec region by the opheader that tracks the payload.
51+
*/
52+
len = xlog_calc_iovec_len(data_len);
53+
if (len - data_len != 0) {
54+
char *buf = vec->i_addr + sizeof(struct xlog_op_header);
55+
56+
memset(buf + data_len, 0, len - data_len);
57+
}
58+
59+
/*
60+
* The opheader tracks aligned payload length, whilst the logvec tracks
61+
* the overall region length.
62+
*/
3363
oph->oh_len = cpu_to_be32(len);
3464

3565
len += sizeof(struct xlog_op_header);
3666
lv->lv_buf_len += len;
3767
lv->lv_bytes += len;
3868
vec->i_len = len;
69+
70+
/* Catch buffer overruns */
71+
ASSERT((void *)lv->lv_buf + lv->lv_bytes <= (void *)lv + lv->lv_size);
3972
}
4073

74+
/*
75+
* Copy the amount of data requested by the caller into a new log iovec.
76+
*/
4177
static inline void *
4278
xlog_copy_iovec(struct xfs_log_vec *lv, struct xfs_log_iovec **vecp,
4379
uint type, void *data, int len)

0 commit comments

Comments
 (0)