~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/ntfs/fs.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  fs.c
  3  *  NTFS driver for Linux 2.3.x
  4  *
  5  *  Copyright (C) 1995-1997, 1999 Martin von Löwis
  6  *  Copyright (C) 1996 Richard Russon
  7  *  Copyright (C) 1996-1997 Régis Duchesne
  8  *  Copyright (C) 2000, Anton Altaparmakov
  9  */
 10 
 11 #ifdef HAVE_CONFIG_H
 12 #include "config.h"
 13 #endif
 14 #ifdef NTFS_IN_LINUX_KERNEL
 15 #include <linux/config.h>
 16 #endif
 17 
 18 #include "ntfstypes.h"
 19 #include "struct.h"
 20 #include "util.h"
 21 #include "inode.h"
 22 #include "super.h"
 23 #include "dir.h"
 24 #include "support.h"
 25 #include "macros.h"
 26 #include "sysctl.h"
 27 #include <linux/module.h>
 28 #include <asm/uaccess.h>
 29 #include <linux/nls.h>
 30 #include <linux/locks.h>
 31 #include <linux/init.h>
 32 #include <linux/smp_lock.h>
 33 
 34 /* Forward declarations */
 35 static struct inode_operations ntfs_dir_inode_operations;
 36 static struct file_operations ntfs_dir_operations;
 37 
 38 #define ITEM_SIZE 2040
 39 
 40 /* io functions to user space */
 41 static void ntfs_putuser(ntfs_io* dest,void *src,ntfs_size_t len)
 42 {
 43         copy_to_user(dest->param,src,len);
 44         dest->param+=len;
 45 }
 46 
 47 #ifdef CONFIG_NTFS_RW
 48 struct ntfs_getuser_update_vm_s{
 49         const char *user;
 50         struct inode *ino;
 51         loff_t off;
 52 };
 53 
 54 static void ntfs_getuser_update_vm (void *dest, ntfs_io *src, ntfs_size_t len)
 55 {
 56         struct ntfs_getuser_update_vm_s *p = src->param;
 57         copy_from_user (dest, p->user, len);
 58         p->user += len;
 59         p->off += len;
 60 }
 61 #endif
 62 
 63 static ssize_t
 64 ntfs_read(struct file * filp, char *buf, size_t count, loff_t *off)
 65 {
 66         int error;
 67         ntfs_io io;
 68         ntfs_inode *ino=NTFS_LINO2NINO(filp->f_dentry->d_inode);
 69 
 70         /* inode is not properly initialized */
 71         if(!ino)return -EINVAL;
 72         ntfs_debug(DEBUG_OTHER, "ntfs_read %x,%x,%x ->",
 73                    (unsigned)ino->i_number,(unsigned)*off,(unsigned)count);
 74         /* inode has no unnamed data attribute */
 75         if(!ntfs_find_attr(ino,ino->vol->at_data,NULL))
 76                 return -EINVAL;
 77         
 78         /* read the data */
 79         io.fn_put=ntfs_putuser;
 80         io.fn_get=0;
 81         io.param=buf;
 82         io.size=count;
 83         error=ntfs_read_attr(ino,ino->vol->at_data,NULL,*off,&io);
 84         if(error && !io.size)return -error;
 85         
 86         *off+=io.size;
 87         return io.size;
 88 }
 89 
 90 #ifdef CONFIG_NTFS_RW
 91 static ssize_t
 92 ntfs_write(struct file *filp,const char* buf,size_t count,loff_t *pos)
 93 {
 94         int ret;
 95         ntfs_io io;
 96         struct inode *inode = filp->f_dentry->d_inode;
 97         ntfs_inode *ino = NTFS_LINO2NINO(inode);
 98         struct ntfs_getuser_update_vm_s param;
 99 
100         if (!ino)
101                 return -EINVAL;
102         ntfs_debug (DEBUG_LINUX, "ntfs_write %x,%x,%x ->\n",
103                (unsigned)ino->i_number, (unsigned)*pos, (unsigned)count);
104         /* Allows to lock fs ro at any time */
105         if (inode->i_sb->s_flags & MS_RDONLY)
106                 return -ENOSPC;
107         if (!ntfs_find_attr(ino,ino->vol->at_data,NULL))
108                 return -EINVAL;
109 
110         /* Evaluating O_APPEND is the file system's job... */
111         if (filp->f_flags & O_APPEND)
112                 *pos = inode->i_size;
113         param.user = buf;
114         param.ino = inode;
115         param.off = *pos;
116         io.fn_put = 0;
117         io.fn_get = ntfs_getuser_update_vm;
118         io.param = &param;
119         io.size = count;
120         ret = ntfs_write_attr (ino, ino->vol->at_data, NULL, *pos, &io);
121         ntfs_debug (DEBUG_LINUX, "write -> %x\n", ret);
122         if(ret<0)
123                 return -EINVAL;
124 
125         *pos += io.size;
126         if (*pos > inode->i_size)
127                 inode->i_size = *pos;
128         mark_inode_dirty (filp->f_dentry->d_inode);
129         return io.size;
130 }
131 #endif
132 
133 struct ntfs_filldir{
134         struct inode *dir;
135         filldir_t filldir;
136         unsigned int type;
137         ntfs_u32 ph,pl;
138         void *dirent;
139         char *name;
140         int namelen;
141 };
142         
143 static int ntfs_printcb(ntfs_u8 *entry,void *param)
144 {
145         struct ntfs_filldir* nf=param;
146         int flags=NTFS_GETU8(entry+0x51);
147         int show_hidden=0;
148         int length=NTFS_GETU8(entry+0x50);
149         int inum=NTFS_GETU32(entry);
150         int error;
151 #ifdef NTFS_NGT_NT_DOES_LOWER
152         int i,to_lower=0;
153 #endif
154         switch(nf->type){
155         case ngt_dos:
156                 /* Don't display long names */
157                 if((flags & 2)==0)
158                         return 0;
159                 break;
160         case ngt_nt:
161                 /* Don't display short-only names */
162                 switch(flags&3){
163                 case 2: return 0;
164 #ifdef NTFS_NGT_NT_DOES_LOWER
165                 case 3: to_lower=1;
166 #endif
167                 }
168                 break;
169         case ngt_posix:
170                 break;
171         case ngt_full:
172                 show_hidden=1;
173                 break;
174         }
175         if(!show_hidden && ((NTFS_GETU8(entry+0x48) & 2)==2)){
176                 ntfs_debug(DEBUG_OTHER,"Skipping hidden file\n");
177                 return 0;
178         }
179         nf->name=0;
180         if(ntfs_encodeuni(NTFS_INO2VOL(nf->dir),(ntfs_u16*)(entry+0x52),
181                           length,&nf->name,&nf->namelen)){
182                 ntfs_debug(DEBUG_OTHER,"Skipping unrepresentable file\n");
183                 if(nf->name)ntfs_free(nf->name);
184                 return 0;
185         }
186         /* Do not return ".", as this is faked */
187         if(length==1 && *nf->name=='.')
188                 return 0;
189 #ifdef NTFS_NGT_NT_DOES_LOWER
190         if(to_lower)
191                 for(i=0;i<nf->namelen;i++)
192                         /* This supports ASCII only. Since only DOS-only
193                            names get converted, and since those are restricted
194                            to ASCII, this should be correct */
195                         if(nf->name[i]>='A' && nf->name[i]<='Z')
196                                 nf->name[i]+='a'-'A';
197 #endif
198         nf->name[nf->namelen]=0;
199         ntfs_debug(DEBUG_OTHER, "readdir got %s,len %d\n",nf->name,nf->namelen);
200         /* filldir expects an off_t rather than an loff_t.
201            Hope we don't have more than 65535 index records */
202         error=nf->filldir(nf->dirent,nf->name,nf->namelen,
203                         (nf->ph<<16)|nf->pl,inum,DT_UNKNOWN);
204         ntfs_free(nf->name);
205         /* Linux filldir errors are negative, other errors positive */
206         return error;
207 }
208 
209 /* readdir returns '..', then '.', then the directory entries in sequence
210    As the root directory contains a entry for itself, '.' is not emulated
211    for the root directory */
212 static int ntfs_readdir(struct file* filp, void *dirent, filldir_t filldir)
213 {
214         struct ntfs_filldir cb;
215         int error;
216         struct inode *dir=filp->f_dentry->d_inode;
217 
218         ntfs_debug(DEBUG_OTHER, "ntfs_readdir ino %x mode %x\n",
219                (unsigned)dir->i_ino,(unsigned int)dir->i_mode);
220 
221         ntfs_debug(DEBUG_OTHER, "readdir: Looking for file %x dircount %d\n",
222                (unsigned)filp->f_pos,atomic_read(&dir->i_count));
223         cb.pl=filp->f_pos & 0xFFFF;
224         cb.ph=filp->f_pos >> 16;
225         /* end of directory */
226         if(cb.ph==0xFFFF){
227                 /* FIXME: Maybe we can return those with the previous call */
228                 switch(cb.pl){
229                 case 0: filldir(dirent,".",1,filp->f_pos,dir->i_ino,DT_DIR);
230                         filp->f_pos=0xFFFF0001;
231                         return 0;
232                         /* FIXME: parent directory */
233                 case 1: filldir(dirent,"..",2,filp->f_pos,0,DT_DIR);
234                         filp->f_pos=0xFFFF0002;
235                         return 0;
236                 }
237                 ntfs_debug(DEBUG_OTHER, "readdir: EOD\n");
238                 return 0;
239         }
240         cb.dir=dir;
241         cb.filldir=filldir;
242         cb.dirent=dirent;
243         cb.type=NTFS_INO2VOL(dir)->ngt;
244         do{
245                 ntfs_debug(DEBUG_OTHER,"looking for next file\n");
246                 error=ntfs_getdir_unsorted(NTFS_LINO2NINO(dir),&cb.ph,&cb.pl,
247                                    ntfs_printcb,&cb);
248         }while(!error && cb.ph!=0xFFFFFFFF);
249         filp->f_pos=(cb.ph<<16)|cb.pl;
250         ntfs_debug(DEBUG_OTHER, "new position %x\n",(unsigned)filp->f_pos);
251         /* -EINVAL is on user buffer full. This is not considered 
252            as an error by sys_getdents */
253         if(error<0) 
254                 error=0;
255         /* Otherwise (device error, inconsistent data), switch the sign */
256         return -error;
257 }
258 
259 /* Copied from vfat driver */
260 static int simple_getbool(char *s, int *setval)
261 {
262         if (s) {
263                 if (!strcmp(s,"1") || !strcmp(s,"yes") || !strcmp(s,"true")) {
264                         *setval = 1;
265                 } else if (!strcmp(s,"") || !strcmp(s,"no") || !strcmp(s,"false")) {
266                         *setval = 0;
267                 } else {
268                         return 0;
269                 }
270         } else {
271                 *setval = 1;
272         }
273         return 1;
274 }
275 
276 /* Parse the (re)mount options */
277 static int parse_options(ntfs_volume* vol,char *opt)
278 {
279         char *value;
280 
281         vol->uid=vol->gid=0;
282         vol->umask=0077;
283         vol->ngt=ngt_nt;
284         vol->nls_map=0;
285         vol->nct=0;
286         if(!opt)goto done;
287 
288         for(opt = strtok(opt,",");opt;opt=strtok(NULL,","))
289         {
290                 if ((value = strchr(opt, '=')) != NULL)
291                         *value++='\0';
292                 if(strcmp(opt,"uid")==0)
293                 {
294                         if(!value || !*value)goto needs_arg;
295                         vol->uid=simple_strtoul(value,&value,0);
296                         if(*value){
297                                 printk(KERN_ERR "NTFS: uid invalid argument\n");
298                                 return 0;
299                         }
300                 }else if(strcmp(opt, "gid") == 0)
301                 {
302                         if(!value || !*value)goto needs_arg;
303                         vol->gid=simple_strtoul(value,&value,0);
304                         if(*value){
305                                 printk(KERN_ERR "gid invalid argument\n");
306                                 return 0;
307                         }
308                 }else if(strcmp(opt, "umask") == 0)
309                 {
310                         if(!value || !*value)goto needs_arg;
311                         vol->umask=simple_strtoul(value,&value,0);
312                         if(*value){
313                                 printk(KERN_ERR "umask invalid argument\n");
314                                 return 0;
315                         }
316                 }else if(strcmp(opt, "iocharset") == 0){
317                         if(!value || !*value)goto needs_arg;
318                         vol->nls_map=load_nls(value);
319                         vol->nct |= nct_map;
320                         if(!vol->nls_map){
321                                 printk(KERN_ERR "NTFS: charset not found");
322                                 return 0;
323                         }
324                 }else if(strcmp(opt, "posix") == 0){
325                         int val;
326                         if(!value || !*value)goto needs_arg;
327                         if(!simple_getbool(value,&val))
328                                 goto needs_bool;
329                         vol->ngt=val?ngt_posix:ngt_nt;
330                 }else if(strcmp(opt,"utf8") == 0){
331                         int val=0;
332                         if(!value || !*value)
333                                 val=1;
334                         else if(!simple_getbool(value,&val))
335                                 goto needs_bool;
336                         if(val)
337                                 vol->nct|=nct_utf8;
338                 }else if(strcmp(opt,"uni_xlate") == 0){
339                         int val=0;
340                         /* no argument: uni_vfat.
341                            boolean argument: uni_vfat.
342                            "2": uni.
343                         */
344                         if(!value || !*value)
345                                 val=1;
346                         else if(strcmp(value,"2")==0)
347                                 vol->nct |= nct_uni_xlate;
348                         else if(!simple_getbool(value,&val))
349                                 goto needs_bool;
350                         if(val)
351                                 vol->nct |= nct_uni_xlate_vfat | nct_uni_xlate;
352                 }else{
353                         printk(KERN_ERR "NTFS: unkown option '%s'\n", opt);
354                         return 0;
355                 }
356         }
357         if(vol->nct & nct_utf8 & (nct_map | nct_uni_xlate)){
358                 printk(KERN_ERR "utf8 cannot be combined with iocharset or uni_xlate\n");
359                 return 0;
360         }
361  done:
362         if((vol->nct & (nct_uni_xlate | nct_map | nct_utf8))==0)
363                 /* default to UTF-8 */
364                 vol->nct=nct_utf8;
365         if(!vol->nls_map){
366                 vol->nls_map=load_nls_default();
367                 if (vol->nls_map)
368                         vol->nct=nct_map | (vol->nct&nct_uni_xlate);
369         }
370         return 1;
371 
372  needs_arg:
373         printk(KERN_ERR "NTFS: %s needs an argument",opt);
374         return 0;
375  needs_bool:
376         printk(KERN_ERR "NTFS: %s needs boolean argument",opt);
377         return 0;
378 }
379                         
380 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *d)
381 {
382         struct inode *res=0;
383         char *item=0;
384         ntfs_iterate_s walk;
385         int error;
386         ntfs_debug(DEBUG_NAME1, "Looking up %s in %x\n",d->d_name.name,
387                    (unsigned)dir->i_ino);
388         /* convert to wide string */
389         error=ntfs_decodeuni(NTFS_INO2VOL(dir),(char*)d->d_name.name,
390                              d->d_name.len,&walk.name,&walk.namelen);
391         if(error)
392                 return ERR_PTR(-error);
393         item=ntfs_malloc(ITEM_SIZE);
394         if( !item )
395                 return ERR_PTR(-ENOMEM);
396         /* ntfs_getdir will place the directory entry into item,
397            and the first long long is the MFT record number */
398         walk.type=BY_NAME;
399         walk.dir=NTFS_LINO2NINO(dir);
400         walk.result=item;
401         if(ntfs_getdir_byname(&walk))
402         {
403                 res=iget(dir->i_sb,NTFS_GETU32(item));
404         }
405         d_add(d,res);
406         ntfs_free(item);
407         ntfs_free(walk.name);
408         /* Always return success, the dcache will handle negative entries. */
409         return NULL;
410 }
411 
412 static struct file_operations ntfs_file_operations_nommap = {
413         read:           ntfs_read,
414 #ifdef CONFIG_NTFS_RW
415         write:          ntfs_write,
416 #endif
417 };
418 
419 static struct inode_operations ntfs_inode_operations_nobmap;
420 
421 #ifdef CONFIG_NTFS_RW
422 static int
423 ntfs_create(struct inode* dir,struct dentry *d,int mode)
424 {
425         struct inode *r=0;
426         ntfs_inode *ino=0;
427         ntfs_volume *vol;
428         int error=0;
429         ntfs_attribute *si;
430 
431         r=new_inode(dir->i_sb);
432         if(!r){
433                 error=ENOMEM;
434                 goto fail;
435         }
436 
437         ntfs_debug(DEBUG_OTHER, "ntfs_create %s\n",d->d_name.name);
438         vol=NTFS_INO2VOL(dir);
439 #ifdef NTFS_IN_LINUX_KERNEL
440         ino=NTFS_LINO2NINO(r);
441 #else
442         ino=ntfs_malloc(sizeof(ntfs_inode));
443         if(!ino){
444                 error=ENOMEM;
445                 goto fail;
446         }
447         r->u.generic_ip=ino;
448 #endif
449         error=ntfs_alloc_file(NTFS_LINO2NINO(dir),ino,(char*)d->d_name.name,
450                                d->d_name.len);
451         if(error)goto fail;
452         error=ntfs_update_inode(ino);
453         if(error)goto fail;
454         error=ntfs_update_inode(NTFS_LINO2NINO(dir));
455         if(error)goto fail;
456 
457         r->i_uid=vol->uid;
458         r->i_gid=vol->gid;
459         /* FIXME: dirty? dev? */
460         /* get the file modification times from the standard information */
461         si=ntfs_find_attr(ino,vol->at_standard_information,NULL);
462         if(si){
463                 char *attr=si->d.data;
464                 r->i_atime=ntfs_ntutc2unixutc(NTFS_GETU64(attr+0x18));
465                 r->i_ctime=ntfs_ntutc2unixutc(NTFS_GETU64(attr));
466                 r->i_mtime=ntfs_ntutc2unixutc(NTFS_GETU64(attr+8));
467         }
468         /* It's not a directory */
469         r->i_op=&ntfs_inode_operations_nobmap;
470         r->i_fop=&ntfs_file_operations_nommap,
471         r->i_mode=S_IFREG|S_IRUGO;
472 #ifdef CONFIG_NTFS_RW
473         r->i_mode|=S_IWUGO;
474 #endif
475         r->i_mode &= ~vol->umask;
476 
477         insert_inode_hash(r);
478         d_instantiate(d,r);
479         return 0;
480  fail:
481         #ifndef NTFS_IN_LINUX_KERNEL
482         if(ino)ntfs_free(ino);
483         #endif
484         if(r)iput(r);
485         return -error;
486 }
487 
488 static int
489 _linux_ntfs_mkdir(struct inode *dir, struct dentry* d, int mode)
490 {
491         int error;
492         struct inode *r = 0;
493         ntfs_volume *vol;
494         ntfs_inode *ino;
495         ntfs_attribute *si;
496 
497         ntfs_debug (DEBUG_DIR1, "mkdir %s in %x\n",d->d_name.name, dir->i_ino);
498         error = ENAMETOOLONG;
499         if (d->d_name.len > /* FIXME */255)
500                 goto out;
501 
502         error = EIO;
503         r = new_inode(dir->i_sb);
504         if (!r)
505                 goto out;
506         
507         vol = NTFS_INO2VOL(dir);
508 #ifdef NTFS_IN_LINUX_KERNEL
509         ino = NTFS_LINO2NINO(r);
510 #else
511         ino = ntfs_malloc(sizeof(ntfs_inode));
512         error = ENOMEM;
513         if(!ino)
514                 goto out;
515         r->u.generic_ip = ino;
516 #endif
517         error = ntfs_mkdir(NTFS_LINO2NINO(dir), 
518                            d->d_name.name, d->d_name.len, ino);
519         if(error)
520                 goto out;
521         r->i_uid = vol->uid;
522         r->i_gid = vol->gid;
523         si = ntfs_find_attr(ino,vol->at_standard_information,NULL);
524         if(si){
525                 char *attr = si->d.data;
526                 r->i_atime = ntfs_ntutc2unixutc(NTFS_GETU64(attr+0x18));
527                 r->i_ctime = ntfs_ntutc2unixutc(NTFS_GETU64(attr));
528                 r->i_mtime = ntfs_ntutc2unixutc(NTFS_GETU64(attr+8));
529         }
530         /* It's a directory */
531         r->i_op = &ntfs_dir_inode_operations;
532         r->i_fop = &ntfs_dir_operations;
533         r->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
534 #ifdef CONFIG_NTFS_RW
535         r->i_mode|=S_IWUGO;
536 #endif
537         r->i_mode &= ~vol->umask;       
538         
539         insert_inode_hash(r);
540         d_instantiate(d, r);
541         error = 0;
542  out:
543         ntfs_debug (DEBUG_DIR1, "mkdir returns %d\n", -error);
544         return -error;
545 }
546 #endif
547 
548 #if 0
549 static int 
550 ntfs_bmap(struct inode *ino,int block)
551 {
552         int ret=ntfs_vcn_to_lcn(NTFS_LINO2NINO(ino),block);
553         ntfs_debug(DEBUG_OTHER, "bmap of %lx,block %x is %x\n",
554                ino->i_ino,block,ret);
555         return (ret==-1) ? 0:ret;
556 }
557 #endif
558 
559 /* It's fscking broken. */
560 /* FIXME: [bm]map code is disabled until ntfs_get_block gets sorted! */
561 /*
562 static int ntfs_get_block(struct inode *inode, long block, struct buffer_head *bh, int create)
563 {
564         BUG();
565         return -1;
566 }
567 
568 static struct file_operations ntfs_file_operations = {
569         read:           ntfs_read,
570         mmap:           generic_file_mmap,
571 #ifdef CONFIG_NTFS_RW
572         write:          ntfs_write,
573 #endif
574 };
575 
576 static struct inode_operations ntfs_inode_operations;
577 */
578 
579 static struct file_operations ntfs_dir_operations = {
580         read:           generic_read_dir,
581         readdir:        ntfs_readdir,
582 };
583 
584 static struct inode_operations ntfs_dir_inode_operations = {
585         lookup:         ntfs_lookup,
586 #ifdef CONFIG_NTFS_RW
587         create:         ntfs_create,
588         mkdir:          _linux_ntfs_mkdir,
589 #endif
590 };
591 
592 /*
593 static int ntfs_writepage(struct page *page)
594 {
595         return block_write_full_page(page,ntfs_get_block);
596 }
597 static int ntfs_readpage(struct file *file, struct page *page)
598 {
599         return block_read_full_page(page,ntfs_get_block);
600 }
601 static int ntfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
602 {
603         return cont_prepare_write(page,from,to,ntfs_get_block,
604                 &page->mapping->host->u.ntfs_i.mmu_private);
605 }
606 static int _ntfs_bmap(struct address_space *mapping, long block)
607 {
608         return generic_block_bmap(mapping,block,ntfs_get_block);
609 }
610 struct address_space_operations ntfs_aops = {
611         readpage: ntfs_readpage,
612         writepage: ntfs_writepage,
613         sync_page: block_sync_page,
614         prepare_write: ntfs_prepare_write,
615         commit_write: generic_commit_write,
616         bmap: _ntfs_bmap
617 };
618 */
619 
620 /* ntfs_read_inode is called by the Virtual File System (the kernel layer that
621  * deals with filesystems) when iget is called requesting an inode not already
622  * present in the inode table. Typically filesystems have separate
623  * inode_operations for directories, files and symlinks.
624  */
625 static void ntfs_read_inode(struct inode* inode)
626 {
627         ntfs_volume *vol;
628         int can_mmap=0;
629         ntfs_inode *ino;
630         ntfs_attribute *data;
631         ntfs_attribute *si;
632 
633         vol=NTFS_INO2VOL(inode);
634         inode->i_mode=0;
635         ntfs_debug(DEBUG_OTHER, "ntfs_read_inode %x\n",(unsigned)inode->i_ino);
636 
637         switch(inode->i_ino)
638         {
639                 /* those are loaded special files */
640         case FILE_MFT:
641                 ntfs_error("Trying to open MFT\n");return;
642         default:
643                 #ifdef NTFS_IN_LINUX_KERNEL
644                 ino=&inode->u.ntfs_i;
645                 #else
646                 /* FIXME: check for ntfs_malloc failure */
647                 ino=(ntfs_inode*)ntfs_malloc(sizeof(ntfs_inode));
648                 inode->u.generic_ip=ino;
649                 #endif
650                 if(!ino || ntfs_init_inode(ino,
651                                            NTFS_INO2VOL(inode),inode->i_ino))
652                 {
653                         ntfs_debug(DEBUG_OTHER, "NTFS:Error loading inode %x\n",
654                                (unsigned int)inode->i_ino);
655                         return;
656                 }
657         }
658         /* Set uid/gid from mount options */
659         inode->i_uid=vol->uid;
660         inode->i_gid=vol->gid;
661         inode->i_nlink=1;
662         /* Use the size of the data attribute as file size */
663         data = ntfs_find_attr(ino,vol->at_data,NULL);
664         if(!data)
665         {
666                 inode->i_size=0;
667                 can_mmap=0;
668         }
669         else
670         {
671                 inode->i_size=data->size;
672                 /* FIXME: once ntfs_get_block is implemented, uncomment the
673                  * next line and remove the can_mmap = 0; */
674                 /* can_mmap=!data->resident && !data->compressed;*/
675                 can_mmap = 0;
676         }
677         /* get the file modification times from the standard information */
678         si=ntfs_find_attr(ino,vol->at_standard_information,NULL);
679         if(si){
680                 char *attr=si->d.data;
681                 inode->i_atime=ntfs_ntutc2unixutc(NTFS_GETU64(attr+0x18));
682                 inode->i_ctime=ntfs_ntutc2unixutc(NTFS_GETU64(attr));
683                 inode->i_mtime=ntfs_ntutc2unixutc(NTFS_GETU64(attr+8));
684         }
685         /* if it has an index root, it's a directory */
686         if(ntfs_find_attr(ino,vol->at_index_root,"$I30"))
687         {
688                 ntfs_attribute *at;
689                 at = ntfs_find_attr (ino, vol->at_index_allocation, "$I30");
690                 inode->i_size = at ? at->size : 0;
691           
692                 inode->i_op=&ntfs_dir_inode_operations;
693                 inode->i_fop=&ntfs_dir_operations;
694                 inode->i_mode=S_IFDIR|S_IRUGO|S_IXUGO;
695         }
696         else
697         {
698                 /* As long as ntfs_get_block() is just a call to BUG() do not
699                  * define any [bm]map ops or we get the BUG() whenever someone
700                  * runs mc or mpg123 on an ntfs partition!
701                  * FIXME: Uncomment the below code when ntfs_get_block is
702                  * implemented. */
703                 /* if (can_mmap) {
704                         inode->i_op = &ntfs_inode_operations;
705                         inode->i_fop = &ntfs_file_operations;
706                         inode->i_mapping->a_ops = &ntfs_aops;
707                         inode->u.ntfs_i.mmu_private = inode->i_size;
708                 } else */ {
709                         inode->i_op=&ntfs_inode_operations_nobmap;
710                         inode->i_fop=&ntfs_file_operations_nommap;
711                 }
712                 inode->i_mode=S_IFREG|S_IRUGO;
713         }
714 #ifdef CONFIG_NTFS_RW
715         if(!data || !data->compressed)
716                 inode->i_mode|=S_IWUGO;
717 #endif
718         inode->i_mode &= ~vol->umask;
719 }
720 
721 #ifdef CONFIG_NTFS_RW
722 static void 
723 ntfs_write_inode (struct inode *ino, int unused)
724 {
725         lock_kernel();
726         ntfs_debug (DEBUG_LINUX, "ntfs:write inode %x\n", ino->i_ino);
727         ntfs_update_inode (NTFS_LINO2NINO (ino));
728         unlock_kernel();
729 }
730 #endif
731 
732 static void _ntfs_clear_inode(struct inode *ino)
733 {
734         lock_kernel();
735         ntfs_debug(DEBUG_OTHER, "ntfs_clear_inode %lx\n",ino->i_ino);
736 #ifdef NTFS_IN_LINUX_KERNEL
737         if(ino->i_ino!=FILE_MFT)
738                 ntfs_clear_inode(&ino->u.ntfs_i);
739 #else
740         if(ino->i_ino!=FILE_MFT && ino->u.generic_ip)
741         {
742                 ntfs_clear_inode(ino->u.generic_ip);
743                 ntfs_free(ino->u.generic_ip);
744                 ino->u.generic_ip=0;
745         }
746 #endif
747         unlock_kernel();
748         return;
749 }
750 
751 /* Called when umounting a filesystem by do_umount() in fs/super.c */
752 static void ntfs_put_super(struct super_block *sb)
753 {
754         ntfs_volume *vol;
755 
756         ntfs_debug(DEBUG_OTHER, "ntfs_put_super\n");
757 
758         vol=NTFS_SB2VOL(sb);
759 
760         ntfs_release_volume(vol);
761         if(vol->nls_map)
762                 unload_nls(vol->nls_map);
763 #ifndef NTFS_IN_LINUX_KERNEL
764         ntfs_free(vol);
765 #endif
766         ntfs_debug(DEBUG_OTHER, "ntfs_put_super: done\n");
767 }
768 
769 /* Called by the kernel when asking for stats */
770 static int ntfs_statfs(struct super_block *sb, struct statfs *sf)
771 {
772         struct inode *mft;
773         ntfs_volume *vol;
774         ntfs_u64 size;
775         int error;
776 
777         ntfs_debug(DEBUG_OTHER, "ntfs_statfs\n");
778         vol=NTFS_SB2VOL(sb);
779         sf->f_type=NTFS_SUPER_MAGIC;
780         sf->f_bsize=vol->clustersize;
781 
782         error = ntfs_get_volumesize( NTFS_SB2VOL( sb ), &size );
783         if( error )
784                 return -error;
785         sf->f_blocks = size;    /* volumesize is in clusters */
786         sf->f_bfree=ntfs_get_free_cluster_count(vol->bitmap);
787         sf->f_bavail=sf->f_bfree;
788 
789         mft=iget(sb,FILE_MFT);
790         if (!mft)
791                 return -EIO;
792         /* So ... we lie... thus this following cast of loff_t value
793            is ok here.. */
794         sf->f_files = (unsigned long)mft->i_size / vol->mft_recordsize;
795         iput(mft);
796 
797         /* should be read from volume */
798         sf->f_namelen=255;
799         return 0;
800 }
801 
802 /* Called when remounting a filesystem by do_remount_sb() in fs/super.c */
803 static int ntfs_remount_fs(struct super_block *sb, int *flags, char *options)
804 {
805         if(!parse_options(NTFS_SB2VOL(sb), options))
806                 return -EINVAL;
807         return 0;
808 }
809 
810 /* Define the super block operation that are implemented */
811 static struct super_operations ntfs_super_operations = {
812         read_inode:     ntfs_read_inode,
813 #ifdef CONFIG_NTFS_RW
814         write_inode:    ntfs_write_inode,
815 #endif
816         put_super:      ntfs_put_super,
817         statfs:         ntfs_statfs,
818         remount_fs:     ntfs_remount_fs,
819         clear_inode:    _ntfs_clear_inode,
820 };
821 
822 /* Called to mount a filesystem by read_super() in fs/super.c
823  * Return a super block, the main structure of a filesystem
824  *
825  * NOTE : Don't store a pointer to an option, as the page containing the
826  * options is freed after ntfs_read_super() returns.
827  *
828  * NOTE : A context switch can happen in kernel code only if the code blocks
829  * (= calls schedule() in kernel/sched.c).
830  */
831 struct super_block * ntfs_read_super(struct super_block *sb, 
832                                      void *options, int silent)
833 {
834         ntfs_volume *vol;
835         struct buffer_head *bh;
836         int i;
837 
838         ntfs_debug(DEBUG_OTHER, "ntfs_read_super\n");
839 
840 #ifdef NTFS_IN_LINUX_KERNEL
841         vol = NTFS_SB2VOL(sb);
842 #else
843         if(!(vol = ntfs_malloc(sizeof(ntfs_volume))))
844                 goto ntfs_read_super_dec;
845         NTFS_SB2VOL(sb)=vol;
846 #endif
847         
848         if(!parse_options(vol,(char*)options))
849                 goto ntfs_read_super_vol;
850 
851 #if 0
852         /* Set to read only, user option might reset it */
853         sb->s_flags |= MS_RDONLY;
854 #endif
855 
856         /* Assume a 512 bytes block device for now */
857         set_blocksize(sb->s_dev, 512);
858         /* Read the super block (boot block) */
859         if(!(bh=bread(sb->s_dev,0,512))) {
860                 ntfs_error("Reading super block failed\n");
861                 goto ntfs_read_super_unl;
862         }
863         ntfs_debug(DEBUG_OTHER, "Done reading boot block\n");
864 
865         /* Check for 'NTFS' magic number */
866         if(!IS_NTFS_VOLUME(bh->b_data)){
867                 ntfs_debug(DEBUG_OTHER, "Not a NTFS volume\n");
868                 brelse(bh);
869                 goto ntfs_read_super_unl;
870         }
871 
872         ntfs_debug(DEBUG_OTHER, "Going to init volume\n");
873         ntfs_init_volume(vol,bh->b_data);
874         ntfs_debug(DEBUG_OTHER, "MFT record at cluster 0x%X\n",vol->mft_cluster);
875         brelse(bh);
876         NTFS_SB(vol)=sb;
877         ntfs_debug(DEBUG_OTHER, "Done to init volume\n");
878 
879         /* Inform the kernel that a device block is a NTFS cluster */
880         sb->s_blocksize=vol->clustersize;
881         for(i=sb->s_blocksize,sb->s_blocksize_bits=0;i != 1;i>>=1)
882                 sb->s_blocksize_bits++;
883         set_blocksize(sb->s_dev,sb->s_blocksize);
884         ntfs_debug(DEBUG_OTHER, "set_blocksize\n");
885 
886         /* Allocate a MFT record (MFT record can be smaller than a cluster) */
887         if(!(vol->mft=ntfs_malloc(max(vol->mft_recordsize,vol->clustersize))))
888                 goto ntfs_read_super_unl;
889 
890         /* Read at least the MFT record for $MFT */
891         for(i=0;i<max(vol->mft_clusters_per_record,1);i++){
892                 if(!(bh=bread(sb->s_dev,vol->mft_cluster+i,vol->clustersize))) {
893                         ntfs_error("Could not read MFT record 0\n");
894                         goto ntfs_read_super_mft;
895                 }
896                 ntfs_memcpy(vol->mft+i*vol->clustersize,bh->b_data,vol->clustersize);
897                 brelse(bh);
898                 ntfs_debug(DEBUG_OTHER, "Read cluster %x\n",vol->mft_cluster+i);
899         }
900 
901         /* Check and fixup this MFT record */
902         if(!ntfs_check_mft_record(vol,vol->mft)){
903                 ntfs_error("Invalid MFT record 0\n");
904                 goto ntfs_read_super_mft;
905         }
906 
907         /* Inform the kernel about which super operations are available */
908         sb->s_op = &ntfs_super_operations;
909         sb->s_magic = NTFS_SUPER_MAGIC;
910         
911         ntfs_debug(DEBUG_OTHER, "Reading special files\n");
912         if(ntfs_load_special_files(vol)){
913                 ntfs_error("Error loading special files\n");
914                 goto ntfs_read_super_mft;
915         }
916 
917         ntfs_debug(DEBUG_OTHER, "Getting RootDir\n");
918         /* Get the root directory */
919         if(!(sb->s_root=d_alloc_root(iget(sb,FILE_ROOT)))){
920                 ntfs_error("Could not get root dir inode\n");
921                 goto ntfs_read_super_mft;
922         }
923         ntfs_debug(DEBUG_OTHER, "read_super: done\n");
924         return sb;
925 
926 ntfs_read_super_mft:
927         ntfs_free(vol->mft);
928 ntfs_read_super_unl:
929 ntfs_read_super_vol:
930         #ifndef NTFS_IN_LINUX_KERNEL
931         ntfs_free(vol);
932 ntfs_read_super_dec:
933         #endif
934         ntfs_debug(DEBUG_OTHER, "read_super: done\n");
935         return NULL;
936 }
937 
938 /* Define the filesystem
939  */
940 static DECLARE_FSTYPE_DEV(ntfs_fs_type, "ntfs", ntfs_read_super);
941 
942 static int __init init_ntfs_fs(void)
943 {
944         /* Comment this if you trust klogd. There are reasons not to trust it
945          */
946 #if defined(DEBUG) && !defined(MODULE)
947         console_verbose();
948 #endif
949         printk(KERN_NOTICE "NTFS version " NTFS_VERSION "\n");
950         SYSCTL(1);
951         ntfs_debug(DEBUG_OTHER, "registering %s\n",ntfs_fs_type.name);
952         /* add this filesystem to the kernel table of filesystems */
953         return register_filesystem(&ntfs_fs_type);
954 }
955 
956 static void __exit exit_ntfs_fs(void)
957 {
958         SYSCTL(0);
959         ntfs_debug(DEBUG_OTHER, "unregistering %s\n",ntfs_fs_type.name);
960         unregister_filesystem(&ntfs_fs_type);
961 }
962 
963 EXPORT_NO_SYMBOLS;
964 MODULE_AUTHOR("Martin von Löwis");
965 MODULE_DESCRIPTION("NTFS driver");
966 #ifdef DEBUG
967 MODULE_PARM(ntdebug, "i");
968 MODULE_PARM_DESC(ntdebug, "Debug level");
969 #endif
970 
971 module_init(init_ntfs_fs)
972 module_exit(exit_ntfs_fs)
973 /*
974  * Local variables:
975  *  c-file-style: "linux"
976  * End:
977  */
978 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.