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