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

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