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

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