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

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