root/fs/open.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_ustat
  2. sys_statfs
  3. sys_fstatfs
  4. sys_truncate
  5. sys_ftruncate
  6. sys_utime
  7. sys_access
  8. sys_chdir
  9. sys_fchdir
  10. sys_chroot
  11. sys_fchmod
  12. sys_chmod
  13. sys_fchown
  14. sys_chown
  15. do_open
  16. sys_open
  17. sys_creat
  18. close_fp
  19. sys_close
  20. sys_vhangup

   1 /*
   2  *  linux/fs/open.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/vfs.h>
   8 #include <linux/types.h>
   9 #include <linux/utime.h>
  10 #include <linux/errno.h>
  11 #include <linux/fcntl.h>
  12 #include <linux/stat.h>
  13 #include <linux/string.h>
  14 #include <linux/sched.h>
  15 #include <linux/kernel.h>
  16 #include <linux/signal.h>
  17 #include <linux/tty.h>
  18 #include <linux/time.h>
  19 
  20 #include <asm/segment.h>
  21 
  22 extern void fcntl_remove_locks(struct task_struct *, struct file *, unsigned int fd);
  23 
  24 asmlinkage int sys_ustat(int dev, struct ustat * ubuf)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         return -ENOSYS;
  27 }
  28 
  29 asmlinkage int sys_statfs(const char * path, struct statfs * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
  30 {
  31         struct inode * inode;
  32         int error;
  33 
  34         error = verify_area(VERIFY_WRITE, buf, sizeof(struct statfs));
  35         if (error)
  36                 return error;
  37         error = namei(path,&inode);
  38         if (error)
  39                 return error;
  40         if (!inode->i_sb->s_op->statfs) {
  41                 iput(inode);
  42                 return -ENOSYS;
  43         }
  44         inode->i_sb->s_op->statfs(inode->i_sb, buf);
  45         iput(inode);
  46         return 0;
  47 }
  48 
  49 asmlinkage int sys_fstatfs(unsigned int fd, struct statfs * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51         struct inode * inode;
  52         struct file * file;
  53         int error;
  54 
  55         error = verify_area(VERIFY_WRITE, buf, sizeof(struct statfs));
  56         if (error)
  57                 return error;
  58         if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
  59                 return -EBADF;
  60         if (!(inode = file->f_inode))
  61                 return -ENOENT;
  62         if (!inode->i_sb->s_op->statfs)
  63                 return -ENOSYS;
  64         inode->i_sb->s_op->statfs(inode->i_sb, buf);
  65         return 0;
  66 }
  67 
  68 asmlinkage int sys_truncate(const char * path, unsigned int length)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70         struct inode * inode;
  71         int error;
  72         struct iattr newattrs;
  73 
  74         error = namei(path,&inode);
  75         if (error)
  76                 return error;
  77         if (S_ISDIR(inode->i_mode) || !permission(inode,MAY_WRITE)) {
  78                 iput(inode);
  79                 return -EACCES;
  80         }
  81         if (IS_RDONLY(inode)) {
  82                 iput(inode);
  83                 return -EROFS;
  84         }
  85         if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
  86                 iput(inode);
  87                 return -EPERM;
  88         }
  89         error = get_write_access(inode);
  90         if (error) {
  91                 iput(inode);
  92                 return error;
  93         }
  94         inode->i_size = newattrs.ia_size = length;
  95         if (inode->i_op && inode->i_op->truncate)
  96                 inode->i_op->truncate(inode);
  97         newattrs.ia_ctime = newattrs.ia_mtime = CURRENT_TIME;
  98         newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME;
  99         inode->i_dirt = 1;
 100         error = notify_change(inode, &newattrs);
 101         put_write_access(inode);
 102         iput(inode);
 103         return error;
 104 }
 105 
 106 asmlinkage int sys_ftruncate(unsigned int fd, unsigned int length)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108         struct inode * inode;
 109         struct file * file;
 110         struct iattr newattrs;
 111 
 112         if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
 113                 return -EBADF;
 114         if (!(inode = file->f_inode))
 115                 return -ENOENT;
 116         if (S_ISDIR(inode->i_mode) || !(file->f_mode & 2))
 117                 return -EACCES;
 118         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
 119                 return -EPERM;
 120         inode->i_size = newattrs.ia_size = length;
 121         if (inode->i_op && inode->i_op->truncate)
 122                 inode->i_op->truncate(inode);
 123         newattrs.ia_ctime = newattrs.ia_mtime = CURRENT_TIME;
 124         newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME;
 125         inode->i_dirt = 1;
 126         return notify_change(inode, &newattrs);
 127 }
 128 
 129 /* If times==NULL, set access and modification to current time,
 130  * must be owner or have write permission.
 131  * Else, update from *times, must be owner or super user.
 132  */
 133 asmlinkage int sys_utime(char * filename, struct utimbuf * times)
     /* [previous][next][first][last][top][bottom][index][help] */
 134 {
 135         struct inode * inode;
 136         long actime,modtime;
 137         int error;
 138         unsigned int flags = 0;
 139         struct iattr newattrs;
 140 
 141         error = namei(filename,&inode);
 142         if (error)
 143                 return error;
 144         if (IS_RDONLY(inode)) {
 145                 iput(inode);
 146                 return -EROFS;
 147         }
 148         /* Don't worry, the checks are done in inode_change_ok() */
 149         if (times) {
 150                 actime = get_fs_long((unsigned long *) &times->actime);
 151                 modtime = get_fs_long((unsigned long *) &times->modtime);
 152                 newattrs.ia_ctime = CURRENT_TIME;
 153                 flags = ATTR_ATIME_SET | ATTR_MTIME_SET;
 154         } else {
 155                 if (!permission(inode,MAY_WRITE)) {
 156                         iput(inode);
 157                         return -EACCES;
 158                 }
 159                 actime = modtime = newattrs.ia_ctime = CURRENT_TIME;
 160         }
 161         newattrs.ia_atime = actime;
 162         newattrs.ia_mtime = modtime;
 163         newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME | flags;
 164         inode->i_dirt = 1;
 165         error = notify_change(inode, &newattrs);
 166         iput(inode);
 167         return error;
 168 }
 169 
 170 /*
 171  * access() needs to use the real uid/gid, not the effective uid/gid.
 172  * We do this by temporarily setting fsuid/fsgid to the wanted values
 173  */
 174 asmlinkage int sys_access(const char * filename, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176         struct inode * inode;
 177         int old_fsuid, old_fsgid;
 178         int res;
 179 
 180         if (mode != (mode & S_IRWXO))   /* where's F_OK, X_OK, W_OK, R_OK? */
 181                 return -EINVAL;
 182         old_fsuid = current->fsuid;
 183         old_fsgid = current->fsgid;
 184         current->fsuid = current->uid;
 185         current->fsgid = current->gid;
 186         res = namei(filename,&inode);
 187         if (!res) {
 188                 if (!permission(inode, mode))
 189                         res = -EACCES;
 190                 iput(inode);
 191         }
 192         current->fsuid = old_fsuid;
 193         current->fsgid = old_fsgid;
 194         return res;
 195 }
 196 
 197 asmlinkage int sys_chdir(const char * filename)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199         struct inode * inode;
 200         int error;
 201 
 202         error = namei(filename,&inode);
 203         if (error)
 204                 return error;
 205         if (!S_ISDIR(inode->i_mode)) {
 206                 iput(inode);
 207                 return -ENOTDIR;
 208         }
 209         if (!permission(inode,MAY_EXEC)) {
 210                 iput(inode);
 211                 return -EACCES;
 212         }
 213         iput(current->fs->pwd);
 214         current->fs->pwd = inode;
 215         return (0);
 216 }
 217 
 218 asmlinkage int sys_fchdir(unsigned int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 219 {
 220         struct inode * inode;
 221         struct file * file;
 222 
 223         if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
 224                 return -EBADF;
 225         if (!(inode = file->f_inode))
 226                 return -ENOENT;
 227         if (!S_ISDIR(inode->i_mode))
 228                 return -ENOTDIR;
 229         if (!permission(inode,MAY_EXEC))
 230                 return -EACCES;
 231         iput(current->fs->pwd);
 232         current->fs->pwd = inode;
 233         inode->i_count++;
 234         return (0);
 235 }
 236 
 237 asmlinkage int sys_chroot(const char * filename)
     /* [previous][next][first][last][top][bottom][index][help] */
 238 {
 239         struct inode * inode;
 240         int error;
 241 
 242         error = namei(filename,&inode);
 243         if (error)
 244                 return error;
 245         if (!S_ISDIR(inode->i_mode)) {
 246                 iput(inode);
 247                 return -ENOTDIR;
 248         }
 249         if (!fsuser()) {
 250                 iput(inode);
 251                 return -EPERM;
 252         }
 253         iput(current->fs->root);
 254         current->fs->root = inode;
 255         return (0);
 256 }
 257 
 258 asmlinkage int sys_fchmod(unsigned int fd, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 259 {
 260         struct inode * inode;
 261         struct file * file;
 262         struct iattr newattrs;
 263 
 264         if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
 265                 return -EBADF;
 266         if (!(inode = file->f_inode))
 267                 return -ENOENT;
 268         if (IS_RDONLY(inode))
 269                 return -EROFS;
 270         if (mode == (mode_t) -1)
 271                 mode = inode->i_mode;
 272         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
 273         newattrs.ia_ctime = CURRENT_TIME;
 274         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
 275         inode->i_dirt = 1;
 276         return notify_change(inode, &newattrs);
 277 }
 278 
 279 asmlinkage int sys_chmod(const char * filename, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 280 {
 281         struct inode * inode;
 282         int error;
 283         struct iattr newattrs;
 284 
 285         error = namei(filename,&inode);
 286         if (error)
 287                 return error;
 288         if (IS_RDONLY(inode)) {
 289                 iput(inode);
 290                 return -EROFS;
 291         }
 292         if (mode == (mode_t) -1)
 293                 mode = inode->i_mode;
 294         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
 295         newattrs.ia_ctime = CURRENT_TIME;
 296         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
 297         inode->i_dirt = 1;
 298         error = notify_change(inode, &newattrs);
 299         iput(inode);
 300         return error;
 301 }
 302 
 303 asmlinkage int sys_fchown(unsigned int fd, uid_t user, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help] */
 304 {
 305         struct inode * inode;
 306         struct file * file;
 307         struct iattr newattrs;
 308 
 309         if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
 310                 return -EBADF;
 311         if (!(inode = file->f_inode))
 312                 return -ENOENT;
 313         if (IS_RDONLY(inode))
 314                 return -EROFS;
 315         if (user == (uid_t) -1)
 316                 user = inode->i_uid;
 317         if (group == (gid_t) -1)
 318                 group = inode->i_gid;
 319         newattrs.ia_mode = inode->i_mode;
 320         newattrs.ia_uid = user;
 321         newattrs.ia_gid = group;
 322         newattrs.ia_ctime = CURRENT_TIME;
 323         newattrs.ia_valid =  ATTR_UID | ATTR_GID | ATTR_CTIME;
 324         /*
 325          * If the owner has been changed, remove the setuid bit
 326          */
 327         if (user != inode->i_uid && (inode->i_mode & S_ISUID)) {
 328                 newattrs.ia_mode &= ~S_ISUID;
 329                 newattrs.ia_valid |= ATTR_MODE;
 330         }
 331         /*
 332          * If the group has been changed, remove the setgid bit
 333          */
 334         if (group != inode->i_gid && (inode->i_mode & S_ISGID)) {
 335                 newattrs.ia_mode &= ~S_ISGID;
 336                 newattrs.ia_valid |= ATTR_MODE;
 337         }
 338         inode->i_dirt = 1;
 339         return notify_change(inode, &newattrs);
 340 }
 341 
 342 asmlinkage int sys_chown(const char * filename, uid_t user, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help] */
 343 {
 344         struct inode * inode;
 345         int error;
 346         struct iattr newattrs;
 347 
 348         error = lnamei(filename,&inode);
 349         if (error)
 350                 return error;
 351         if (IS_RDONLY(inode)) {
 352                 iput(inode);
 353                 return -EROFS;
 354         }
 355         if (user == (uid_t) -1)
 356                 user = inode->i_uid;
 357         if (group == (gid_t) -1)
 358                 group = inode->i_gid;
 359         newattrs.ia_mode = inode->i_mode;
 360         newattrs.ia_uid = user;
 361         newattrs.ia_gid = group;
 362         newattrs.ia_ctime = CURRENT_TIME;
 363         newattrs.ia_valid =  ATTR_UID | ATTR_GID | ATTR_CTIME;
 364         /*
 365          * If the owner has been changed, remove the setuid bit
 366          */
 367         if (user != inode->i_uid && (inode->i_mode & S_ISUID)) {
 368                 newattrs.ia_mode &= ~S_ISUID;
 369                 newattrs.ia_valid |= ATTR_MODE;
 370         }
 371         /*
 372          * If the group has been changed, remove the setgid bit
 373          */
 374         if (group != inode->i_gid && (inode->i_mode & S_ISGID)) {
 375                 newattrs.ia_mode &= ~S_ISGID;
 376                 newattrs.ia_valid |= ATTR_MODE;
 377         }
 378         inode->i_dirt = 1;
 379         error = notify_change(inode, &newattrs);
 380         iput(inode);
 381         return(error);
 382 }
 383 
 384 /*
 385  * Note that while the flag value (low two bits) for sys_open means:
 386  *      00 - read-only
 387  *      01 - write-only
 388  *      10 - read-write
 389  *      11 - special
 390  * it is changed into
 391  *      00 - no permissions needed
 392  *      01 - read-permission
 393  *      10 - write-permission
 394  *      11 - read-write
 395  * for the internal routines (ie open_namei()/follow_link() etc). 00 is
 396  * used by symlinks.
 397  */
 398 int do_open(const char * filename,int flags,int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 399 {
 400         struct inode * inode;
 401         struct file * f;
 402         int flag,error,fd;
 403 
 404         for(fd=0 ; fd<NR_OPEN ; fd++)
 405                 if (!current->files->fd[fd])
 406                         break;
 407         if (fd>=NR_OPEN)
 408                 return -EMFILE;
 409         FD_CLR(fd,&current->files->close_on_exec);
 410         f = get_empty_filp();
 411         if (!f)
 412                 return -ENFILE;
 413         current->files->fd[fd] = f;
 414         f->f_flags = flag = flags;
 415         f->f_mode = (flag+1) & O_ACCMODE;
 416         if (f->f_mode)
 417                 flag++;
 418         if (flag & (O_TRUNC | O_CREAT))
 419                 flag |= 2;
 420         error = open_namei(filename,flag,mode,&inode,NULL);
 421         if (!error && (f->f_mode & 2))
 422                 error = get_write_access(inode);
 423         if (error) {
 424                 current->files->fd[fd]=NULL;
 425                 f->f_count--;
 426                 return error;
 427         }
 428 
 429         f->f_inode = inode;
 430         f->f_pos = 0;
 431         f->f_reada = 0;
 432         f->f_op = NULL;
 433         if (inode->i_op)
 434                 f->f_op = inode->i_op->default_file_ops;
 435         if (f->f_op && f->f_op->open) {
 436                 error = f->f_op->open(inode,f);
 437                 if (error) {
 438                         if (f->f_mode & 2) put_write_access(inode);
 439                         iput(inode);
 440                         f->f_count--;
 441                         current->files->fd[fd]=NULL;
 442                         return error;
 443                 }
 444         }
 445         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
 446         return (fd);
 447 }
 448 
 449 asmlinkage int sys_open(const char * filename,int flags,int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 450 {
 451         char * tmp;
 452         int error;
 453 
 454         error = getname(filename, &tmp);
 455         if (error)
 456                 return error;
 457         error = do_open(tmp,flags,mode);
 458         putname(tmp);
 459         return error;
 460 }
 461 
 462 asmlinkage int sys_creat(const char * pathname, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 463 {
 464         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
 465 }
 466 
 467 int close_fp(struct file *filp, unsigned int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 468 {
 469         struct inode *inode;
 470 
 471         if (filp->f_count == 0) {
 472                 printk("VFS: Close: file count is 0\n");
 473                 return 0;
 474         }
 475         inode = filp->f_inode;
 476         if (inode)
 477                 fcntl_remove_locks(current, filp, fd);
 478         if (filp->f_count > 1) {
 479                 filp->f_count--;
 480                 return 0;
 481         }
 482         if (filp->f_op && filp->f_op->release)
 483                 filp->f_op->release(inode,filp);
 484         filp->f_count--;
 485         filp->f_inode = NULL;
 486         if (filp->f_mode & 2) put_write_access(inode);
 487         iput(inode);
 488         return 0;
 489 }
 490 
 491 asmlinkage int sys_close(unsigned int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 492 {       
 493         struct file * filp;
 494 
 495         if (fd >= NR_OPEN)
 496                 return -EBADF;
 497         FD_CLR(fd, &current->files->close_on_exec);
 498         if (!(filp = current->files->fd[fd]))
 499                 return -EBADF;
 500         current->files->fd[fd] = NULL;
 501         return (close_fp (filp, fd));
 502 }
 503 
 504 /*
 505  * This routine simulates a hangup on the tty, to arrange that users
 506  * are given clean terminals at login time.
 507  */
 508 asmlinkage int sys_vhangup(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 509 {
 510         if (!suser())
 511                 return -EPERM;
 512         /* If there is a controlling tty, hang it up */
 513         if (current->tty)
 514                 tty_vhangup(current->tty);
 515         return 0;
 516 }

/* [previous][next][first][last][top][bottom][index][help] */