root/fs/umsdos/inode.c

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

DEFINITIONS

This source file includes following definitions.
  1. UMSDOS_put_inode
  2. UMSDOS_put_super
  3. UMSDOS_statfs
  4. umsdos_real_lookup
  5. umsdos_setup_dir_inode
  6. umsdos_set_dirinfo
  7. umsdos_isinit
  8. umsdos_patch_inode
  9. umsdos_get_dirowner
  10. UMSDOS_read_inode
  11. UMSDOS_write_inode
  12. UMSDOS_notify_change
  13. UMSDOS_read_super

   1 /*
   2  *  linux/fs/umsdos/inode.c
   3  *
   4  *      Written 1993 by Jacques Gelinas 
   5  *      Inspired from linux/fs/msdos/... by Werner Almesberger
   6  *
   7  */
   8 
   9 #include <stdlib.h>
  10 #include <linux/fs.h>
  11 #include <linux/msdos_fs.h>
  12 #include <linux/kernel.h>
  13 #include <linux/sched.h>
  14 #include <linux/errno.h>
  15 #include <asm/segment.h>
  16 #include <linux/string.h>
  17 #include <linux/ctype.h>
  18 #include <linux/stat.h>
  19 #include <linux/umsdos_fs.h>
  20 
  21 struct inode *pseudo_root=NULL;         /* Useful to simulate the pseudo DOS */
  22                                                                         /* directory. See UMSDOS_readdir_x() */
  23 
  24 /* #Specification: convention / PRINTK Printk and printk
  25         Here is the convention for the use of printk inside fs/umsdos
  26 
  27         printk carry important message (error or status).
  28         Printk is for debugging (it is a macro defined at the beginning of
  29                    most source.
  30         PRINTK is a nulled Printk macro.
  31 
  32         This convention makes the source easier to read, and Printk easier
  33         to shut off.
  34 */
  35 #define PRINTK(x)
  36 #define Printk(x) printk x
  37 
  38 
  39 void UMSDOS_put_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41         PRINTK (("put inode %x owner %x pos %d dir %x\n",inode
  42                 ,inode->u.umsdos_i.i_emd_owner,inode->u.umsdos_i.pos
  43                 ,inode->u.umsdos_i.i_emd_dir));
  44         msdos_put_inode(inode);
  45 }
  46 
  47 
  48 void UMSDOS_put_super(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50         msdos_put_super(sb);
  51 }
  52 
  53 
  54 void UMSDOS_statfs(struct super_block *sb,struct statfs *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56         msdos_statfs(sb,buf);
  57 }
  58 
  59 
  60 /*
  61         Call msdos_lookup, but set back the original msdos function table.
  62         Return 0 if ok, or a negative error code if not.
  63 */
  64 int umsdos_real_lookup (
     /* [previous][next][first][last][top][bottom][index][help] */
  65         struct inode *dir,
  66         const char *name,
  67         int len,
  68         struct inode **result)  /* Will hold inode of the file, if successful */
  69 {
  70         int ret;
  71         dir->i_count++;
  72         ret = msdos_lookup (dir,name,len,result);
  73         return ret;
  74 }
  75 /*
  76         Complete the setup of an directory inode.
  77         First, it completes the function pointers, then
  78         it locates the EMD file. If the EMD is there, then plug the
  79         umsdos function table. If not, use the msdos one.
  80 */
  81 void umsdos_setup_dir_inode (struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  82 {
  83         inode->u.umsdos_i.i_emd_dir = 0;
  84         {
  85                 struct inode *emd_dir = umsdos_emd_dir_lookup (inode,0);
  86                 extern struct inode_operations umsdos_rdir_inode_operations;
  87                 inode->i_op = emd_dir != NULL
  88                         ? &umsdos_dir_inode_operations
  89                         : &umsdos_rdir_inode_operations;
  90                 iput (emd_dir);
  91         }
  92 }
  93 /*
  94         Add some info into an inode so it can find its owner quickly
  95 */
  96 void umsdos_set_dirinfo(
     /* [previous][next][first][last][top][bottom][index][help] */
  97         struct inode *inode,
  98         struct inode *dir,
  99         off_t f_pos)
 100 {
 101         struct inode *emd_owner = umsdos_emd_dir_lookup(dir,1);
 102         inode->u.umsdos_i.i_dir_owner = dir->i_ino;
 103         inode->u.umsdos_i.i_emd_owner = emd_owner->i_ino;
 104         iput (emd_owner);
 105         inode->u.umsdos_i.pos = f_pos;
 106 }
 107 /*
 108         Tells if an Umsdos inode has been "patched" once.
 109         Return != 0 if so.
 110 */
 111 int umsdos_isinit (struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 112 {
 113 #if     1
 114         return inode->u.umsdos_i.i_emd_owner != 0;
 115 #elif 0
 116         return inode->i_atime != 0;
 117 #else
 118         return inode->i_count > 1;
 119 #endif
 120 }
 121 /*
 122         Connect the proper tables in the inode and add some info.
 123 */
 124 void umsdos_patch_inode (
     /* [previous][next][first][last][top][bottom][index][help] */
 125         struct inode *inode,
 126         struct inode *dir,              /* May be NULL */
 127         off_t f_pos)
 128 {
 129         /*
 130                 This function is called very early to setup the inode, somewhat
 131                 too early (called by UMSDOS_read_inode). At this point, we can't
 132                 do to much, such as lookup up EMD files and so on. This causes
 133                 confusion in the kernel. This is why some initialisation
 134                 will be done when dir != NULL only.
 135 
 136                 UMSDOS do run piggy back on top of msdos fs. It looks like something
 137                 is missing in the VFS to accommodate stacked fs. Still unclear what
 138                 (quite honestly).
 139 
 140                 Well, maybe one! A new entry "may_unmount" which would allow
 141                 the stacked fs to allocate some inode permanently and release
 142                 them at the end. Doing that now introduce a problem. unmount
 143                 always fail because some inodes are in use.
 144         */
 145         if (!umsdos_isinit(inode)){
 146                 inode->u.umsdos_i.i_emd_dir = 0;
 147                 if (S_ISREG(inode->i_mode)){
 148                         static char is_init = 0;
 149                         if (!is_init){
 150                                 /*
 151                                         I don't want to change the msdos file system code
 152                                         so I get the address of some subroutine dynamically
 153                                         once.
 154                                 */
 155                                 umsdos_file_inode_operations.bmap = inode->i_op->bmap;
 156                                 inode->i_op = &umsdos_file_inode_operations;
 157                                 is_init = 1;
 158                         }
 159                         inode->i_op = &umsdos_file_inode_operations;
 160                 }else if (S_ISDIR(inode->i_mode)){
 161                         if (dir != NULL){
 162                                 umsdos_setup_dir_inode(inode);
 163                         }
 164                 }else if (S_ISLNK(inode->i_mode)){
 165                         inode->i_op = &umsdos_symlink_inode_operations;
 166                 }else if (S_ISCHR(inode->i_mode)){
 167                         inode->i_op = &chrdev_inode_operations;
 168                 }else if (S_ISBLK(inode->i_mode)){
 169                         inode->i_op = &blkdev_inode_operations;
 170                 }else if (S_ISFIFO(inode->i_mode)){
 171                         init_fifo(inode);
 172                 }
 173                 if (dir != NULL){
 174                         /* #Specification: inode / umsdos info
 175                                 The first time an inode is seen (inode->i_count == 1),
 176                                 the inode number of the EMD file which control this inode
 177                                 is tagged to this inode. It allows operation such
 178                                 as notify_change to be handled.
 179                         */
 180                         /*
 181                                 This is done last because it also control the
 182                                 status of umsdos_isinit()
 183                         */
 184                         umsdos_set_dirinfo (inode,dir,f_pos);
 185                 }
 186         }else if (dir != NULL){
 187                 /*
 188                         Test to see if the info is maintained.
 189                         This should be removed when the file system will be proven.
 190                 */
 191                 struct inode *emd_owner = umsdos_emd_dir_lookup(dir,1);
 192                 iput (emd_owner);
 193                 if (emd_owner->i_ino != inode->u.umsdos_i.i_emd_owner){
 194                         printk ("UMSDOS: *** EMD_OWNER ??? *** ino = %ld %ld <> %ld "
 195                                 ,inode->i_ino,emd_owner->i_ino,inode->u.umsdos_i.i_emd_owner);
 196                 }
 197         }
 198 }
 199 /*
 200         Get the inode of the directory which owns this inode.
 201         Return 0 if ok, -EIO if error.
 202 */
 203 int umsdos_get_dirowner(
     /* [previous][next][first][last][top][bottom][index][help] */
 204         struct inode *inode,
 205         struct inode **result)  /* Hold NULL if any error */
 206                                                         /* else, the inode of the directory */
 207 {
 208         int ret = -EIO;
 209         unsigned long ino = inode->u.umsdos_i.i_dir_owner;
 210         *result = NULL;
 211         if (ino == 0){
 212                 printk ("UMSDOS: umsdos_get_dirowner ino == 0\n");
 213         }else{
 214                 struct inode *dir = *result = iget(inode->i_sb,ino);
 215                 if (dir != NULL){
 216                         umsdos_patch_inode (dir,NULL,0);
 217                         ret = 0;
 218                 }
 219         }
 220         return ret;
 221 }
 222 /*
 223         Load an inode from disk.
 224 */
 225 void UMSDOS_read_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 226 {
 227         PRINTK (("read inode %x ino = %d ",inode,inode->i_ino));
 228         msdos_read_inode(inode);
 229         PRINTK (("ino = %d %d\n",inode->i_ino,inode->i_count));
 230         if (S_ISDIR(inode->i_mode)
 231                 && (inode->u.umsdos_i.u.dir_info.creating != 0
 232                         || inode->u.umsdos_i.u.dir_info.looking != 0
 233                         || inode->u.umsdos_i.u.dir_info.p != NULL)){
 234                 Printk (("read inode %d %d %p\n"
 235                         ,inode->u.umsdos_i.u.dir_info.creating
 236                         ,inode->u.umsdos_i.u.dir_info.looking
 237                         ,inode->u.umsdos_i.u.dir_info.p));
 238         }
 239         /* #Specification: Inode / post initialisation
 240                 To completely initialise an inode, we need access to the owner
 241                 directory, so we can locate more info in the EMD file. This is
 242                 not available the first time the inode is access, we use
 243                 a value in the inode to tell if it has been finally initialised.
 244 
 245                 At first, we have tried testing i_count but it was causing
 246                 problem. It is possible that two or more process use the
 247                 newly accessed inode. While the first one block during
 248                 the initialisation (probably while reading the EMD file), the
 249                 others believe all is well because i_count > 1. They go banana
 250                 with a broken inode. See umsdos_lookup_patch and umsdos_patch_inode.
 251         */
 252         umsdos_patch_inode(inode,NULL,0);
 253 }
 254 
 255 /*
 256         Update the disk with the inode content
 257 */
 258 void UMSDOS_write_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 259 {
 260         struct iattr newattrs;
 261 
 262         PRINTK (("UMSDOS_write_inode emd %d\n",inode->u.umsdos_i.i_emd_owner));
 263         msdos_write_inode(inode);
 264         newattrs.ia_mtime = inode->i_mtime;
 265         newattrs.ia_atime = inode->i_atime;
 266         newattrs.ia_ctime = inode->i_ctime;
 267         newattrs.ia_valid = ATTR_MTIME | ATTR_ATIME | ATTR_CTIME;
 268         UMSDOS_notify_change (inode, &newattrs);
 269 }
 270 
 271 int UMSDOS_notify_change(struct inode *inode, struct iattr *attr)
     /* [previous][next][first][last][top][bottom][index][help] */
 272 {
 273         int ret = 0;
 274 
 275         if ((ret = inode_change_ok(inode, attr)) != 0) 
 276                 return ret;
 277 
 278         if (inode->i_nlink > 0){
 279                 /* #Specification: notify_change / i_nlink > 0
 280                         notify change is only done for inode with nlink > 0. An inode
 281                         with nlink == 0 is no longer associated with any entry in
 282                         the EMD file, so there is nothing to update.
 283                 */
 284                 unsigned long i_emd_owner = inode->u.umsdos_i.i_emd_owner;
 285                 if (inode == inode->i_sb->s_mounted){
 286                         /* #Specification: root inode / attributes
 287                                 I don't know yet how this should work. Normally
 288                                 the attributes (permissions bits, owner, times) of
 289                                 a directory are stored in the EMD file of its parent.
 290 
 291                                 One thing we could do is store the attributes of the root
 292                                 inode in its own EMD file. A simple entry named "." could
 293                                 be used for this special case. It would be read once
 294                                 when the file system is mounted and update in
 295                                 UMSDOS_notify_change() (right here).
 296 
 297                                 I am not sure of the behavior of the root inode for
 298                                 a real UNIX file system. For now, this is a nop.
 299                         */
 300                 }else if (i_emd_owner != 0xffffffff && i_emd_owner != 0){
 301                         /* This inode is not a EMD file nor an inode used internally
 302                                 by MSDOS, so we can update its status.
 303                                 See emd.c
 304                         */
 305                         struct inode *emd_owner = iget (inode->i_sb,i_emd_owner);
 306                         PRINTK (("notify change %p ",inode));
 307                         if (emd_owner == NULL){
 308                                 printk ("UMSDOS: emd_owner = NULL ???");
 309                                 ret = -EPERM;
 310                         }else{
 311                                 struct file filp;
 312                                 struct umsdos_dirent entry;
 313                                 filp.f_pos = inode->u.umsdos_i.pos;
 314                                 PRINTK (("pos = %d ",filp.f_pos));
 315                                 /* Read only the start of the entry since we don't touch */
 316                                 /* the name */
 317                                 ret = umsdos_emd_dir_read (emd_owner,&filp,(char*)&entry
 318                                         ,UMSDOS_REC_SIZE);
 319                                 if (ret == 0){
 320                                         if (attr->ia_valid & ATTR_UID) 
 321                                                 entry.uid = attr->ia_uid;
 322                                         if (attr->ia_valid & ATTR_GID) 
 323                                                 entry.gid = attr->ia_gid;
 324                                         if (attr->ia_valid & ATTR_MODE) 
 325                                                 entry.mode = attr->ia_mode;
 326                                         if (attr->ia_valid & ATTR_ATIME) 
 327                                                 entry.atime = attr->ia_atime;
 328                                         if (attr->ia_valid & ATTR_MTIME) 
 329                                                 entry.mtime = attr->ia_mtime;
 330                                         if (attr->ia_valid & ATTR_CTIME) 
 331                                                 entry.ctime = attr->ia_ctime;
 332 
 333                                         entry.nlink = inode->i_nlink;
 334                                         filp.f_pos = inode->u.umsdos_i.pos;
 335                                         ret = umsdos_emd_dir_write (emd_owner,&filp,(char*)&entry
 336                                                 ,UMSDOS_REC_SIZE);
 337 
 338                                         PRINTK (("notify pos %d ret %d nlink %d "
 339                                                 ,inode->u.umsdos_i.pos
 340                                                 ,ret,entry.nlink));
 341                                         /* #Specification: notify_change / msdos fs
 342                                                 notify_change operation are done only on the
 343                                                 EMD file. The msdos fs is not even called.
 344                                         */
 345                                 }
 346                                 iput (emd_owner);
 347                         }
 348                         PRINTK (("\n"));
 349                 }
 350         }
 351         if (ret == 0) 
 352                 inode_setattr(inode, attr);
 353         return ret;
 354 }
 355 
 356 /* #Specification: function name / convention
 357         A simple convention for function name has been used in
 358         the UMSDOS file system. First all function use the prefix
 359         umsdos_ to avoid name clash with other part of the kernel.
 360 
 361         And standard VFS entry point use the prefix UMSDOS (upper case)
 362         so it's easier to tell them apart.
 363 */
 364 
 365 static struct super_operations umsdos_sops = { 
 366         UMSDOS_read_inode,
 367         UMSDOS_notify_change,
 368         UMSDOS_write_inode,
 369         UMSDOS_put_inode,
 370         UMSDOS_put_super,
 371         NULL, /* added in 0.96c */
 372         UMSDOS_statfs,
 373         NULL
 374 };
 375 
 376 /*
 377         Read the super block of an Extended MS-DOS FS.
 378 */
 379 struct super_block *UMSDOS_read_super(
     /* [previous][next][first][last][top][bottom][index][help] */
 380         struct super_block *s,
 381         void *data,
 382         int silent)
 383 {
 384         /* #Specification: mount / options
 385                 Umsdos run on top of msdos. Currently, it supports no
 386                 mount option, but happily pass all option received to
 387                 the msdos driver. I am not sure if all msdos mount option
 388                 make sense with Umsdos. Here are at least those who
 389                 are useful.
 390                         uid=
 391                         gid=
 392 
 393                 These options affect the operation of umsdos in directories
 394                 which do not have an EMD file. They behave like normal
 395                 msdos directory, with all limitation of msdos.
 396         */
 397         struct super_block *sb = msdos_read_super(s,data,silent);
 398         printk ("UMSDOS Alpha 0.4 (compatibility level %d.%d)\n"
 399                 ,UMSDOS_VERSION,UMSDOS_RELEASE);
 400         if (sb != NULL){
 401                 sb->s_op = &umsdos_sops;
 402                 PRINTK (("umsdos_read_super %p\n",sb->s_mounted));
 403                 umsdos_setup_dir_inode (sb->s_mounted);
 404                 PRINTK (("End umsdos_read_super\n"));
 405                 if (s == super_blocks){
 406                         /* #Specification: pseudo root / mount
 407                                 When a umsdos fs is mounted, a special handling is done
 408                                 if it is the root partition. We check for the presence
 409                                 of the file /linux/etc/init or /linux/etc/rc.
 410                                 If one is there, we do a chroot("/linux").
 411 
 412                                 We check both because (see init/main.c) the kernel
 413                                 try to exec init at different place and if it fails
 414                                 it tries /bin/sh /etc/rc. To be consistent with
 415                                 init/main.c, many more test would have to be done
 416                                 to locate init. Any complain ?
 417 
 418                                 The chroot is done manually in init/main.c but the
 419                                 info (the inode) is located at mount time and store
 420                                 in a global variable (pseudo_root) which is used at
 421                                 different place in the umsdos driver. There is no
 422                                 need to store this variable elsewhere because it
 423                                 will always be one, not one per mount.
 424 
 425                                 This feature allows the installation
 426                                 of a linux system within a DOS system in a subdirectory.
 427         
 428                                 A user may install its linux stuff in c:\linux
 429                                 avoiding any clash with existing DOS file and subdirectory.
 430                                 When linux boots, it hides this fact, showing a normal
 431                                 root directory with /etc /bin /tmp ...
 432 
 433                                 The word "linux" is hardcoded in /usr/include/linux/umsdos_fs.h
 434                                 in the macro UMSDOS_PSDROOT_NAME.
 435                         */
 436 
 437                         struct inode *pseudo;
 438                         Printk (("Mounting root\n"));
 439                         if (umsdos_real_lookup (sb->s_mounted,UMSDOS_PSDROOT_NAME
 440                                         ,UMSDOS_PSDROOT_LEN,&pseudo)==0
 441                                 && S_ISDIR(pseudo->i_mode)){
 442                                 struct inode *etc = NULL;
 443                                 struct inode *rc = NULL;
 444                                 Printk (("/%s is there\n",UMSDOS_PSDROOT_NAME));
 445                                 if (umsdos_real_lookup (pseudo,"etc",3,&etc)==0
 446                                         && S_ISDIR(etc->i_mode)){
 447                                         struct inode *init;
 448                                         Printk (("/%s/etc is there\n",UMSDOS_PSDROOT_NAME));
 449                                         if ((umsdos_real_lookup (etc,"init",4,&init)==0
 450                                                         && S_ISREG(init->i_mode))
 451                                                 || (umsdos_real_lookup (etc,"rc",2,&rc)==0
 452                                                         && S_ISREG(rc->i_mode))){
 453                                                 umsdos_setup_dir_inode (pseudo);
 454                                                 Printk (("Activating pseudo root /%s\n",UMSDOS_PSDROOT_NAME));
 455                                                 pseudo_root = pseudo;
 456                                                 pseudo->i_count++;
 457                                                 pseudo = NULL;
 458                                         }
 459                                         iput (init);
 460                                         iput (rc);
 461                                 }
 462                                 iput (etc);
 463                         }
 464                         iput (pseudo);
 465                 }
 466         }
 467         return sb;
 468 }
 469 
 470 

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