Skip to content

Commit

Permalink
meta: adjust some error codes (#4324)
Browse files Browse the repository at this point in the history
  • Loading branch information
SandyXSD authored Jan 9, 2024
1 parent 94bdcc6 commit 5d644fe
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 12 deletions.
28 changes: 27 additions & 1 deletion pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,9 @@ func (m *baseMeta) nextInode() (Ino, error) {
}

func (m *baseMeta) Mknod(ctx Context, parent Ino, name string, _type uint8, mode, cumask uint16, rdev uint32, path string, inode *Ino, attr *Attr) syscall.Errno {
if _type < TypeFile || _type > TypeSocket {
return syscall.EINVAL
}
if isTrash(parent) {
return syscall.EPERM
}
Expand Down Expand Up @@ -1383,6 +1386,14 @@ func (m *baseMeta) Mkdir(ctx Context, parent Ino, name string, mode uint16, cuma
}

func (m *baseMeta) Symlink(ctx Context, parent Ino, name string, path string, inode *Ino, attr *Attr) syscall.Errno {
if len(path) == 0 || len(path) > MaxSymlink {
return syscall.EINVAL
}
for _, c := range path {
if c == 0 {
return syscall.EINVAL
}
}
// mode of symlink is ignored in POSIX
return m.Mknod(ctx, parent, name, TypeSymlink, 0777, 0, 0, path, inode, attr)
}
Expand All @@ -1409,6 +1420,9 @@ func (m *baseMeta) Link(ctx Context, inode, parent Ino, name string, attr *Attr)
if st := m.GetAttr(ctx, inode, attr); st != 0 {
return st
}
if attr.Typ == TypeDirectory {
return syscall.EPERM
}
if m.checkDirQuota(ctx, parent, align4K(attr.Length), 1) {
return syscall.EDQUOT
}
Expand Down Expand Up @@ -1444,7 +1458,14 @@ func (m *baseMeta) ReadLink(ctx Context, inode Ino, path *[]byte) syscall.Errno
return errno(err)
}
if len(target) == 0 {
return syscall.ENOENT
var attr Attr
if st := m.GetAttr(ctx, inode, &attr); st != 0 {
return st
}
if attr.Typ != TypeSymlink {
return syscall.EINVAL
}
return syscall.EIO
}
*path = target
if noatime {
Expand Down Expand Up @@ -1778,6 +1799,11 @@ func (m *baseMeta) SetXattr(ctx Context, inode Ino, name string, value []byte, f
if name == "" {
return syscall.EINVAL
}
switch flags {
case 0, XattrCreate, XattrReplace:
default:
return syscall.EINVAL
}

defer m.timeit("SetXattr", time.Now())
return m.en.doSetXattr(ctx, m.checkRoot(inode), name, value, flags)
Expand Down
9 changes: 3 additions & 6 deletions pkg/meta/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func testMetaClient(t *testing.T, m Meta) {
if !bytes.Equal(target1, target2) || !bytes.Equal(target1, []byte("/f")) {
t.Fatalf("readlink got %s %s, expected %s", target1, target2, "/f")
}
if st := m.ReadLink(ctx, parent, &target1); st != syscall.ENOENT {
if st := m.ReadLink(ctx, parent, &target1); st != syscall.EINVAL {
t.Fatalf("readlink d: %s", st)
}
if st := m.Lookup(ctx, 1, "f", &inode, attr, true); st != 0 {
Expand Down Expand Up @@ -542,7 +542,7 @@ func testMetaClient(t *testing.T, m Meta) {
if st := m.SetXattr(ctx, inode, "a", []byte("v4"), XattrReplace); st != 0 {
t.Fatalf("setxattr: %s", st)
}
if st := m.SetXattr(ctx, inode, "a", []byte("v5"), 5); st != 0 { // unknown flag is ignored
if st := m.SetXattr(ctx, inode, "a", []byte("v5"), 5); st != syscall.EINVAL {
t.Fatalf("setxattr: %s", st)
}

Expand Down Expand Up @@ -693,12 +693,9 @@ func testMetaClient(t *testing.T, m Meta) {
t.Fatalf("unlink f3: %s", st)
}
time.Sleep(time.Millisecond * 100) // wait for delete
if st := m.Read(ctx, inode, 0, &slices); st != 0 {
if st := m.Read(ctx, inode, 0, &slices); st != syscall.ENOENT {
t.Fatalf("read chunk: %s", st)
}
if len(slices) != 0 {
t.Fatalf("slices: %v", slices)
}
if st := m.Rmdir(ctx, 1, "d"); st != 0 {
t.Fatalf("rmdir d: %s", st)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/meta/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const (
)

const MaxName = 255
const MaxSymlink = 4096
const RootInode Ino = 1
const TrashInode Ino = 0x7FFFFFFF10000000 // larger than vfs.minInternalNode
var TrashName = ".trash"
Expand Down
28 changes: 26 additions & 2 deletions pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,9 @@ func (m *redisMeta) doSetAttr(ctx Context, inode Ino, set uint16, sugidclearmode
func (m *redisMeta) doReadlink(ctx Context, inode Ino, noatime bool) (atime int64, target []byte, err error) {
if noatime {
target, err = m.rdb.Get(ctx, m.symKey(inode)).Bytes()
if err == redis.Nil {
err = nil
}
return
}

Expand All @@ -1171,10 +1174,16 @@ func (m *redisMeta) doReadlink(ctx Context, inode Ino, noatime bool) (atime int6
if e != nil {
return e
}
if rs[0] == nil || rs[1] == nil {
return redis.Nil
if rs[0] == nil {
return syscall.ENOENT
}
m.parseAttr([]byte(rs[0].(string)), attr)
if attr.Typ != TypeSymlink {
return syscall.EINVAL
}
if rs[1] == nil {
return syscall.EIO
}
target = []byte(rs[1].(string))
if !m.atimeNeedsUpdate(attr, now) {
return nil
Expand Down Expand Up @@ -1683,6 +1692,10 @@ func (m *redisMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentD
if st := m.Access(ctx, parentDst, MODE_MASK_W|MODE_MASK_X, &dattr); st != 0 {
return st
}
// TODO: check parentDst is a subdir of source node
if ino == parentDst || ino == dattr.Parent {
return syscall.EPERM
}
m.parseAttr([]byte(rs[2].(string)), &iattr)
if (sattr.Flags&FlagAppend) != 0 || (sattr.Flags&FlagImmutable) != 0 || (dattr.Flags&FlagImmutable) != 0 || (iattr.Flags&FlagAppend) != 0 || (iattr.Flags&FlagImmutable) != 0 {
return syscall.EPERM
Expand Down Expand Up @@ -2207,6 +2220,17 @@ func (m *redisMeta) Read(ctx Context, inode Ino, indx uint32, slices *[]Slice) (
if err != nil {
return errno(err)
}
if len(vals) == 0 {
var attr Attr
eno := m.doGetAttr(ctx, inode, &attr)
if eno != 0 {
return eno
}
if attr.Typ != TypeFile {
return syscall.EPERM
}
return 0
}
ss := readSlices(vals)
if ss == nil {
return syscall.EIO
Expand Down
20 changes: 19 additions & 1 deletion pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,13 +1164,16 @@ func (m *dbMeta) doReadlink(ctx Context, inode Ino, noatime bool) (atime int64,
if !ok {
return syscall.ENOENT
}
if nodeAttr.Type != TypeSymlink {
return syscall.EINVAL
}
l := symlink{Inode: inode}
ok, e = s.Get(&l)
if e != nil {
return e
}
if !ok {
return syscall.ENOENT
return syscall.EIO
}
m.parseAttr(&nodeAttr, attr)
target = l.Target
Expand Down Expand Up @@ -1696,6 +1699,10 @@ func (m *dbMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentDst
}
return nil
}
// TODO: check parentDst is a subdir of source node
if se.Inode == parentDst || se.Inode == dpattr.Parent {
return syscall.EPERM
}
var sn = node{Inode: se.Inode}
ok, err = s.Get(&sn)
if err != nil {
Expand Down Expand Up @@ -2205,6 +2212,17 @@ func (m *dbMeta) Read(ctx Context, inode Ino, indx uint32, slices *[]Slice) (rer
if err != nil {
return errno(err)
}
if len(c.Slices) == 0 {
var attr Attr
eno := m.doGetAttr(ctx, inode, &attr)
if eno != 0 {
return eno
}
if attr.Typ != TypeFile {
return syscall.EPERM
}
return 0
}
ss := readSliceBuf(c.Slices)
if ss == nil {
return syscall.EIO
Expand Down
23 changes: 22 additions & 1 deletion pkg/meta/tkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,10 +1062,16 @@ func (m *kvMeta) doReadlink(ctx Context, inode Ino, noatime bool) (atime int64,
now := time.Now()
err = m.txn(func(tx *kvTxn) error {
rs := tx.gets(m.inodeKey(inode), m.symKey(inode))
if rs[0] == nil || rs[1] == nil {
if rs[0] == nil {
return syscall.ENOENT
}
m.parseAttr(rs[0], attr)
if attr.Typ != TypeSymlink {
return syscall.EINVAL
}
if rs[1] == nil {
return syscall.EIO
}
target = rs[1]
if !m.atimeNeedsUpdate(attr, now) {
return nil
Expand Down Expand Up @@ -1514,6 +1520,10 @@ func (m *kvMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentDst
if st := m.Access(ctx, parentDst, MODE_MASK_W|MODE_MASK_X, &dattr); st != 0 {
return st
}
// TODO: check parentDst is a subdir of source node
if ino == parentDst || ino == dattr.Parent {
return syscall.EPERM
}
m.parseAttr(rs[2], &iattr)
if (sattr.Flags&FlagAppend) != 0 || (sattr.Flags&FlagImmutable) != 0 || (dattr.Flags&FlagImmutable) != 0 || (iattr.Flags&FlagAppend) != 0 || (iattr.Flags&FlagImmutable) != 0 {
return syscall.EPERM
Expand Down Expand Up @@ -1895,6 +1905,17 @@ func (m *kvMeta) Read(ctx Context, inode Ino, indx uint32, slices *[]Slice) (rer
if err != nil {
return errno(err)
}
if len(val) == 0 {
var attr Attr
eno := m.doGetAttr(ctx, inode, &attr)
if eno != 0 {
return eno
}
if attr.Typ != TypeFile {
return syscall.EPERM
}
return 0
}
ss := readSliceBuf(val)
if ss == nil {
return syscall.EIO
Expand Down
2 changes: 1 addition & 1 deletion pkg/vfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Context = LogContext
const (
rootID = 1
maxName = meta.MaxName
maxSymlink = 4096
maxSymlink = meta.MaxSymlink
maxFileSize = meta.ChunkSize << 31
)

Expand Down

0 comments on commit 5d644fe

Please sign in to comment.