Carnegie Mellon
SCS logo
Computer Science Department
home
syllabus
staff
schedule
lecture
projects
homeworks
QA
 
 

15-410 File System Interface


Your file system should support the following interface. The system call numbers are defined in syscall_nums.h. Please continue to use the same system call convention as in Project 3.

Basic Calls

  • int open(char *pathname)
    Opens the file named by pathname. If the file exists, this syscall sets the file position to the beginning of the file and returns a file descriptor number that can be used for future operations on this open file. If the file does not exist or pathname refers to a directory, open returns ERROR. open also returns ERROR if pathname is relative and the process' current director y has not yet been set with chdir, or if any internal file system error occurs. The file descriptor returned should be a small integer that uniquely identifies the open pathname file for as long as the file remains open. A single pathname name can be opened multiple times simultaneously, and each separate open should return a separate file descriptor. If pathname is a symbolic link, open attempts to open the file that the symbolic link points to, and not the symbolic link itself.

  • int close(int fd)
    Closes the open file specified by the file descriptor number fd. If fd is not the descriptor number of a file currently open in the system, close returns ERROR; otherwise, it returns SUCCESS.

  • int read(int fd, void *buf, int size)
    Reads data from the current file position of an open file. The argument fd specifies the file descriptor number of the open file to be read. buf specifies the address of the calling process' buffer into which to deposit the data from the file, and size is the number of bytes of data to be read. read returns the number of bytes read, which will be 0 if reading at the end-of-file. The number of bytes read will be the minimum of the number of bytes requested and the number of bytes remaining in the file between the current file position and the end-of-file. Any error should cause read to return ERROR.

  • int write(int fd, void *buf, int size)
    Writes data to the current file position of an open file. The argument fd specifies the file descriptor number of the open file to be written, buf specifies the address of the calling process' buffer from which to get the write data, and size is the number of bytes to be written to the file. write returns the number of bytes written, unless any error occurs, in which case it returns ERROR. It is not an error to write past the end of the file.

    If two processes (or threads) write to the same file "at the same time", their write() calls should be serialized (not intermixed).

  • int seek(int fd, int offset)
    Changes the current file position of the open file specified by fd. The argument offset specifies a byte offset in the file to which the current file position will be set. offset must be non-negative, and fd must be a currently valid descriptor. offset, however, may be larger than the current file size, in which case the file is automatically grown. seek returns the new file position or ERROR.

  • int filesize(char *pathname)
    Returns the size of the file pathname, in bytes. Returns ERROR if the file does not exist, if the file is a directory, or if any other error occurs.

  • int dup(int fd)
    Returns a new file descriptor based on the same file-open status as fd. That is, the two descriptors share the same file position. dup() should assign the new descriptor the lowest-numbered file descriptor slot not in use by the current process. In other words,
    close(0);
    x = dup(7);

    should result in x having the value 0, but now both 0 and 7 should refer to the same file-open state in the kernel.

Basic File Management

  • int create(char *pathname)
    Creates and opens the new file named pathname. If the file can be created and opened, create sets the file position to the beginning of the empty file and returns a file descriptor number that can be used for future operations on this open file. Otherwise, create returns ERROR. Note that create should return ERROR if pathname already exists, or if its parent directory does not exist, and also if pathname is a relative path but the process' current directory has not yet been set with chdir.

  • int unlink(char* pathname)
    Removes the directory entry for pathname, and, if pathname is the last reference to a file, also deletes the file itself by freeing all data and metadata used by the file. The file pathname must not be a directory. If pathname is a symbolic link, the unlink operation is attempted on the symbolic link itself, and not on the file the symbolic link points to. On success, unlink returns SUCCESS, and it returns ERROR otherwise.

Directory Management

  • int mkdir(char *pathname)
    Creates a new directory named pathname and creates the ``.'' and ``..'' entries in the new directory. If pathname exists and/or any other error occurs, mkdir returns ERROR. It returns SUCCESS otherwise.

  • int rmdir(char *pathname)
    Deletes the existing directory named pathname. pathname must be a directory, and it must contain no valid entries other than ``.'' and ``..''. rmdir returns ERROR if any error occurs, and returns SUCCESS otherwise.

  • int chdir(char *pathname)
    Changes the current directory of the current process to pathname. The notion of current directory is maintained by the file system for processes that use the file system, and is not reflected on disk. Before a process calls chdir, or if all previous calls from a process to chdir have returned ERROR, the current directory for that process is undefined, and all pathnames given to file system calls must be absolute paths (paths starting with ' /'.) After a successful call to chdir, the process may specify relative paths, that is, paths that do not start with ' /'. chdir returns ERROR if any error occurs (if pathname does not exist, for example), and returns SUCCESS otherwise.

  • int dirsize(char *pathname)
    Returns the number of entries in the directory pathname, including the entries for ``.'' and ``..''. Returns ERROR if pathname does not exist, if pathname is not a directory, or if any other error occurs.

  • int direntry(char *pathname, int entrypos, char *entryname)
    Sets entryname to the name of the entrypos'th entry within the directory specified by pathname. The entries within a directory can be returned in any order, but the order must remain the same for as long as no files are created or unlinked within it. 0 is a valid value for entrypos. direntry returns ERROR if pathname refers to a regular file, if pathname does not exist, if the directory has fewer than entrypos + 1 entries, or if any other error occurs. It returns SUCCESS otherwise. If direntry returns ERROR, the value of entryname is undefined. entryname is an address in the caller's memory space to which at least MAXFILENAMESIZE bytes have been allocated by the caller.

Extended File Management

  • int link(char *oldname, char *newname)
    Creates a hard link from the new file newname to the existing file oldname. The files oldname and newname need not be in the same directory. The file oldname must not be a directory. If newname exists, oldname does not exist, or if any file system limit prevents the completion of the operation, link returns ERROR, and it returns SUCCESS otherwise.

  • int symlink(char *oldname, char *newname)
    Creates a symbolic link from the new file newname to the existing file oldname. The files oldname and newname need not be in the same directory. The file oldname may be a directory and may not even exist. If newname exists or if any file system limit prevents the completion of the operation, symlink returns ERROR, and it returns SUCCESS otherwise.

File System Management

  • int sync()
    Writes all ``dirty'' cached metadata and data to the disk, thus ensuring that the disk contains a consistent copy of the file system. ``Dirty'' data (or metadata) is data whose value on disk is stale because the buffer cache's write-behind policy has not yet updated it. sync returns ERROR if any file system error prevents the sync operation from proceeding. Otherwise sync returns SUCCESS.

  • int mkfs(int max_inodes)
    Initializes the file system such that max_inodes inodes are available. This parameter determines the maximum number of files and directories that may be present on your file system. If max_inodes is 0, mkfs estimates a reasonable number for max_inodes based on the size of the disk. If max_inodes is less than 0 or greater than 8192, or if any error prevents the initialization from completing, ERROR is returned. Otherwise mkfs returns SUCCESS.


[Last modified Saturday January 10, 2004]