Fix uses of RootFromContext.

RootFromContext can return a dirent with reference taken, or nil. We must call
DecRef if (and only if) a real dirent is returned.

PiperOrigin-RevId: 242965515
Change-Id: Ie2b7b4cb19ee09b6ccf788b71f3fd7efcdf35a11
This commit is contained in:
Nicolas Lacasse
2019-04-10 16:36:28 -07:00
committed by Shentubot
parent c8368e477b
commit d93d19fd4e
12 changed files with 48 additions and 14 deletions
+7 -3
View File
@@ -188,11 +188,15 @@ func copyUpLocked(ctx context.Context, parent *Dirent, next *Dirent) error {
var childUpperInode *Inode
parentUpper := parent.Inode.overlay.upper
root := RootFromContext(ctx)
if root != nil {
defer root.DecRef()
}
// Create the file in the upper filesystem and get an Inode for it.
switch next.Inode.StableAttr.Type {
case RegularFile:
childFile, err := parentUpper.Create(ctx, RootFromContext(ctx), next.name, FileFlags{Read: true, Write: true}, attrs.Perms)
childFile, err := parentUpper.Create(ctx, root, next.name, FileFlags{Read: true, Write: true}, attrs.Perms)
if err != nil {
log.Warningf("copy up failed to create file: %v", err)
return syserror.EIO
@@ -201,7 +205,7 @@ func copyUpLocked(ctx context.Context, parent *Dirent, next *Dirent) error {
childUpperInode = childFile.Dirent.Inode
case Directory:
if err := parentUpper.CreateDirectory(ctx, RootFromContext(ctx), next.name, attrs.Perms); err != nil {
if err := parentUpper.CreateDirectory(ctx, root, next.name, attrs.Perms); err != nil {
log.Warningf("copy up failed to create directory: %v", err)
return syserror.EIO
}
@@ -221,7 +225,7 @@ func copyUpLocked(ctx context.Context, parent *Dirent, next *Dirent) error {
log.Warningf("copy up failed to read symlink value: %v", err)
return syserror.EIO
}
if err := parentUpper.CreateLink(ctx, RootFromContext(ctx), link, next.name); err != nil {
if err := parentUpper.CreateLink(ctx, root, link, next.name); err != nil {
log.Warningf("copy up failed to create symlink: %v", err)
return syserror.EIO
}
+5 -1
View File
@@ -410,7 +410,11 @@ func (f *File) ConfigureMMap(ctx context.Context, opts *memmap.MMapOpts) error {
// MappedName implements memmap.MappingIdentity.MappedName.
func (f *File) MappedName(ctx context.Context) string {
name, _ := f.Dirent.FullName(RootFromContext(ctx))
root := RootFromContext(ctx)
if root != nil {
defer root.DecRef()
}
name, _ := f.Dirent.FullName(root)
return name
}
+8 -2
View File
@@ -169,7 +169,9 @@ func (f *overlayFileOperations) Seek(ctx context.Context, file *File, whence See
// Readdir implements FileOperations.Readdir.
func (f *overlayFileOperations) Readdir(ctx context.Context, file *File, serializer DentrySerializer) (int64, error) {
root := RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
dirCtx := &DirCtx{
Serializer: serializer,
DirCursor: &f.dirCursor,
@@ -440,7 +442,11 @@ func (omi *overlayMappingIdentity) InodeID() uint64 {
// MappedName implements MappingIdentity.MappedName.
func (omi *overlayMappingIdentity) MappedName(ctx context.Context) string {
name, _ := omi.overlayFile.Dirent.FullName(RootFromContext(ctx))
root := RootFromContext(ctx)
if root != nil {
defer root.DecRef()
}
name, _ := omi.overlayFile.Dirent.FullName(root)
return name
}
+3 -1
View File
@@ -277,7 +277,9 @@ func (sdfo *StaticDirFileOperations) IterateDir(ctx context.Context, dirCtx *fs.
// Readdir implements fs.FileOperations.Readdir.
func (sdfo *StaticDirFileOperations) Readdir(ctx context.Context, file *fs.File, serializer fs.DentrySerializer) (int64, error) {
root := fs.RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
dirCtx := &fs.DirCtx{
Serializer: serializer,
DirCursor: &sdfo.dirCursor,
+3 -1
View File
@@ -120,7 +120,9 @@ func (f *fileOperations) Release() {
// Readdir implements fs.FileOperations.Readdir.
func (f *fileOperations) Readdir(ctx context.Context, file *fs.File, serializer fs.DentrySerializer) (int64, error) {
root := fs.RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
dirCtx := &fs.DirCtx{
Serializer: serializer,
+3 -1
View File
@@ -166,7 +166,9 @@ func (f *fileOperations) Readiness(mask waiter.EventMask) waiter.EventMask {
// Readdir implements fs.FileOperations.Readdir.
func (f *fileOperations) Readdir(ctx context.Context, file *fs.File, serializer fs.DentrySerializer) (int64, error) {
root := fs.RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
dirCtx := &fs.DirCtx{
Serializer: serializer,
DirCursor: &f.dirCursor,
+3 -1
View File
@@ -119,7 +119,9 @@ func (f *fd) GetFile(context.Context, *fs.Dirent, fs.FileFlags) (*fs.File, error
// Readlink returns the current target.
func (f *fd) Readlink(ctx context.Context, _ *fs.Inode) (string, error) {
root := fs.RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
n, _ := f.Dirent.FullName(root)
return n, nil
}
+3 -1
View File
@@ -210,7 +210,9 @@ func (rpf *rootProcFile) Readdir(ctx context.Context, file *fs.File, ser fs.Dent
// Add dot and dotdot.
root := fs.RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
dot, dotdot := file.Dirent.GetDotAttrs(root)
names = append(names, ".", "..")
m["."] = dot
+3 -1
View File
@@ -162,7 +162,9 @@ func (f *subtasksFile) Readdir(ctx context.Context, file *fs.File, ser fs.Dentry
if offset == 0 {
// Serialize "." and "..".
root := fs.RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
dot, dotdot := file.Dirent.GetDotAttrs(root)
if err := dirCtx.DirEmit(".", dot); err != nil {
return offset, err
+3 -1
View File
@@ -417,7 +417,9 @@ func (dfo *dirFileOperations) IterateDir(ctx context.Context, dirCtx *fs.DirCtx,
// Readdir implements FileOperations.Readdir.
func (dfo *dirFileOperations) Readdir(ctx context.Context, file *fs.File, serializer fs.DentrySerializer) (int64, error) {
root := fs.RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
dirCtx := &fs.DirCtx{
Serializer: serializer,
DirCursor: &dfo.dirCursor,
+3 -1
View File
@@ -315,7 +315,9 @@ func (df *dirFileOperations) IterateDir(ctx context.Context, dirCtx *fs.DirCtx,
// Readdir implements FileOperations.Readdir.
func (df *dirFileOperations) Readdir(ctx context.Context, file *fs.File, serializer fs.DentrySerializer) (int64, error) {
root := fs.RootFromContext(ctx)
defer root.DecRef()
if root != nil {
defer root.DecRef()
}
dirCtx := &fs.DirCtx{
Serializer: serializer,
DirCursor: &df.dirCursor,
+4
View File
@@ -676,6 +676,10 @@ func (k *Kernel) CreateProcess(args CreateProcessArgs) (*ThreadGroup, ThreadID,
root := args.Root
if root == nil {
root = fs.RootFromContext(ctx)
// Is the root STILL nil?
if root == nil {
return nil, 0, fmt.Errorf("CreateProcessArgs.Root was not provided, and failed to get root from context")
}
}
defer root.DecRef()
args.Root = nil