Skip to content

Commit 003ff18

Browse files
committed
nilfs2: allow future expansion of metadata read out via get info ioctl
Nilfs has some ioctl commands to read out metadata from meta data files: - NILFS_IOCTL_GET_CPINFO for checkpoint file, - NILFS_IOCTL_GET_SUINFO for segment usage file, and - NILFS_IOCTL_GET_VINFO for Disk Address Transalation (DAT) file, respectively. Every routine on these metadata files is implemented so that it allows future expansion of on-disk format. But, the above ioctl commands do not support expansion even though nilfs_argv structure can handle arbitrary size for data exchanged via ioctl. This allows future expansion of the following structures which give basic format of the "get information" ioctls: - struct nilfs_cpinfo - struct nilfs_suinfo - struct nilfs_vinfo So, this introduces forward compatility of such ioctl commands. In this patch, a sanity check in nilfs_ioctl_get_info() function is changed to accept larger data structure [1], and metadata read routines are rewritten so that they become compatible for larger structures; the routines will just ignore the remaining fields which the current version of nilfs doesn't know. [1] The ioctl function already has another upper limit (PAGE_SIZE against a structure, which appears in nilfs_ioctl_wrap_copy function), and this will not cause security problem. Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
1 parent 258ef67 commit 003ff18

7 files changed

Lines changed: 54 additions & 39 deletions

File tree

‎fs/nilfs2/cpfile.c‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,10 @@ static void nilfs_cpfile_checkpoint_to_cpinfo(struct inode *cpfile,
384384
}
385385

386386
static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
387-
struct nilfs_cpinfo *ci, size_t nci)
387+
void *buf, unsigned cisz, size_t nci)
388388
{
389389
struct nilfs_checkpoint *cp;
390+
struct nilfs_cpinfo *ci = buf;
390391
struct buffer_head *bh;
391392
size_t cpsz = NILFS_MDT(cpfile)->mi_entry_size;
392393
__u64 cur_cno = nilfs_mdt_cno(cpfile), cno = *cnop;
@@ -410,29 +411,35 @@ static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
410411
kaddr = kmap_atomic(bh->b_page, KM_USER0);
411412
cp = nilfs_cpfile_block_get_checkpoint(cpfile, cno, bh, kaddr);
412413
for (i = 0; i < ncps && n < nci; i++, cp = (void *)cp + cpsz) {
413-
if (!nilfs_checkpoint_invalid(cp))
414-
nilfs_cpfile_checkpoint_to_cpinfo(
415-
cpfile, cp, &ci[n++]);
414+
if (!nilfs_checkpoint_invalid(cp)) {
415+
nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp,
416+
ci);
417+
ci = (void *)ci + cisz;
418+
n++;
419+
}
416420
}
417421
kunmap_atomic(kaddr, KM_USER0);
418422
brelse(bh);
419423
}
420424

421425
ret = n;
422-
if (n > 0)
423-
*cnop = ci[n - 1].ci_cno + 1;
426+
if (n > 0) {
427+
ci = (void *)ci - cisz;
428+
*cnop = ci->ci_cno + 1;
429+
}
424430

425431
out:
426432
up_read(&NILFS_MDT(cpfile)->mi_sem);
427433
return ret;
428434
}
429435

430436
static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
431-
struct nilfs_cpinfo *ci, size_t nci)
437+
void *buf, unsigned cisz, size_t nci)
432438
{
433439
struct buffer_head *bh;
434440
struct nilfs_cpfile_header *header;
435441
struct nilfs_checkpoint *cp;
442+
struct nilfs_cpinfo *ci = buf;
436443
__u64 curr = *cnop, next;
437444
unsigned long curr_blkoff, next_blkoff;
438445
void *kaddr;
@@ -472,7 +479,9 @@ static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
472479
if (unlikely(nilfs_checkpoint_invalid(cp) ||
473480
!nilfs_checkpoint_snapshot(cp)))
474481
break;
475-
nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, &ci[n++]);
482+
nilfs_cpfile_checkpoint_to_cpinfo(cpfile, cp, ci);
483+
ci = (void *)ci + cisz;
484+
n++;
476485
next = le64_to_cpu(cp->cp_snapshot_list.ssl_next);
477486
if (next == 0)
478487
break; /* reach end of the snapshot list */
@@ -511,13 +520,13 @@ static ssize_t nilfs_cpfile_do_get_ssinfo(struct inode *cpfile, __u64 *cnop,
511520
*/
512521

513522
ssize_t nilfs_cpfile_get_cpinfo(struct inode *cpfile, __u64 *cnop, int mode,
514-
struct nilfs_cpinfo *ci, size_t nci)
523+
void *buf, unsigned cisz, size_t nci)
515524
{
516525
switch (mode) {
517526
case NILFS_CHECKPOINT:
518-
return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, ci, nci);
527+
return nilfs_cpfile_do_get_cpinfo(cpfile, cnop, buf, cisz, nci);
519528
case NILFS_SNAPSHOT:
520-
return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, ci, nci);
529+
return nilfs_cpfile_do_get_ssinfo(cpfile, cnop, buf, cisz, nci);
521530
default:
522531
return -EINVAL;
523532
}
@@ -535,7 +544,7 @@ int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
535544
ssize_t nci;
536545
int ret;
537546

538-
nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, 1);
547+
nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, sizeof(ci), 1);
539548
if (nci < 0)
540549
return nci;
541550
else if (nci == 0 || ci.ci_cno != cno)

‎fs/nilfs2/cpfile.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int nilfs_cpfile_delete_checkpoint(struct inode *, __u64);
3939
int nilfs_cpfile_change_cpmode(struct inode *, __u64, int);
4040
int nilfs_cpfile_is_snapshot(struct inode *, __u64);
4141
int nilfs_cpfile_get_stat(struct inode *, struct nilfs_cpstat *);
42-
ssize_t nilfs_cpfile_get_cpinfo(struct inode *, __u64 *, int,
43-
struct nilfs_cpinfo *, size_t);
42+
ssize_t nilfs_cpfile_get_cpinfo(struct inode *, __u64 *, int, void *, unsigned,
43+
size_t);
4444

4545
#endif /* _NILFS_CPFILE_H */

‎fs/nilfs2/dat.c‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,36 +376,37 @@ int nilfs_dat_translate(struct inode *dat, __u64 vblocknr, sector_t *blocknrp)
376376
return ret;
377377
}
378378

379-
ssize_t nilfs_dat_get_vinfo(struct inode *dat, struct nilfs_vinfo *vinfo,
379+
ssize_t nilfs_dat_get_vinfo(struct inode *dat, void *buf, unsigned visz,
380380
size_t nvi)
381381
{
382382
struct buffer_head *entry_bh;
383383
struct nilfs_dat_entry *entry;
384+
struct nilfs_vinfo *vinfo = buf;
384385
__u64 first, last;
385386
void *kaddr;
386387
unsigned long entries_per_block = NILFS_MDT(dat)->mi_entries_per_block;
387388
int i, j, n, ret;
388389

389390
for (i = 0; i < nvi; i += n) {
390-
ret = nilfs_palloc_get_entry_block(dat, vinfo[i].vi_vblocknr,
391+
ret = nilfs_palloc_get_entry_block(dat, vinfo->vi_vblocknr,
391392
0, &entry_bh);
392393
if (ret < 0)
393394
return ret;
394395
kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
395396
/* last virtual block number in this block */
396-
first = vinfo[i].vi_vblocknr;
397+
first = vinfo->vi_vblocknr;
397398
do_div(first, entries_per_block);
398399
first *= entries_per_block;
399400
last = first + entries_per_block - 1;
400401
for (j = i, n = 0;
401-
j < nvi && vinfo[j].vi_vblocknr >= first &&
402-
vinfo[j].vi_vblocknr <= last;
403-
j++, n++) {
402+
j < nvi && vinfo->vi_vblocknr >= first &&
403+
vinfo->vi_vblocknr <= last;
404+
j++, n++, vinfo = (void *)vinfo + visz) {
404405
entry = nilfs_palloc_block_get_entry(
405-
dat, vinfo[j].vi_vblocknr, entry_bh, kaddr);
406-
vinfo[j].vi_start = le64_to_cpu(entry->de_start);
407-
vinfo[j].vi_end = le64_to_cpu(entry->de_end);
408-
vinfo[j].vi_blocknr = le64_to_cpu(entry->de_blocknr);
406+
dat, vinfo->vi_vblocknr, entry_bh, kaddr);
407+
vinfo->vi_start = le64_to_cpu(entry->de_start);
408+
vinfo->vi_end = le64_to_cpu(entry->de_end);
409+
vinfo->vi_blocknr = le64_to_cpu(entry->de_blocknr);
409410
}
410411
kunmap_atomic(kaddr, KM_USER0);
411412
brelse(entry_bh);

‎fs/nilfs2/dat.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ void nilfs_dat_abort_end(struct inode *, struct nilfs_palloc_req *);
4747
int nilfs_dat_mark_dirty(struct inode *, __u64);
4848
int nilfs_dat_freev(struct inode *, __u64 *, size_t);
4949
int nilfs_dat_move(struct inode *, __u64, sector_t);
50-
ssize_t nilfs_dat_get_vinfo(struct inode *, struct nilfs_vinfo *, size_t);
50+
ssize_t nilfs_dat_get_vinfo(struct inode *, void *, unsigned, size_t);
5151

5252
#endif /* _NILFS_DAT_H */

‎fs/nilfs2/ioctl.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
152152

153153
down_read(&nilfs->ns_segctor_sem);
154154
ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
155-
nmembs);
155+
size, nmembs);
156156
up_read(&nilfs->ns_segctor_sem);
157157
return ret;
158158
}
@@ -182,7 +182,8 @@ nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
182182
int ret;
183183

184184
down_read(&nilfs->ns_segctor_sem);
185-
ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, nmembs);
185+
ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
186+
nmembs);
186187
up_read(&nilfs->ns_segctor_sem);
187188
return ret;
188189
}
@@ -212,7 +213,7 @@ nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
212213
int ret;
213214

214215
down_read(&nilfs->ns_segctor_sem);
215-
ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, nmembs);
216+
ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, size, nmembs);
216217
up_read(&nilfs->ns_segctor_sem);
217218
return ret;
218219
}
@@ -589,7 +590,7 @@ static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
589590
if (copy_from_user(&argv, argp, sizeof(argv)))
590591
return -EFAULT;
591592

592-
if (argv.v_size != membsz)
593+
if (argv.v_size < membsz)
593594
return -EINVAL;
594595

595596
ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);

‎fs/nilfs2/sufile.c‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,8 @@ void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
587587
* nilfs_sufile_get_suinfo -
588588
* @sufile: inode of segment usage file
589589
* @segnum: segment number to start looking
590-
* @si: array of suinfo
590+
* @buf: array of suinfo
591+
* @sisz: byte size of suinfo
591592
* @nsi: size of suinfo array
592593
*
593594
* Description:
@@ -599,11 +600,12 @@ void nilfs_sufile_do_set_error(struct inode *sufile, __u64 segnum,
599600
*
600601
* %-ENOMEM - Insufficient amount of memory available.
601602
*/
602-
ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum,
603-
struct nilfs_suinfo *si, size_t nsi)
603+
ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum, void *buf,
604+
unsigned sisz, size_t nsi)
604605
{
605606
struct buffer_head *su_bh;
606607
struct nilfs_segment_usage *su;
608+
struct nilfs_suinfo *si = buf;
607609
size_t susz = NILFS_MDT(sufile)->mi_entry_size;
608610
struct the_nilfs *nilfs = NILFS_MDT(sufile)->mi_nilfs;
609611
void *kaddr;
@@ -628,20 +630,22 @@ ssize_t nilfs_sufile_get_suinfo(struct inode *sufile, __u64 segnum,
628630
if (ret != -ENOENT)
629631
goto out;
630632
/* hole */
631-
memset(&si[i], 0, sizeof(struct nilfs_suinfo) * n);
633+
memset(si, 0, sisz * n);
634+
si = (void *)si + sisz * n;
632635
continue;
633636
}
634637

635638
kaddr = kmap_atomic(su_bh->b_page, KM_USER0);
636639
su = nilfs_sufile_block_get_segment_usage(
637640
sufile, segnum, su_bh, kaddr);
638-
for (j = 0; j < n; j++, su = (void *)su + susz) {
639-
si[i + j].sui_lastmod = le64_to_cpu(su->su_lastmod);
640-
si[i + j].sui_nblocks = le32_to_cpu(su->su_nblocks);
641-
si[i + j].sui_flags = le32_to_cpu(su->su_flags) &
641+
for (j = 0; j < n;
642+
j++, su = (void *)su + susz, si = (void *)si + sisz) {
643+
si->sui_lastmod = le64_to_cpu(su->su_lastmod);
644+
si->sui_nblocks = le32_to_cpu(su->su_nblocks);
645+
si->sui_flags = le32_to_cpu(su->su_flags) &
642646
~(1UL << NILFS_SEGMENT_USAGE_ACTIVE);
643647
if (nilfs_segment_is_active(nilfs, segnum + j))
644-
si[i + j].sui_flags |=
648+
si->sui_flags |=
645649
(1UL << NILFS_SEGMENT_USAGE_ACTIVE);
646650
}
647651
kunmap_atomic(kaddr, KM_USER0);

‎fs/nilfs2/sufile.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void nilfs_sufile_put_segment_usage(struct inode *, __u64,
4343
struct buffer_head *);
4444
int nilfs_sufile_get_stat(struct inode *, struct nilfs_sustat *);
4545
int nilfs_sufile_get_ncleansegs(struct inode *, unsigned long *);
46-
ssize_t nilfs_sufile_get_suinfo(struct inode *, __u64, struct nilfs_suinfo *,
46+
ssize_t nilfs_sufile_get_suinfo(struct inode *, __u64, void *, unsigned,
4747
size_t);
4848

4949
int nilfs_sufile_updatev(struct inode *, __u64 *, size_t, int, size_t *,

0 commit comments

Comments
 (0)