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         struct iattr newattrs;
 139 
 140         error = namei(filename,&inode);
 141         if (error)
 142                 return error;
 143         if (IS_RDONLY(inode)) {
 144                 iput(inode);
 145                 return -EROFS;
 146         }
 147         if (times) {
 148                 if ((current->fsuid != inode->i_uid) && !fsuser()) {
 149                         iput(inode);
 150                         return -EPERM;
 151                 }
 152                 actime = get_fs_long((unsigned long *) &times->actime);
 153                 modtime = get_fs_long((unsigned long *) &times->modtime);
 154                 newattrs.ia_ctime = CURRENT_TIME;
 155         } else {
 156                 if ((current->fsuid != inode->i_uid) &&
 157                     !permission(inode,MAY_WRITE)) {
 158                         iput(inode);
 159                         return -EACCES;
 160                 }
 161                 actime = modtime = newattrs.ia_ctime = CURRENT_TIME;
 162         }
 163         newattrs.ia_atime = actime;
 164         newattrs.ia_mtime = modtime;
 165         newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
 166         inode->i_dirt = 1;
 167         error = notify_change(inode, &newattrs);
 168         iput(inode);
 169         return error;
 170 }
 171 
 172 /*
 173  * access() needs to use the real uid/gid, not the effective uid/gid.
 174  * We do this by temporarily setting fsuid/fsgid to the wanted values
 175  */
 176 asmlinkage int sys_access(const char * filename, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 177 {
 178         struct inode * inode;
 179         int old_fsuid, old_fsgid;
 180         int res;
 181 
 182         if (mode != (mode & S_IRWXO))   /* where's F_OK, X_OK, W_OK, R_OK? */
 183                 return -EINVAL;
 184         old_fsuid = current->fsuid;
 185         old_fsgid = current->fsgid;
 186         current->fsuid = current->uid;
 187         current->fsgid = current->gid;
 188         res = namei(filename,&inode);
 189         if (!res) {
 190                 if (!permission(inode, mode))
 191                         res = -EACCES;
 192                 iput(inode);
 193         }
 194         current->fsuid = old_fsuid;
 195         current->fsgid = old_fsgid;
 196         return res;
 197 }
 198 
 199 asmlinkage int sys_chdir(const char * filename)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         struct inode * inode;
 202         int error;
 203 
 204         error = namei(filename,&inode);
 205         if (error)
 206                 return error;
 207         if (!S_ISDIR(inode->i_mode)) {
 208                 iput(inode);
 209                 return -ENOTDIR;
 210         }
 211         if (!permission(inode,MAY_EXEC)) {
 212                 iput(inode);
 213                 return -EACCES;
 214         }
 215         iput(current->fs->pwd);
 216         current->fs->pwd = inode;
 217         return (0);
 218 }
 219 
 220 asmlinkage int sys_fchdir(unsigned int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 221 {
 222         struct inode * inode;
 223         struct file * file;
 224 
 225         if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
 226                 return -EBADF;
 227         if (!(inode = file->f_inode))
 228                 return -ENOENT;
 229         if (!S_ISDIR(inode->i_mode))
 230                 return -ENOTDIR;
 231         if (!permission(inode,MAY_EXEC))
 232                 return -EACCES;
 233         iput(current->fs->pwd);
 234         current->fs->pwd = inode;
 235         inode->i_count++;
 236         return (0);
 237 }
 238 
 239 asmlinkage int sys_chroot(const char * filename)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241         struct inode * inode;
 242         int error;
 243 
 244         error = namei(filename,&inode);
 245         if (error)
 246                 return error;
 247         if (!S_ISDIR(inode->i_mode)) {
 248                 iput(inode);
 249                 return -ENOTDIR;
 250         }
 251         if (!fsuser()) {
 252                 iput(inode);
 253                 return -EPERM;
 254         }
 255         iput(current->fs->root);
 256         current->fs->root = inode;
 257         return (0);
 258 }
 259 
 260 asmlinkage int sys_fchmod(unsigned int fd, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262         struct inode * inode;
 263         struct file * file;
 264         struct iattr newattrs;
 265 
 266         if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
 267                 return -EBADF;
 268         if (!(inode = file->f_inode))
 269                 return -ENOENT;
 270         if (IS_RDONLY(inode))
 271                 return -EROFS;
 272         if (mode == (mode_t) -1)
 273                 mode = inode->i_mode;
 274         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
 275         newattrs.ia_ctime = CURRENT_TIME;
 276         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
 277         inode->i_dirt = 1;
 278         return notify_change(inode, &newattrs);
 279 }
 280 
 281 asmlinkage int sys_chmod(const char * filename, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283         struct inode * inode;
 284         int error;
 285         struct iattr newattrs;
 286 
 287         error = namei(filename,&inode);
 288         if (error)
 289                 return error;
 290         if (IS_RDONLY(inode)) {
 291                 iput(inode);
 292                 return -EROFS;
 293         }
 294         if (mode == (mode_t) -1)
 295                 mode = inode->i_mode;
 296         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
 297         newattrs.ia_ctime = CURRENT_TIME;
 298         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
 299         inode->i_dirt = 1;
 300         error = notify_change(inode, &newattrs);
 301         iput(inode);
 302         return error;
 303 }
 304 
 305 asmlinkage int sys_fchown(unsigned int fd, uid_t user, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help] */
 306 {
 307         struct inode * inode;
 308         struct file * file;
 309         struct iattr newattrs;
 310 
 311         if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
 312                 return -EBADF;
 313         if (!(inode = file->f_inode))
 314                 return -ENOENT;
 315         if (IS_RDONLY(inode))
 316                 return -EROFS;
 317         if (user == (uid_t) -1)
 318                 user = inode->i_uid;
 319         if (group == (gid_t) -1)
 320                 group = inode->i_gid;
 321         newattrs.ia_mode = inode->i_mode;
 322         newattrs.ia_uid = user;
 323         newattrs.ia_gid = group;
 324         newattrs.ia_ctime = CURRENT_TIME;
 325         newattrs.ia_valid =  ATTR_UID | ATTR_GID | ATTR_CTIME;
 326         /*
 327          * If the owner has been changed, remove the setuid bit
 328          */
 329         if (user != inode->i_uid && (inode->i_mode & S_ISUID)) {
 330                 newattrs.ia_mode &= ~S_ISUID;
 331                 newattrs.ia_valid |= ATTR_MODE;
 332         }
 333         /*
 334          * If the group has been changed, remove the setgid bit
 335          */
 336         if (group != inode->i_gid && (inode->i_mode & S_ISGID)) {
 337                 newattrs.ia_mode &= ~S_ISGID;
 338                 newattrs.ia_valid |= ATTR_MODE;
 339         }
 340         inode->i_dirt = 1;
 341         return notify_change(inode, &newattrs);
 342 }
 343 
 344 asmlinkage int sys_chown(const char * filename, uid_t user, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346         struct inode * inode;
 347         int error;
 348         struct iattr newattrs;
 349 
 350         error = lnamei(filename,&inode);
 351         if (error)
 352                 return error;
 353         if (IS_RDONLY(inode)) {
 354                 iput(inode);
 355                 return -EROFS;
 356         }
 357         if (user == (uid_t) -1)
 358                 user = inode->i_uid;
 359         if (group == (gid_t) -1)
 360                 group = inode->i_gid;
 361         newattrs.ia_mode = inode->i_mode;
 362         newattrs.ia_uid = user;
 363         newattrs.ia_gid = group;
 364         newattrs.ia_ctime = CURRENT_TIME;
 365         newattrs.ia_valid =  ATTR_UID | ATTR_GID | ATTR_CTIME;
 366         /*
 367          * If the owner has been changed, remove the setuid bit
 368          */
 369         if (user != inode->i_uid && (inode->i_mode & S_ISUID)) {
 370                 newattrs.ia_mode &= ~S_ISUID;
 371                 newattrs.ia_valid |= ATTR_MODE;
 372         }
 373         /*
 374          * If the group has been changed, remove the setgid bit
 375          */
 376         if (group != inode->i_gid && (inode->i_mode & S_ISGID)) {
 377                 newattrs.ia_mode &= ~S_ISGID;
 378                 newattrs.ia_valid |= ATTR_MODE;
 379         }
 380         inode->i_dirt = 1;
 381         error = notify_change(inode, &newattrs);
 382         iput(inode);
 383         return(error);
 384 }
 385 
 386 /*
 387  * Note that while the flag value (low two bits) for sys_open means:
 388  *      00 - read-only
 389  *      01 - write-only
 390  *      10 - read-write
 391  *      11 - special
 392  * it is changed into
 393  *      00 - no permissions needed
 394  *      01 - read-permission
 395  *      10 - write-permission
 396  *      11 - read-write
 397  * for the internal routines (ie open_namei()/follow_link() etc). 00 is
 398  * used by symlinks.
 399  */
 400 int do_open(const char * filename,int flags,int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402         struct inode * inode;
 403         struct file * f;
 404         int flag,error,fd;
 405 
 406         for(fd=0 ; fd<NR_OPEN ; fd++)
 407                 if (!current->files->fd[fd])
 408                         break;
 409         if (fd>=NR_OPEN)
 410                 return -EMFILE;
 411         FD_CLR(fd,&current->files->close_on_exec);
 412         f = get_empty_filp();
 413         if (!f)
 414                 return -ENFILE;
 415         current->files->fd[fd] = f;
 416         f->f_flags = flag = flags;
 417         f->f_mode = (flag+1) & O_ACCMODE;
 418         if (f->f_mode)
 419                 flag++;
 420         if (flag & (O_TRUNC | O_CREAT))
 421                 flag |= 2;
 422         error = open_namei(filename,flag,mode,&inode,NULL);
 423         if (!error && (f->f_mode & 2))
 424                 error = get_write_access(inode);
 425         if (error) {
 426                 current->files->fd[fd]=NULL;
 427                 f->f_count--;
 428                 return error;
 429         }
 430 
 431         f->f_inode = inode;
 432         f->f_pos = 0;
 433         f->f_reada = 0;
 434         f->f_op = NULL;
 435         if (inode->i_op)
 436                 f->f_op = inode->i_op->default_file_ops;
 437         if (f->f_op && f->f_op->open) {
 438                 error = f->f_op->open(inode,f);
 439                 if (error) {
 440                         if (f->f_mode & 2) put_write_access(inode);
 441                         iput(inode);
 442                         f->f_count--;
 443                         current->files->fd[fd]=NULL;
 444                         return error;
 445                 }
 446         }
 447         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
 448         return (fd);
 449 }
 450 
 451 asmlinkage int sys_open(const char * filename,int flags,int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 452 {
 453         char * tmp;
 454         int error;
 455 
 456         error = getname(filename, &tmp);
 457         if (error)
 458                 return error;
 459         error = do_open(tmp,flags,mode);
 460         putname(tmp);
 461         return error;
 462 }
 463 
 464 asmlinkage int sys_creat(const char * pathname, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 465 {
 466         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
 467 }
 468 
 469 int close_fp(struct file *filp, unsigned int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 470 {
 471         struct inode *inode;
 472 
 473         if (filp->f_count == 0) {
 474                 printk("VFS: Close: file count is 0\n");
 475                 return 0;
 476         }
 477         inode = filp->f_inode;
 478         if (inode)
 479                 fcntl_remove_locks(current, filp, fd);
 480         if (filp->f_count > 1) {
 481                 filp->f_count--;
 482                 return 0;
 483         }
 484         if (filp->f_op && filp->f_op->release)
 485                 filp->f_op->release(inode,filp);
 486         filp->f_count--;
 487         filp->f_inode = NULL;
 488         if (filp->f_mode & 2) put_write_access(inode);
 489         iput(inode);
 490         return 0;
 491 }
 492 
 493 asmlinkage int sys_close(unsigned int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 494 {       
 495         struct file * filp;
 496 
 497         if (fd >= NR_OPEN)
 498                 return -EBADF;
 499         FD_CLR(fd, &current->files->close_on_exec);
 500         if (!(filp = current->files->fd[fd]))
 501                 return -EBADF;
 502         current->files->fd[fd] = NULL;
 503         return (close_fp (filp, fd));
 504 }
 505 
 506 /*
 507  * This routine simulates a hangup on the tty, to arrange that users
 508  * are given clean terminals at login time.
 509  */
 510 asmlinkage int sys_vhangup(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 511 {
 512         if (!suser())
 513                 return -EPERM;
 514         /* If there is a controlling tty, hang it up */
 515         if (current->tty)
 516                 tty_vhangup(current->tty);
 517         return 0;
 518 }

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