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         invalidate_inode_pages(inode, 0);
 152         wait_on_inode(inode);
 153         if (IS_WRITABLE(inode)) {
 154                 if (inode->i_sb && inode->i_sb->dq_op)
 155                         inode->i_sb->dq_op->drop(inode);
 156         }
 157         remove_inode_hash(inode);
 158         remove_inode_free(inode);
 159         wait = ((volatile struct inode *) inode)->i_wait;
 160         if (inode->i_count)
 161                 nr_free_inodes++;
 162         memset(inode,0,sizeof(*inode));
 163         ((volatile struct inode *) inode)->i_wait = wait;
 164         insert_inode_free(inode);
 165 }
 166 
 167 int fs_may_mount(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 {
 169         struct inode * inode, * next;
 170         int i;
 171 
 172         next = first_inode;
 173         for (i = nr_inodes ; i > 0 ; i--) {
 174                 inode = next;
 175                 next = inode->i_next;   /* clear_inode() changes the queues.. */
 176                 if (inode->i_dev != dev)
 177                         continue;
 178                 if (inode->i_count || inode->i_dirt || inode->i_lock)
 179                         return 0;
 180                 clear_inode(inode);
 181         }
 182         return 1;
 183 }
 184 
 185 int fs_may_umount(kdev_t dev, struct inode * mount_root)
     /* [previous][next][first][last][top][bottom][index][help] */
 186 {
 187         struct inode * inode;
 188         int i;
 189 
 190         inode = first_inode;
 191         for (i=0 ; i < nr_inodes ; i++, inode = inode->i_next) {
 192                 if (inode->i_dev != dev || !inode->i_count)
 193                         continue;
 194                 if (inode == mount_root && inode->i_count == 1)
 195                         continue;
 196                 return 0;
 197         }
 198         return 1;
 199 }
 200 
 201 int fs_may_remount_ro(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203         struct file * file;
 204         int i;
 205 
 206         /* Check that no files are currently opened for writing. */
 207         for (file = first_file, i=0; i<nr_files; i++, file=file->f_next) {
 208                 if (!file->f_count || !file->f_inode ||
 209                     file->f_inode->i_dev != dev)
 210                         continue;
 211                 if (S_ISREG(file->f_inode->i_mode) && (file->f_mode & 2))
 212                         return 0;
 213         }
 214         return 1;
 215 }
 216 
 217 static void write_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219         if (!inode->i_dirt)
 220                 return;
 221         wait_on_inode(inode);
 222         if (!inode->i_dirt)
 223                 return;
 224         if (!inode->i_sb || !inode->i_sb->s_op || !inode->i_sb->s_op->write_inode) {
 225                 inode->i_dirt = 0;
 226                 return;
 227         }
 228         inode->i_lock = 1;      
 229         inode->i_sb->s_op->write_inode(inode);
 230         unlock_inode(inode);
 231 }
 232 
 233 static inline void read_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235         lock_inode(inode);
 236         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->read_inode)
 237                 inode->i_sb->s_op->read_inode(inode);
 238         unlock_inode(inode);
 239 }
 240 
 241 /* POSIX UID/GID verification for setting inode attributes */
 242 int inode_change_ok(struct inode *inode, struct iattr *attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 243 {
 244         /* Make sure a caller can chown */
 245         if ((attr->ia_valid & ATTR_UID) &&
 246             (current->fsuid != inode->i_uid ||
 247              attr->ia_uid != inode->i_uid) && !fsuser())
 248                 return -EPERM;
 249 
 250         /* Make sure caller can chgrp */
 251         if ((attr->ia_valid & ATTR_GID) &&
 252             (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid) &&
 253             !fsuser())
 254                 return -EPERM;
 255 
 256         /* Make sure a caller can chmod */
 257         if (attr->ia_valid & ATTR_MODE) {
 258                 if ((current->fsuid != inode->i_uid) && !fsuser())
 259                         return -EPERM;
 260                 /* Also check the setgid bit! */
 261                 if (!fsuser() && !in_group_p((attr->ia_valid & ATTR_GID) ? attr->ia_gid :
 262                                              inode->i_gid))
 263                         attr->ia_mode &= ~S_ISGID;
 264         }
 265 
 266         /* Check for setting the inode time */
 267         if ((attr->ia_valid & ATTR_ATIME_SET) &&
 268             ((current->fsuid != inode->i_uid) && !fsuser()))
 269                 return -EPERM;
 270         if ((attr->ia_valid & ATTR_MTIME_SET) &&
 271             ((current->fsuid != inode->i_uid) && !fsuser()))
 272                 return -EPERM;
 273 
 274 
 275         return 0;
 276 }
 277 
 278 /*
 279  * Set the appropriate attributes from an attribute structure into
 280  * the inode structure.
 281  */
 282 void inode_setattr(struct inode *inode, struct iattr *attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 283 {
 284         if (attr->ia_valid & ATTR_UID)
 285                 inode->i_uid = attr->ia_uid;
 286         if (attr->ia_valid & ATTR_GID)
 287                 inode->i_gid = attr->ia_gid;
 288         if (attr->ia_valid & ATTR_SIZE)
 289                 inode->i_size = attr->ia_size;
 290         if (attr->ia_valid & ATTR_ATIME)
 291                 inode->i_atime = attr->ia_atime;
 292         if (attr->ia_valid & ATTR_MTIME)
 293                 inode->i_mtime = attr->ia_mtime;
 294         if (attr->ia_valid & ATTR_CTIME)
 295                 inode->i_ctime = attr->ia_ctime;
 296         if (attr->ia_valid & ATTR_MODE) {
 297                 inode->i_mode = attr->ia_mode;
 298                 if (!fsuser() && !in_group_p(inode->i_gid))
 299                         inode->i_mode &= ~S_ISGID;
 300         }
 301         inode->i_dirt = 1;
 302 }
 303 
 304 /*
 305  * notify_change is called for inode-changing operations such as
 306  * chown, chmod, utime, and truncate.  It is guaranteed (unlike
 307  * write_inode) to be called from the context of the user requesting
 308  * the change.  It is not called for ordinary access-time updates.
 309  * NFS uses this to get the authentication correct.  -- jrs
 310  */
 311 
 312 int notify_change(struct inode * inode, struct iattr *attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 313 {
 314         int retval;
 315 
 316         if (inode->i_sb && inode->i_sb->s_op  &&
 317             inode->i_sb->s_op->notify_change) 
 318                 return inode->i_sb->s_op->notify_change(inode, attr);
 319 
 320         if ((retval = inode_change_ok(inode, attr)) != 0)
 321                 return retval;
 322 
 323         inode_setattr(inode, attr);
 324         return 0;
 325 }
 326 
 327 /*
 328  * bmap is needed for demand-loading and paging: if this function
 329  * doesn't exist for a filesystem, then those things are impossible:
 330  * executables cannot be run from the filesystem etc...
 331  *
 332  * This isn't as bad as it sounds: the read-routines might still work,
 333  * so the filesystem would be otherwise ok (for example, you might have
 334  * a DOS filesystem, which doesn't lend itself to bmap very well, but
 335  * you could still transfer files to/from the filesystem)
 336  */
 337 int bmap(struct inode * inode, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 338 {
 339         if (inode->i_op && inode->i_op->bmap)
 340                 return inode->i_op->bmap(inode,block);
 341         return 0;
 342 }
 343 
 344 void invalidate_inodes(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346         struct inode * inode, * next;
 347         int i;
 348 
 349         next = first_inode;
 350         for(i = nr_inodes ; i > 0 ; i--) {
 351                 inode = next;
 352                 next = inode->i_next;           /* clear_inode() changes the queues.. */
 353                 if (inode->i_dev != dev)
 354                         continue;
 355                 if (inode->i_count || inode->i_dirt || inode->i_lock) {
 356                         printk("VFS: inode busy on removed device %s\n",
 357                                kdevname(dev));
 358                         continue;
 359                 }
 360                 clear_inode(inode);
 361         }
 362 }
 363 
 364 void sync_inodes(kdev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 365 {
 366         int i;
 367         struct inode * inode;
 368 
 369         inode = first_inode;
 370         for(i = 0; i < nr_inodes*2; i++, inode = inode->i_next) {
 371                 if (dev && inode->i_dev != dev)
 372                         continue;
 373                 wait_on_inode(inode);
 374                 if (inode->i_dirt)
 375                         write_inode(inode);
 376         }
 377 }
 378 
 379 void iput(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 380 {
 381         if (!inode)
 382                 return;
 383         wait_on_inode(inode);
 384         if (!inode->i_count) {
 385                 printk("VFS: iput: trying to free free inode\n");
 386                 printk("VFS: device %s, inode %lu, mode=0%07o\n",
 387                         kdevname(inode->i_rdev), inode->i_ino, inode->i_mode);
 388                 return;
 389         }
 390         if (inode->i_pipe)
 391                 wake_up_interruptible(&PIPE_WAIT(*inode));
 392 repeat:
 393         if (inode->i_count>1) {
 394                 inode->i_count--;
 395                 return;
 396         }
 397 
 398         wake_up(&inode_wait);
 399         if (inode->i_pipe) {
 400                 unsigned long page = (unsigned long) PIPE_BASE(*inode);
 401                 PIPE_BASE(*inode) = NULL;
 402                 free_page(page);
 403         }
 404 
 405         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) {
 406                 inode->i_sb->s_op->put_inode(inode);
 407                 if (!inode->i_nlink)
 408                         return;
 409         }
 410 
 411         if (inode->i_dirt) {
 412                 write_inode(inode);     /* we can sleep - so do again */
 413                 wait_on_inode(inode);
 414                 goto repeat;
 415         }
 416 
 417         inode->i_count--;
 418         if (IS_WRITABLE(inode)) {
 419                 if (inode->i_sb && inode->i_sb->dq_op)
 420                         inode->i_sb->dq_op->drop(inode);
 421         }
 422 
 423         if (inode->i_mmap) {
 424                 printk("iput: inode %lu on device %s still has mappings.\n",
 425                         inode->i_ino, kdevname(inode->i_dev));
 426                 inode->i_mmap = NULL;
 427         }
 428 
 429         nr_free_inodes++;
 430         return;
 431 }
 432 
 433 struct inode * get_empty_inode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 434 {
 435         static int ino = 0;
 436         struct inode * inode, * best;
 437         int i;
 438 
 439         if (nr_inodes < NR_INODE && nr_free_inodes < (nr_inodes >> 2))
 440                 grow_inodes();
 441 repeat:
 442         inode = first_inode;
 443         best = NULL;
 444         for (i = 0; i<nr_inodes; inode = inode->i_next, i++) {
 445                 if (!inode->i_count) {
 446                         if (!best)
 447                                 best = inode;
 448                         if (!inode->i_dirt && !inode->i_lock) {
 449                                 best = inode;
 450                                 break;
 451                         }
 452                 }
 453         }
 454         if (!best || best->i_dirt || best->i_lock)
 455                 if (nr_inodes < NR_INODE) {
 456                         grow_inodes();
 457                         goto repeat;
 458                 }
 459         inode = best;
 460         if (!inode) {
 461                 printk("VFS: No free inodes - contact Linus\n");
 462                 sleep_on(&inode_wait);
 463                 goto repeat;
 464         }
 465         if (inode->i_lock) {
 466                 wait_on_inode(inode);
 467                 goto repeat;
 468         }
 469         if (inode->i_dirt) {
 470                 write_inode(inode);
 471                 goto repeat;
 472         }
 473         if (inode->i_count)
 474                 goto repeat;
 475         clear_inode(inode);
 476         inode->i_count = 1;
 477         inode->i_nlink = 1;
 478         inode->i_version = ++event;
 479         inode->i_sem.count = 1;
 480         inode->i_ino = ++ino;
 481         inode->i_dev = 0;
 482         nr_free_inodes--;
 483         if (nr_free_inodes < 0) {
 484                 printk ("VFS: get_empty_inode: bad free inode count.\n");
 485                 nr_free_inodes = 0;
 486         }
 487         return inode;
 488 }
 489 
 490 struct inode * get_pipe_inode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 491 {
 492         struct inode * inode;
 493         extern struct inode_operations pipe_inode_operations;
 494 
 495         if (!(inode = get_empty_inode()))
 496                 return NULL;
 497         if (!(PIPE_BASE(*inode) = (char*) __get_free_page(GFP_USER))) {
 498                 iput(inode);
 499                 return NULL;
 500         }
 501         inode->i_op = &pipe_inode_operations;
 502         inode->i_count = 2;     /* sum of readers/writers */
 503         PIPE_WAIT(*inode) = NULL;
 504         PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
 505         PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
 506         PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
 507         PIPE_LOCK(*inode) = 0;
 508         inode->i_pipe = 1;
 509         inode->i_mode |= S_IFIFO | S_IRUSR | S_IWUSR;
 510         inode->i_uid = current->fsuid;
 511         inode->i_gid = current->fsgid;
 512         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 513         inode->i_blksize = PAGE_SIZE;
 514         return inode;
 515 }
 516 
 517 struct inode *__iget(struct super_block * sb, int nr, int crossmntp)
     /* [previous][next][first][last][top][bottom][index][help] */
 518 {
 519         static struct wait_queue * update_wait = NULL;
 520         struct inode_hash_entry * h;
 521         struct inode * inode;
 522         struct inode * empty = NULL;
 523 
 524         if (!sb)
 525                 panic("VFS: iget with sb==NULL");
 526         h = hash(sb->s_dev, nr);
 527 repeat:
 528         for (inode = h->inode; inode ; inode = inode->i_hash_next)
 529                 if (inode->i_dev == sb->s_dev && inode->i_ino == nr)
 530                         goto found_it;
 531         if (!empty) {
 532                 h->updating++;
 533                 empty = get_empty_inode();
 534                 if (!--h->updating)
 535                         wake_up(&update_wait);
 536                 if (empty)
 537                         goto repeat;
 538                 return (NULL);
 539         }
 540         inode = empty;
 541         inode->i_sb = sb;
 542         inode->i_dev = sb->s_dev;
 543         inode->i_ino = nr;
 544         inode->i_flags = sb->s_flags;
 545         put_last_free(inode);
 546         insert_inode_hash(inode);
 547         read_inode(inode);
 548         goto return_it;
 549 
 550 found_it:
 551         if (!inode->i_count)
 552                 nr_free_inodes--;
 553         inode->i_count++;
 554         wait_on_inode(inode);
 555         if (inode->i_dev != sb->s_dev || inode->i_ino != nr) {
 556                 printk("Whee.. inode changed from under us. Tell Linus\n");
 557                 iput(inode);
 558                 goto repeat;
 559         }
 560         if (crossmntp && inode->i_mount) {
 561                 struct inode * tmp = inode->i_mount;
 562                 tmp->i_count++;
 563                 iput(inode);
 564                 inode = tmp;
 565                 wait_on_inode(inode);
 566         }
 567         if (empty)
 568                 iput(empty);
 569 
 570 return_it:
 571         while (h->updating)
 572                 sleep_on(&update_wait);
 573         return inode;
 574 }
 575 
 576 /*
 577  * The "new" scheduling primitives (new as of 0.97 or so) allow this to
 578  * be done without disabling interrupts (other than in the actual queue
 579  * updating things: only a couple of 386 instructions). This should be
 580  * much better for interrupt latency.
 581  */
 582 static void __wait_on_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 583 {
 584         struct wait_queue wait = { current, NULL };
 585 
 586         add_wait_queue(&inode->i_wait, &wait);
 587 repeat:
 588         current->state = TASK_UNINTERRUPTIBLE;
 589         if (inode->i_lock) {
 590                 schedule();
 591                 goto repeat;
 592         }
 593         remove_wait_queue(&inode->i_wait, &wait);
 594         current->state = TASK_RUNNING;
 595 }

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