root/fs/inode.c

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

DEFINITIONS

This source file includes following definitions.
  1. hashfn
  2. insert_inode_free
  3. remove_inode_free
  4. insert_inode_hash
  5. remove_inode_hash
  6. put_last_free
  7. grow_inodes
  8. inode_init
  9. wait_on_inode
  10. lock_inode
  11. unlock_inode
  12. clear_inode
  13. fs_may_mount
  14. fs_may_umount
  15. fs_may_remount_ro
  16. write_inode
  17. read_inode
  18. inode_change_ok
  19. inode_setattr
  20. notify_change
  21. bmap
  22. invalidate_inodes
  23. sync_inodes
  24. iput
  25. get_empty_inode
  26. get_pipe_inode
  27. __iget
  28. __wait_on_inode

   1 /*
   2  *  linux/fs/inode.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/stat.h>
   8 #include <linux/sched.h>
   9 #include <linux/kernel.h>
  10 #include <linux/mm.h>
  11 #include <linux/string.h>
  12 
  13 #include <asm/system.h>
  14 
  15 static struct inode_hash_entry {
  16         struct inode * inode;
  17         int updating;
  18 } hash_table[NR_IHASH];
  19 
  20 static struct inode * first_inode;
  21 static struct wait_queue * inode_wait = NULL;
  22 static int nr_inodes = 0, nr_free_inodes = 0;
  23 
  24 static inline int const hashfn(kdev_t dev, unsigned int i)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         return (HASHDEV(dev) ^ i) % NR_IHASH;
  27 }
  28 
  29 static inline struct inode_hash_entry * const hash(kdev_t dev, int i)
  30 {
  31         return hash_table + hashfn(dev, i);
  32 }
  33 
  34 static inline void insert_inode_free(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36         inode->i_next = first_inode;
  37         inode->i_prev = first_inode->i_prev;
  38         inode->i_next->i_prev = inode;
  39         inode->i_prev->i_next = inode;
  40         first_inode = inode;
  41 }
  42 
  43 static inline void remove_inode_free(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45         if (first_inode == inode)
  46                 first_inode = first_inode->i_next;
  47         if (inode->i_next)
  48                 inode->i_next->i_prev = inode->i_prev;
  49         if (inode->i_prev)
  50                 inode->i_prev->i_next = inode->i_next;
  51         inode->i_next = inode->i_prev = NULL;
  52 }
  53 
  54 void insert_inode_hash(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56         struct inode_hash_entry *h;
  57         h = hash(inode->i_dev, inode->i_ino);
  58 
  59         inode->i_hash_next = h->inode;
  60         inode->i_hash_prev = NULL;
  61         if (inode->i_hash_next)
  62                 inode->i_hash_next->i_hash_prev = inode;
  63         h->inode = inode;
  64 }
  65 
  66 static inline void remove_inode_hash(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  67 {
  68         struct inode_hash_entry *h;
  69         h = hash(inode->i_dev, inode->i_ino);
  70 
  71         if (h->inode == inode)
  72                 h->inode = inode->i_hash_next;
  73         if (inode->i_hash_next)
  74                 inode->i_hash_next->i_hash_prev = inode->i_hash_prev;
  75         if (inode->i_hash_prev)
  76                 inode->i_hash_prev->i_hash_next = inode->i_hash_next;
  77         inode->i_hash_prev = inode->i_hash_next = NULL;
  78 }
  79 
  80 static inline void put_last_free(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82         remove_inode_free(inode);
  83         inode->i_prev = first_inode->i_prev;
  84         inode->i_prev->i_next = inode;
  85         inode->i_next = first_inode;
  86         inode->i_next->i_prev = inode;
  87 }
  88 
  89 void grow_inodes(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  90 {
  91         struct inode * inode;
  92         int i;
  93 
  94         if (!(inode = (struct inode*) get_free_page(GFP_KERNEL)))
  95                 return;
  96 
  97         i=PAGE_SIZE / sizeof(struct inode);
  98         nr_inodes += i;
  99         nr_free_inodes += i;
 100 
 101         if (!first_inode)
 102                 inode->i_next = inode->i_prev = first_inode = inode++, i--;
 103 
 104         for ( ; i ; i-- )
 105                 insert_inode_free(inode++);
 106 }
 107 
 108 unsigned long inode_init(unsigned long start, unsigned long end)
     /* [previous][next][first][last][top][bottom][index][help] */
 109 {
 110         memset(hash_table, 0, sizeof(hash_table));
 111         first_inode = NULL;
 112         return start;
 113 }
 114 
 115 static void __wait_on_inode(struct inode *);
 116 
 117 static inline void wait_on_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119         if (inode->i_lock)
 120                 __wait_on_inode(inode);
 121 }
 122 
 123 static inline void lock_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         wait_on_inode(inode);
 126         inode->i_lock = 1;
 127 }
 128 
 129 static inline void unlock_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131         inode->i_lock = 0;
 132         wake_up(&inode->i_wait);
 133 }
 134 
 135 /*
 136  * Note that we don't want to disturb any wait-queues when we discard
 137  * an inode.
 138  *
 139  * Argghh. Got bitten by a gcc problem with inlining: no way to tell
 140  * the compiler that the inline asm function 'memset' changes 'inode'.
 141  * I've been searching for the bug for days, and was getting desperate.
 142  * Finally looked at the assembler output... Grrr.
 143  *
 144  * The solution is the weird use of 'volatile'. Ho humm. Have to report
 145  * it to the gcc lists, and hope we can do this more cleanly some day..
 146  */
 147 void clear_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 148 {
 149         struct wait_queue * wait;
 150 
 151         wait_on_inode(inode);
 152         if (IS_WRITABLE(inode)) {
 153                 if (inode->i_sb && inode->i_sb->dq_op)
 154                         inode->i_sb->dq_op->drop(inode);
 155         }
 156         remove_inode_hash(inode);
 157         remove_inode_free(inode);
 158         wait = ((volatile struct inode *) inode)->i_wait;
 159         if (inode->i_count)
 160                 nr_free_inodes++;
 161         memset(inode,0,sizeof(*inode));
 162         ((volatile struct inode *) inode)->i_wait = wait;
 163         insert_inode_free(inode);
 164 }
 165 
 166 int fs_may_mount(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168         struct inode * inode, * next;
 169         int i;
 170 
 171         next = first_inode;
 172         for (i = nr_inodes ; i > 0 ; i--) {
 173                 inode = next;
 174                 next = inode->i_next;   /* clear_inode() changes the queues.. */
 175                 if (inode->i_dev != dev)
 176                         continue;
 177                 if (inode->i_count || inode->i_dirt || inode->i_lock)
 178                         return 0;
 179                 clear_inode(inode);
 180         }
 181         return 1;
 182 }
 183 
 184 int fs_may_umount(kdev_t dev, struct inode * mount_root)
     /* [previous][next][first][last][top][bottom][index][help] */
 185 {
 186         struct inode * inode;
 187         int i;
 188 
 189         inode = first_inode;
 190         for (i=0 ; i < nr_inodes ; i++, inode = inode->i_next) {
 191                 if (inode->i_dev != dev || !inode->i_count)
 192                         continue;
 193                 if (inode == mount_root && inode->i_count == 1)
 194                         continue;
 195                 return 0;
 196         }
 197         return 1;
 198 }
 199 
 200 int fs_may_remount_ro(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 201 {
 202         struct file * file;
 203         int i;
 204 
 205         /* Check that no files are currently opened for writing. */
 206         for (file = first_file, i=0; i<nr_files; i++, file=file->f_next) {
 207                 if (!file->f_count || !file->f_inode ||
 208                     file->f_inode->i_dev != dev)
 209                         continue;
 210                 if (S_ISREG(file->f_inode->i_mode) && (file->f_mode & 2))
 211                         return 0;
 212         }
 213         return 1;
 214 }
 215 
 216 static void write_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 217 {
 218         if (!inode->i_dirt)
 219                 return;
 220         wait_on_inode(inode);
 221         if (!inode->i_dirt)
 222                 return;
 223         if (!inode->i_sb || !inode->i_sb->s_op || !inode->i_sb->s_op->write_inode) {
 224                 inode->i_dirt = 0;
 225                 return;
 226         }
 227         inode->i_lock = 1;      
 228         inode->i_sb->s_op->write_inode(inode);
 229         unlock_inode(inode);
 230 }
 231 
 232 static inline void read_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234         lock_inode(inode);
 235         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->read_inode)
 236                 inode->i_sb->s_op->read_inode(inode);
 237         unlock_inode(inode);
 238 }
 239 
 240 /* POSIX UID/GID verification for setting inode attributes */
 241 int inode_change_ok(struct inode *inode, struct iattr *attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 242 {
 243         /* Make sure a caller can chown */
 244         if ((attr->ia_valid & ATTR_UID) &&
 245             (current->fsuid != inode->i_uid ||
 246              attr->ia_uid != inode->i_uid) && !fsuser())
 247                 return -EPERM;
 248 
 249         /* Make sure caller can chgrp */
 250         if ((attr->ia_valid & ATTR_GID) &&
 251             (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid) &&
 252             !fsuser())
 253                 return -EPERM;
 254 
 255         /* Make sure a caller can chmod */
 256         if (attr->ia_valid & ATTR_MODE) {
 257                 if ((current->fsuid != inode->i_uid) && !fsuser())
 258                         return -EPERM;
 259                 /* Also check the setgid bit! */
 260                 if (!fsuser() && !in_group_p((attr->ia_valid & ATTR_GID) ? attr->ia_gid :
 261                                              inode->i_gid))
 262                         attr->ia_mode &= ~S_ISGID;
 263         }
 264 
 265         /* Check for setting the inode time */
 266         if ((attr->ia_valid & ATTR_ATIME_SET) &&
 267             ((current->fsuid != inode->i_uid) && !fsuser()))
 268                 return -EPERM;
 269         if ((attr->ia_valid & ATTR_MTIME_SET) &&
 270             ((current->fsuid != inode->i_uid) && !fsuser()))
 271                 return -EPERM;
 272 
 273 
 274         return 0;
 275 }
 276 
 277 /*
 278  * Set the appropriate attributes from an attribute structure into
 279  * the inode structure.
 280  */
 281 void inode_setattr(struct inode *inode, struct iattr *attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283         if (attr->ia_valid & ATTR_UID)
 284                 inode->i_uid = attr->ia_uid;
 285         if (attr->ia_valid & ATTR_GID)
 286                 inode->i_gid = attr->ia_gid;
 287         if (attr->ia_valid & ATTR_SIZE)
 288                 inode->i_size = attr->ia_size;
 289         if (attr->ia_valid & ATTR_ATIME)
 290                 inode->i_atime = attr->ia_atime;
 291         if (attr->ia_valid & ATTR_MTIME)
 292                 inode->i_mtime = attr->ia_mtime;
 293         if (attr->ia_valid & ATTR_CTIME)
 294                 inode->i_ctime = attr->ia_ctime;
 295         if (attr->ia_valid & ATTR_MODE) {
 296                 inode->i_mode = attr->ia_mode;
 297                 if (!fsuser() && !in_group_p(inode->i_gid))
 298                         inode->i_mode &= ~S_ISGID;
 299         }
 300         inode->i_dirt = 1;
 301 }
 302 
 303 /*
 304  * notify_change is called for inode-changing operations such as
 305  * chown, chmod, utime, and truncate.  It is guaranteed (unlike
 306  * write_inode) to be called from the context of the user requesting
 307  * the change.  It is not called for ordinary access-time updates.
 308  * NFS uses this to get the authentication correct.  -- jrs
 309  */
 310 
 311 int notify_change(struct inode * inode, struct iattr *attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313         int retval;
 314 
 315         if (inode->i_sb && inode->i_sb->s_op  &&
 316             inode->i_sb->s_op->notify_change) 
 317                 return inode->i_sb->s_op->notify_change(inode, attr);
 318 
 319         if ((retval = inode_change_ok(inode, attr)) != 0)
 320                 return retval;
 321 
 322         inode_setattr(inode, attr);
 323         return 0;
 324 }
 325 
 326 /*
 327  * bmap is needed for demand-loading and paging: if this function
 328  * doesn't exist for a filesystem, then those things are impossible:
 329  * executables cannot be run from the filesystem etc...
 330  *
 331  * This isn't as bad as it sounds: the read-routines might still work,
 332  * so the filesystem would be otherwise ok (for example, you might have
 333  * a DOS filesystem, which doesn't lend itself to bmap very well, but
 334  * you could still transfer files to/from the filesystem)
 335  */
 336 int bmap(struct inode * inode, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         if (inode->i_op && inode->i_op->bmap)
 339                 return inode->i_op->bmap(inode,block);
 340         return 0;
 341 }
 342 
 343 void invalidate_inodes(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 344 {
 345         struct inode * inode, * next;
 346         int i;
 347 
 348         next = first_inode;
 349         for(i = nr_inodes ; i > 0 ; i--) {
 350                 inode = next;
 351                 next = inode->i_next;           /* clear_inode() changes the queues.. */
 352                 if (inode->i_dev != dev)
 353                         continue;
 354                 if (inode->i_count || inode->i_dirt || inode->i_lock) {
 355                         printk("VFS: inode busy on removed device %s\n",
 356                                kdevname(dev));
 357                         continue;
 358                 }
 359                 clear_inode(inode);
 360         }
 361 }
 362 
 363 void sync_inodes(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 364 {
 365         int i;
 366         struct inode * inode;
 367 
 368         inode = first_inode;
 369         for(i = 0; i < nr_inodes*2; i++, inode = inode->i_next) {
 370                 if (dev && inode->i_dev != dev)
 371                         continue;
 372                 wait_on_inode(inode);
 373                 if (inode->i_dirt)
 374                         write_inode(inode);
 375         }
 376 }
 377 
 378 void iput(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 379 {
 380         if (!inode)
 381                 return;
 382         wait_on_inode(inode);
 383         if (!inode->i_count) {
 384                 printk("VFS: iput: trying to free free inode\n");
 385                 printk("VFS: device %s, inode %lu, mode=0%07o\n",
 386                         kdevname(inode->i_rdev), inode->i_ino, inode->i_mode);
 387                 return;
 388         }
 389         if (inode->i_pipe)
 390                 wake_up_interruptible(&PIPE_WAIT(*inode));
 391 repeat:
 392         if (inode->i_count>1) {
 393                 inode->i_count--;
 394                 return;
 395         }
 396 
 397         wake_up(&inode_wait);
 398         if (inode->i_pipe) {
 399                 unsigned long page = (unsigned long) PIPE_BASE(*inode);
 400                 PIPE_BASE(*inode) = NULL;
 401                 free_page(page);
 402         }
 403 
 404         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) {
 405                 inode->i_sb->s_op->put_inode(inode);
 406                 if (!inode->i_nlink)
 407                         return;
 408         }
 409 
 410         if (inode->i_dirt) {
 411                 write_inode(inode);     /* we can sleep - so do again */
 412                 wait_on_inode(inode);
 413                 goto repeat;
 414         }
 415 
 416         inode->i_count--;
 417         if (IS_WRITABLE(inode)) {
 418                 if (inode->i_sb && inode->i_sb->dq_op)
 419                         inode->i_sb->dq_op->drop(inode);
 420         }
 421 
 422         if (inode->i_mmap) {
 423                 printk("iput: inode %lu on device %s still has mappings.\n",
 424                         inode->i_ino, kdevname(inode->i_dev));
 425                 inode->i_mmap = NULL;
 426         }
 427 
 428         nr_free_inodes++;
 429         return;
 430 }
 431 
 432 struct inode * get_empty_inode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 433 {
 434         static int ino = 0;
 435         struct inode * inode, * best;
 436         int i;
 437 
 438         if (nr_inodes < NR_INODE && nr_free_inodes < (nr_inodes >> 2))
 439                 grow_inodes();
 440 repeat:
 441         inode = first_inode;
 442         best = NULL;
 443         for (i = 0; i<nr_inodes; inode = inode->i_next, i++) {
 444                 if (!inode->i_count) {
 445                         if (!best)
 446                                 best = inode;
 447                         if (!inode->i_dirt && !inode->i_lock) {
 448                                 best = inode;
 449                                 break;
 450                         }
 451                 }
 452         }
 453         if (!best || best->i_dirt || best->i_lock)
 454                 if (nr_inodes < NR_INODE) {
 455                         grow_inodes();
 456                         goto repeat;
 457                 }
 458         inode = best;
 459         if (!inode) {
 460                 printk("VFS: No free inodes - contact Linus\n");
 461                 sleep_on(&inode_wait);
 462                 goto repeat;
 463         }
 464         if (inode->i_lock) {
 465                 wait_on_inode(inode);
 466                 goto repeat;
 467         }
 468         if (inode->i_dirt) {
 469                 write_inode(inode);
 470                 goto repeat;
 471         }
 472         if (inode->i_count)
 473                 goto repeat;
 474         clear_inode(inode);
 475         inode->i_count = 1;
 476         inode->i_nlink = 1;
 477         inode->i_version = ++event;
 478         inode->i_sem.count = 1;
 479         inode->i_ino = ++ino;
 480         inode->i_dev = 0;
 481         nr_free_inodes--;
 482         if (nr_free_inodes < 0) {
 483                 printk ("VFS: get_empty_inode: bad free inode count.\n");
 484                 nr_free_inodes = 0;
 485         }
 486         return inode;
 487 }
 488 
 489 struct inode * get_pipe_inode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 490 {
 491         struct inode * inode;
 492         extern struct inode_operations pipe_inode_operations;
 493 
 494         if (!(inode = get_empty_inode()))
 495                 return NULL;
 496         if (!(PIPE_BASE(*inode) = (char*) __get_free_page(GFP_USER))) {
 497                 iput(inode);
 498                 return NULL;
 499         }
 500         inode->i_op = &pipe_inode_operations;
 501         inode->i_count = 2;     /* sum of readers/writers */
 502         PIPE_WAIT(*inode) = NULL;
 503         PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
 504         PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
 505         PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
 506         PIPE_LOCK(*inode) = 0;
 507         inode->i_pipe = 1;
 508         inode->i_mode |= S_IFIFO | S_IRUSR | S_IWUSR;
 509         inode->i_uid = current->fsuid;
 510         inode->i_gid = current->fsgid;
 511         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 512         inode->i_blksize = PAGE_SIZE;
 513         return inode;
 514 }
 515 
 516 struct inode *__iget(struct super_block * sb, int nr, int crossmntp)
     /* [previous][next][first][last][top][bottom][index][help] */
 517 {
 518         static struct wait_queue * update_wait = NULL;
 519         struct inode_hash_entry * h;
 520         struct inode * inode;
 521         struct inode * empty = NULL;
 522 
 523         if (!sb)
 524                 panic("VFS: iget with sb==NULL");
 525         h = hash(sb->s_dev, nr);
 526 repeat:
 527         for (inode = h->inode; inode ; inode = inode->i_hash_next)
 528                 if (inode->i_dev == sb->s_dev && inode->i_ino == nr)
 529                         goto found_it;
 530         if (!empty) {
 531                 h->updating++;
 532                 empty = get_empty_inode();
 533                 if (!--h->updating)
 534                         wake_up(&update_wait);
 535                 if (empty)
 536                         goto repeat;
 537                 return (NULL);
 538         }
 539         inode = empty;
 540         inode->i_sb = sb;
 541         inode->i_dev = sb->s_dev;
 542         inode->i_ino = nr;
 543         inode->i_flags = sb->s_flags;
 544         put_last_free(inode);
 545         insert_inode_hash(inode);
 546         read_inode(inode);
 547         goto return_it;
 548 
 549 found_it:
 550         if (!inode->i_count)
 551                 nr_free_inodes--;
 552         inode->i_count++;
 553         wait_on_inode(inode);
 554         if (inode->i_dev != sb->s_dev || inode->i_ino != nr) {
 555                 printk("Whee.. inode changed from under us. Tell Linus\n");
 556                 iput(inode);
 557                 goto repeat;
 558         }
 559         if (crossmntp && inode->i_mount) {
 560                 struct inode * tmp = inode->i_mount;
 561                 tmp->i_count++;
 562                 iput(inode);
 563                 inode = tmp;
 564                 wait_on_inode(inode);
 565         }
 566         if (empty)
 567                 iput(empty);
 568 
 569 return_it:
 570         while (h->updating)
 571                 sleep_on(&update_wait);
 572         return inode;
 573 }
 574 
 575 /*
 576  * The "new" scheduling primitives (new as of 0.97 or so) allow this to
 577  * be done without disabling interrupts (other than in the actual queue
 578  * updating things: only a couple of 386 instructions). This should be
 579  * much better for interrupt latency.
 580  */
 581 static void __wait_on_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 582 {
 583         struct wait_queue wait = { current, NULL };
 584 
 585         add_wait_queue(&inode->i_wait, &wait);
 586 repeat:
 587         current->state = TASK_UNINTERRUPTIBLE;
 588         if (inode->i_lock) {
 589                 schedule();
 590                 goto repeat;
 591         }
 592         remove_wait_queue(&inode->i_wait, &wait);
 593         current->state = TASK_RUNNING;
 594 }

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