root/fs/inode.c

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

DEFINITIONS

This source file includes following definitions.
  1. hashfn
  2. hash
  3. insert_inode_free
  4. remove_inode_free
  5. insert_inode_hash
  6. remove_inode_hash
  7. put_last_free
  8. grow_inodes
  9. inode_init
  10. wait_on_inode
  11. lock_inode
  12. unlock_inode
  13. clear_inode
  14. fs_may_mount
  15. fs_may_umount
  16. fs_may_remount_ro
  17. write_inode
  18. read_inode
  19. notify_change
  20. bmap
  21. invalidate_inodes
  22. sync_inodes
  23. iput
  24. get_empty_inode
  25. get_pipe_inode
  26. iget
  27. __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_table[NR_IHASH];
  16 static struct inode * first_inode;
  17 static struct wait_queue * inode_wait = NULL;
  18 static int nr_inodes = 0, nr_free_inodes = 0;
  19 
  20 static inline int const hashfn(dev_t dev, int i)
     /* [previous][next][first][last][top][bottom][index][help] */
  21 {
  22         return (dev ^ i) % NR_IHASH;
  23 }
  24 
  25 static inline struct inode ** const hash(dev_t dev, int i)
     /* [previous][next][first][last][top][bottom][index][help] */
  26 {
  27         return hash_table + hashfn(dev, i);
  28 }
  29 
  30 static void insert_inode_free(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  31 {
  32         inode->i_next = first_inode;
  33         inode->i_prev = first_inode->i_prev;
  34         inode->i_next->i_prev = inode;
  35         inode->i_prev->i_next = inode;
  36         first_inode = inode;
  37 }
  38 
  39 static void remove_inode_free(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41         if (first_inode == inode)
  42                 first_inode = first_inode->i_next;
  43         if (inode->i_next)
  44                 inode->i_next->i_prev = inode->i_prev;
  45         if (inode->i_prev)
  46                 inode->i_prev->i_next = inode->i_next;
  47         inode->i_next = inode->i_prev = NULL;
  48 }
  49 
  50 void insert_inode_hash(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52         struct inode **h;
  53         h = hash(inode->i_dev, inode->i_ino);
  54 
  55         inode->i_hash_next = *h;
  56         inode->i_hash_prev = NULL;
  57         if (inode->i_hash_next)
  58                 inode->i_hash_next->i_hash_prev = inode;
  59         *h = inode;
  60 }
  61 
  62 static void remove_inode_hash(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  63 {
  64         struct inode **h;
  65         h = hash(inode->i_dev, inode->i_ino);
  66 
  67         if (*h == inode)
  68                 *h = inode->i_hash_next;
  69         if (inode->i_hash_next)
  70                 inode->i_hash_next->i_hash_prev = inode->i_hash_prev;
  71         if (inode->i_hash_prev)
  72                 inode->i_hash_prev->i_hash_next = inode->i_hash_next;
  73         inode->i_hash_prev = inode->i_hash_next = NULL;
  74 }
  75 
  76 static void put_last_free(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  77 {
  78         remove_inode_free(inode);
  79         inode->i_prev = first_inode->i_prev;
  80         inode->i_prev->i_next = inode;
  81         inode->i_next = first_inode;
  82         inode->i_next->i_prev = inode;
  83 }
  84 
  85 void grow_inodes(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87         unsigned long page;
  88         struct inode * inode;
  89         int i;
  90 
  91         page = get_free_page(GFP_KERNEL);
  92         if (!page)
  93                 return;
  94         inode = (struct inode *) page;
  95         for (i=0; i < (PAGE_SIZE / sizeof(struct inode)); i++, inode++)
  96         {
  97                 if (!first_inode)
  98                 {
  99                         inode->i_next = inode;
 100                         inode->i_prev = inode;
 101                         first_inode = inode;
 102                 }
 103                 else
 104                         insert_inode_free(inode);
 105         }
 106         nr_inodes += i;
 107         nr_free_inodes += i;
 108 }
 109 
 110 unsigned long inode_init(unsigned long start, unsigned long end)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112         memset(hash_table, 0, sizeof(hash_table));
 113         first_inode = NULL;
 114         return start;
 115 }
 116 
 117 static void __wait_on_inode(struct inode *);
 118 
 119 static inline void wait_on_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         if (inode->i_lock)
 122                 __wait_on_inode(inode);
 123 }
 124 
 125 static inline void lock_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 126 {
 127         wait_on_inode(inode);
 128         inode->i_lock = 1;
 129 }
 130 
 131 static inline void unlock_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133         inode->i_lock = 0;
 134         wake_up(&inode->i_wait);
 135 }
 136 
 137 /*
 138  * Note that we don't want to disturb any wait-queues when we discard
 139  * an inode.
 140  *
 141  * Argghh. Got bitten by a gcc problem with inlining: no way to tell
 142  * the compiler that the inline asm function 'memset' changes 'inode'.
 143  * I've been searching for the bug for days, and was getting desperate.
 144  * Finally looked at the assembler output... Grrr.
 145  *
 146  * The solution is the weird use of 'volatile'. Ho humm. Have to report
 147  * it to the gcc lists, and hope we can do this more cleanly some day..
 148  */
 149 void clear_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151         struct wait_queue * wait;
 152 
 153         wait_on_inode(inode);
 154         wait = ((volatile struct inode *) inode)->i_wait;
 155         remove_inode_hash(inode);
 156         remove_inode_free(inode);
 157         if (inode->i_count)
 158                 nr_free_inodes++;
 159         memset(inode,0,sizeof(*inode));
 160         ((volatile struct inode *) inode)->i_wait = wait;
 161         insert_inode_free(inode);
 162 }
 163 
 164 int fs_may_mount(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 165 {
 166         struct inode * inode;
 167         int i;
 168 
 169         for (inode = first_inode, i=0; i<nr_inodes; i++, inode=inode->i_next) {
 170                 if (inode->i_dev != dev)
 171                         continue;
 172                 if (inode->i_count || inode->i_dirt || inode->i_lock)
 173                         return 0;
 174                 clear_inode(inode);
 175         }
 176         return 1;
 177 }
 178 
 179 int fs_may_umount(dev_t dev, struct inode * mount_root)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181         struct inode * inode;
 182         int i;
 183 
 184         for (inode = first_inode, i=0; i<nr_inodes; i++, inode=inode->i_next) {
 185                 if (inode->i_dev==dev && inode->i_count)
 186                         if (inode == mount_root && inode->i_count == 1)
 187                                 continue;
 188                         else
 189                                 return 0;
 190         }
 191         return 1;
 192 }
 193 
 194 int fs_may_remount_ro(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         struct file * file;
 197         int i;
 198 
 199         /* Check that no files are currently opened for writing. */
 200         for (file = first_file, i=0; i<nr_files; i++, file=file->f_next) {
 201                 if (!file->f_count || !file->f_inode ||
 202                     file->f_inode->i_dev != dev)
 203                         continue;
 204                 if (S_ISREG(file->f_inode->i_mode) && (file->f_mode & 2))
 205                         return 0;
 206         }
 207         return 1;
 208 }
 209 
 210 static void write_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 211 {
 212         if (!inode->i_dirt)
 213                 return;
 214         wait_on_inode(inode);
 215         if (!inode->i_dirt)
 216                 return;
 217         if (!inode->i_sb || !inode->i_sb->s_op || !inode->i_sb->s_op->write_inode) {
 218                 inode->i_dirt = 0;
 219                 return;
 220         }
 221         inode->i_lock = 1;      
 222         inode->i_sb->s_op->write_inode(inode);
 223         unlock_inode(inode);
 224 }
 225 
 226 static void read_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 227 {
 228         lock_inode(inode);
 229         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->read_inode)
 230                 inode->i_sb->s_op->read_inode(inode);
 231         unlock_inode(inode);
 232 }
 233 
 234 /*
 235  * notify_change is called for inode-changing operations such as
 236  * chown, chmod, utime, and truncate.  It is guaranteed (unlike
 237  * write_inode) to be called from the context of the user requesting
 238  * the change.  It is not called for ordinary access-time updates.
 239  * NFS uses this to get the authentication correct.  -- jrs
 240  */
 241 
 242 int notify_change(int flags, struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 243 {
 244         if (inode->i_sb && inode->i_sb->s_op  &&
 245             inode->i_sb->s_op->notify_change)
 246                 return inode->i_sb->s_op->notify_change(flags, inode);
 247         return 0;
 248 }
 249 
 250 /*
 251  * bmap is needed for demand-loading and paging: if this function
 252  * doesn't exist for a filesystem, then those things are impossible:
 253  * executables cannot be run from the filesystem etc...
 254  *
 255  * This isn't as bad as it sounds: the read-routines might still work,
 256  * so the filesystem would be otherwise ok (for example, you might have
 257  * a DOS filesystem, which doesn't lend itself to bmap very well, but
 258  * you could still transfer files to/from the filesystem)
 259  */
 260 int bmap(struct inode * inode, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262         if (inode->i_op && inode->i_op->bmap)
 263                 return inode->i_op->bmap(inode,block);
 264         return 0;
 265 }
 266 
 267 void invalidate_inodes(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 268 {
 269         int i;
 270         struct inode * inode;
 271 
 272         inode = first_inode;
 273         for(i = 0; i < nr_inodes*2; i++, inode = inode->i_next) {
 274                 wait_on_inode(inode);
 275                 if (inode->i_dev == dev) {
 276                         if (inode->i_count) {
 277                                 printk("VFS: inode busy on removed device %d/%d\n",
 278                                                         MAJOR(dev), MINOR(dev));
 279                                 continue;
 280                         }
 281                         clear_inode(inode);
 282                 }
 283         }
 284 }
 285 
 286 void sync_inodes(dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 287 {
 288         int i;
 289         struct inode * inode;
 290 
 291         inode = first_inode;
 292         for(i = 0; i < nr_inodes*2; i++, inode = inode->i_next) {
 293                 if (dev && inode->i_dev != dev)
 294                         continue;
 295                 wait_on_inode(inode);
 296                 if (inode->i_dirt)
 297                         write_inode(inode);
 298         }
 299 }
 300 
 301 void iput(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 302 {
 303         if (!inode)
 304                 return;
 305         wait_on_inode(inode);
 306         if (!inode->i_count) {
 307                 printk("VFS: iput: trying to free free inode\n");
 308                 printk("VFS: device %d/%d, inode %d, mode=0%07o\n",
 309                         MAJOR(inode->i_rdev), MINOR(inode->i_rdev),
 310                                         inode->i_ino, inode->i_mode);
 311                 return;
 312         }
 313         if (inode->i_pipe) {
 314                 wake_up(&PIPE_READ_WAIT(*inode));
 315                 wake_up(&PIPE_WRITE_WAIT(*inode));
 316         }
 317 repeat:
 318         if (inode->i_count>1) {
 319                 inode->i_count--;
 320                 return;
 321         }
 322         wake_up(&inode_wait);
 323         if (inode->i_pipe) {
 324                 unsigned long page = (unsigned long) PIPE_BASE(*inode);
 325                 PIPE_BASE(*inode) = NULL;
 326                 free_page(page);
 327         }
 328         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) {
 329                 inode->i_sb->s_op->put_inode(inode);
 330                 if (!inode->i_nlink)
 331                         return;
 332         }
 333         if (inode->i_dirt) {
 334                 write_inode(inode);     /* we can sleep - so do again */
 335                 wait_on_inode(inode);
 336                 goto repeat;
 337         }
 338         inode->i_count--;
 339         nr_free_inodes++;
 340         return;
 341 }
 342 
 343 struct inode * get_empty_inode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 344 {
 345         struct inode * inode, * best;
 346         int i;
 347 
 348         if (nr_inodes < NR_INODE && nr_free_inodes < (nr_inodes >> 2))
 349                 grow_inodes();
 350 repeat:
 351         inode = first_inode;
 352         best = NULL;
 353         for (i = 0; i<nr_inodes; inode = inode->i_next, i++) {
 354                 if (!inode->i_count) {
 355                         if (!best)
 356                                 best = inode;
 357                         if (!inode->i_dirt && !inode->i_lock) {
 358                                 best = inode;
 359                                 break;
 360                         }
 361                 }
 362         }
 363         if (!best || best->i_dirt || best->i_lock)
 364                 if (nr_inodes < NR_INODE) {
 365                         grow_inodes();
 366                         goto repeat;
 367                 }
 368         inode = best;
 369         if (!inode) {
 370                 printk("VFS: No free inodes - contact Linus\n");
 371                 sleep_on(&inode_wait);
 372                 goto repeat;
 373         }
 374         if (inode->i_lock) {
 375                 wait_on_inode(inode);
 376                 goto repeat;
 377         }
 378         if (inode->i_dirt) {
 379                 write_inode(inode);
 380                 goto repeat;
 381         }
 382         if (inode->i_count)
 383                 goto repeat;
 384         clear_inode(inode);
 385         inode->i_count = 1;
 386         inode->i_nlink = 1;
 387         nr_free_inodes--;
 388         if (nr_free_inodes < 0)
 389         {
 390                 printk ("VFS: get_empty_inode: bad free inode count.\n");
 391                 nr_free_inodes = 0;
 392         }
 393         return inode;
 394 }
 395 
 396 struct inode * get_pipe_inode(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 397 {
 398         struct inode * inode;
 399 
 400         if (!(inode = get_empty_inode()))
 401                 return NULL;
 402         if (!(PIPE_BASE(*inode) = (char *) get_free_page(GFP_USER))) {
 403                 inode->i_count = 0;
 404                 return NULL;
 405         }
 406         inode->i_count = 2;     /* sum of readers/writers */
 407         PIPE_READ_WAIT(*inode) = PIPE_WRITE_WAIT(*inode) = NULL;
 408         PIPE_HEAD(*inode) = PIPE_TAIL(*inode) = 0;
 409         PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
 410         PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
 411         inode->i_pipe = 1;
 412         inode->i_mode |= S_IFIFO;
 413         inode->i_uid = current->euid;
 414         inode->i_gid = current->egid;
 415         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 416         return inode;
 417 }
 418 
 419 struct inode * iget(struct super_block * sb,int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 420 {
 421         struct inode * inode, * empty;
 422 
 423         if (!sb)
 424                 panic("VFS: iget with sb==NULL");
 425 repeat:
 426         empty = get_empty_inode();
 427         inode = *(hash(sb->s_dev,nr));
 428         while (inode) {
 429                 if (inode->i_dev != sb->s_dev || inode->i_ino != nr) {
 430                         inode = inode->i_hash_next;
 431                         continue;
 432                 }
 433                 wait_on_inode(inode);
 434                 if (inode->i_dev != sb->s_dev || inode->i_ino != nr)
 435                         goto repeat;
 436                 if (!inode->i_count)
 437                         nr_free_inodes--;
 438                 inode->i_count++;
 439                 if (inode->i_mount) {
 440                         int i;
 441 
 442                         for (i = 0 ; i<NR_SUPER ; i++)
 443                                 if (super_block[i].s_covered==inode)
 444                                         break;
 445                         if (i >= NR_SUPER) {
 446                                 printk("VFS: Mounted inode hasn't got sb\n");
 447                                 if (empty)
 448                                         iput(empty);
 449                                 return inode;
 450                         }
 451                         iput(inode);
 452                         if (!(inode = super_block[i].s_mounted))
 453                                 printk("VFS: Mounted device %d/%d has no rootinode\n",
 454                                         MAJOR(inode->i_dev), MINOR(inode->i_dev));
 455                         else {
 456                                 if (!inode->i_count)
 457                                         nr_free_inodes--;
 458                                 inode->i_count++;
 459                                 wait_on_inode(inode);
 460                         }
 461                 }
 462                 if (empty)
 463                         iput(empty);
 464                 return inode;
 465         }
 466         if (!empty)
 467                 return (NULL);
 468         inode = empty;
 469         inode->i_sb = sb;
 470         inode->i_dev = sb->s_dev;
 471         inode->i_ino = nr;
 472         inode->i_flags = sb->s_flags;
 473         put_last_free(inode);
 474         insert_inode_hash(inode);
 475         read_inode(inode);
 476         return inode;
 477 }
 478 
 479 /*
 480  * The "new" scheduling primitives (new as of 0.97 or so) allow this to
 481  * be done without disabling interrupts (other than in the actual queue
 482  * updating things: only a couple of 386 instructions). This should be
 483  * much better for interrupt latency.
 484  */
 485 static void __wait_on_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 486 {
 487         struct wait_queue wait = { current, NULL };
 488 
 489         add_wait_queue(&inode->i_wait, &wait);
 490 repeat:
 491         current->state = TASK_UNINTERRUPTIBLE;
 492         if (inode->i_lock) {
 493                 schedule();
 494                 goto repeat;
 495         }
 496         remove_wait_queue(&inode->i_wait, &wait);
 497         current->state = TASK_RUNNING;
 498 }

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