File: /usr/src/linux/fs/ntfs/fs.c
1 /*
2 * fs.c - NTFS driver for Linux 2.4.x
3 *
4 * Legato Systems, Inc. (http://www.legato.com) have sponsored Anton
5 * Altaparmakov to develop NTFS on Linux since June 2001.
6 *
7 * Copyright (C) 1995-1997, 1999 Martin von Löwis
8 * Copyright (C) 1996 Richard Russon
9 * Copyright (C) 1996-1997 Régis Duchesne
10 * Copyright (C) 2000-2001, Anton Altaparmakov (AIA)
11 */
12
13 #include <linux/config.h>
14 #include <linux/errno.h>
15 #include "ntfstypes.h"
16 #include "struct.h"
17 #include "util.h"
18 #include "inode.h"
19 #include "super.h"
20 #include "dir.h"
21 #include "support.h"
22 #include "macros.h"
23 #include "sysctl.h"
24 #include "attr.h"
25 #include <linux/module.h>
26 #include <asm/uaccess.h>
27 #include <linux/locks.h>
28 #include <linux/init.h>
29 #include <linux/smp_lock.h>
30 #include <asm/page.h>
31 #include <linux/nls.h>
32 #include <linux/ntfs_fs.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
58 copy_from_user(dest, p->user, len);
59 p->user += len;
60 p->off += len;
61 }
62 #endif
63
64 /* loff_t is 64 bit signed, so is cool. */
65 static ssize_t ntfs_read(struct file *filp, char *buf, size_t count,loff_t *off)
66 {
67 int error;
68 ntfs_io io;
69 ntfs_attribute *attr;
70 ntfs_inode *ino = NTFS_LINO2NINO(filp->f_dentry->d_inode);
71
72 /* Inode is not properly initialized. */
73 if (!ino)
74 return -EINVAL;
75 ntfs_debug(DEBUG_OTHER, "ntfs_read %x, %Lx, %x ->",
76 (unsigned)ino->i_number, (unsigned long long)*off,
77 (unsigned)count);
78 attr = ntfs_find_attr(ino, ino->vol->at_data, NULL);
79 /* Inode has no unnamed data attribute. */
80 if (!attr) {
81 ntfs_debug(DEBUG_OTHER, "ntfs_read: $DATA not found!\n");
82 return -EINVAL;
83 }
84 if (attr->flags & ATTR_IS_ENCRYPTED)
85 return -EACCES;
86 /* Read the data. */
87 io.fn_put = ntfs_putuser;
88 io.fn_get = 0;
89 io.param = buf;
90 io.size = count;
91 error = ntfs_read_attr(ino, ino->vol->at_data, NULL, *off, &io);
92 if (error && !io.size) {
93 ntfs_debug(DEBUG_OTHER, "ntfs_read: read_attr failed with "
94 "error %i, io size %u.\n", error, io.size);
95 return error;
96 }
97 *off += io.size;
98 ntfs_debug(DEBUG_OTHER, "ntfs_read: finished. read %u bytes.\n",
99 io.size);
100 return io.size;
101 }
102
103 #ifdef CONFIG_NTFS_RW
104 static ssize_t ntfs_write(struct file *filp, const char *buf, size_t count,
105 loff_t *pos)
106 {
107 int err;
108 struct inode *vfs_ino = filp->f_dentry->d_inode;
109 ntfs_inode *ntfs_ino = NTFS_LINO2NINO(vfs_ino);
110 ntfs_attribute *data;
111 ntfs_io io;
112 struct ntfs_getuser_update_vm_s param;
113
114 if (!ntfs_ino)
115 return -EINVAL;
116 ntfs_debug(DEBUG_LINUX, __FUNCTION__ "(): Entering for inode 0x%lx, "
117 "*pos 0x%Lx, count 0x%x.\n", ntfs_ino->i_number, *pos,
118 count);
119 /* Allows to lock fs ro at any time. */
120 if (vfs_ino->i_sb->s_flags & MS_RDONLY)
121 return -EROFS;
122 data = ntfs_find_attr(ntfs_ino, ntfs_ino->vol->at_data, NULL);
123 if (!data)
124 return -EINVAL;
125 /* Evaluating O_APPEND is the file system's job... */
126 if (filp->f_flags & O_APPEND)
127 *pos = vfs_ino->i_size;
128 if (!data->resident && *pos + count > data->allocated) {
129 err = ntfs_extend_attr(ntfs_ino, data, *pos + count);
130 if (err < 0)
131 return err;
132 }
133 param.user = buf;
134 param.ino = vfs_ino;
135 param.off = *pos;
136 io.fn_put = 0;
137 io.fn_get = ntfs_getuser_update_vm;
138 io.param = ¶m;
139 io.size = count;
140 io.do_read = 0;
141 err = ntfs_readwrite_attr(ntfs_ino, data, *pos, &io);
142 ntfs_debug(DEBUG_LINUX, __FUNCTION__ "(): Returning %i\n", -err);
143 if (!err) {
144 *pos += io.size;
145 if (*pos > vfs_ino->i_size)
146 vfs_ino->i_size = *pos;
147 mark_inode_dirty(vfs_ino);
148 return io.size;
149 }
150 return err;
151 }
152 #endif
153
154 struct ntfs_filldir {
155 struct inode *dir;
156 filldir_t filldir;
157 unsigned int type;
158 u32 ph, pl;
159 void *dirent;
160 char *name;
161 int namelen;
162 int ret_code;
163 };
164
165 static int ntfs_printcb(ntfs_u8 *entry, void *param)
166 {
167 unsigned long inum = NTFS_GETU64(entry) & 0xffffffffffff;
168 struct ntfs_filldir *nf = param;
169 u32 flags = NTFS_GETU32(entry + 0x48);
170 char show_sys_files = 0;
171 u8 name_len = NTFS_GETU8(entry + 0x50);
172 u8 name_type = NTFS_GETU8(entry + 0x51);
173 int err;
174 unsigned file_type;
175
176 switch (nf->type) {
177 case ngt_dos:
178 /* Don't display long names. */
179 if (!(name_type & 2))
180 return 0;
181 break;
182 case ngt_nt:
183 /* Don't display short-only names. */
184 if ((name_type & 3) == 2)
185 return 0;
186 break;
187 case ngt_posix:
188 break;
189 case ngt_full:
190 show_sys_files = 1;
191 break;
192 default:
193 BUG();
194 }
195 err = ntfs_encodeuni(NTFS_INO2VOL(nf->dir), (ntfs_u16*)(entry + 0x52),
196 name_len, &nf->name, &nf->namelen);
197 if (err) {
198 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Skipping "
199 "unrepresentable file.\n");
200 err = 0;
201 goto err_ret;
202 }
203 if (!show_sys_files && inum < 0x10UL) {
204 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Skipping system "
205 "file (%s).\n", nf->name);
206 err = 0;
207 goto err_ret;
208 }
209 /* Do not return ".", as this is faked. */
210 if (nf->namelen == 1 && nf->name[0] == '.') {
211 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Skipping \".\"\n");
212 err = 0;
213 goto err_ret;
214 }
215 nf->name[nf->namelen] = 0;
216 if (flags & 0x10000000) /* FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT */
217 file_type = DT_DIR;
218 else
219 file_type = DT_REG;
220 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Calling filldir for %s with "
221 "len %i, f_pos 0x%Lx, inode %lu, %s.\n",
222 nf->name, nf->namelen, (loff_t)(nf->ph << 16) | nf->pl,
223 inum, file_type == DT_DIR ? "DT_DIR" : "DT_REG");
224 /*
225 * Userspace side of filldir expects an off_t rather than an loff_t.
226 * And it also doesn't like the most significant bit being set as it
227 * then considers the value to be negative. Thus this implementation
228 * limits the number of index records to 32766, which should be plenty.
229 */
230 err = nf->filldir(nf->dirent, nf->name, nf->namelen,
231 (loff_t)(nf->ph << 16) | nf->pl, inum, file_type);
232 if (err)
233 nf->ret_code = err;
234 err_ret:
235 nf->namelen = 0;
236 ntfs_free(nf->name);
237 nf->name = NULL;
238 return err;
239 }
240
241 /*
242 * readdir returns '.', then '..', then the directory entries in sequence.
243 * As the root directory contains an entry for itself, '.' is not emulated for
244 * the root directory.
245 */
246 static int ntfs_readdir(struct file* filp, void *dirent, filldir_t filldir)
247 {
248 struct inode *dir = filp->f_dentry->d_inode;
249 int err;
250 struct ntfs_filldir cb;
251
252 cb.ret_code = 0;
253 cb.pl = filp->f_pos & 0xffff;
254 cb.ph = (filp->f_pos >> 16) & 0x7fff;
255 filp->f_pos = (loff_t)(cb.ph << 16) | cb.pl;
256 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Entering for inode %lu, "
257 "f_pos 0x%Lx, i_mode 0x%x, i_count %lu.\n", dir->i_ino,
258 filp->f_pos, (unsigned int)dir->i_mode,
259 atomic_read(&dir->i_count));
260 if (!cb.ph) {
261 /* Start of directory. Emulate "." and "..". */
262 if (!cb.pl) {
263 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Calling "
264 "filldir for . with len 1, f_pos 0x%Lx, "
265 "inode %lu, DT_DIR.\n", filp->f_pos,
266 dir->i_ino);
267 cb.ret_code = filldir(dirent, ".", 1, filp->f_pos,
268 dir->i_ino, DT_DIR);
269 if (cb.ret_code)
270 goto done;
271 cb.pl++;
272 filp->f_pos = (loff_t)(cb.ph << 16) | cb.pl;
273 }
274 if (cb.pl == (u32)1) {
275 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Calling "
276 "filldir for .. with len 2, f_pos 0x%Lx, "
277 "inode %lu, DT_DIR.\n", filp->f_pos,
278 filp->f_dentry->d_parent->d_inode->i_ino);
279 cb.ret_code = filldir(dirent, "..", 2, filp->f_pos,
280 filp->f_dentry->d_parent->d_inode->i_ino,
281 DT_DIR);
282 if (cb.ret_code)
283 goto done;
284 cb.pl++;
285 filp->f_pos = (loff_t)(cb.ph << 16) | cb.pl;
286 }
287 } else if (cb.ph >= 0x7fff)
288 /* End of directory. */
289 goto done;
290 cb.dir = dir;
291 cb.filldir = filldir;
292 cb.dirent = dirent;
293 cb.type = NTFS_INO2VOL(dir)->ngt;
294 do {
295 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Looking for next "
296 "file using ntfs_getdir_unsorted(), f_pos "
297 "0x%Lx.\n", (loff_t)(cb.ph << 16) | cb.pl);
298 err = ntfs_getdir_unsorted(NTFS_LINO2NINO(dir), &cb.ph, &cb.pl,
299 ntfs_printcb, &cb);
300 } while (!err && !cb.ret_code && cb.ph < 0x7fff);
301 filp->f_pos = (loff_t)(cb.ph << 16) | cb.pl;
302 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): After ntfs_getdir_unsorted()"
303 " calls, f_pos 0x%Lx.\n", filp->f_pos);
304 if (!err) {
305 #ifdef DEBUG
306 if (cb.ph != 0x7fff || cb.pl)
307 BUG();
308 done:
309 if (!cb.ret_code)
310 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): EOD, f_pos "
311 "0x%Lx, returning 0.\n", filp->f_pos);
312 else
313 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): filldir "
314 "returned %i, returning 0, f_pos "
315 "0x%Lx.\n", cb.ret_code, filp->f_pos);
316 #else
317 done:
318 #endif
319 return 0;
320 }
321 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Returning %i, f_pos 0x%Lx.\n",
322 err, filp->f_pos);
323 return err;
324 }
325
326 /* Copied from vfat driver. */
327 static int simple_getbool(char *s, int *setval)
328 {
329 if (s) {
330 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
331 *setval = 1;
332 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
333 !strcmp(s, "false"))
334 *setval = 0;
335 else
336 return 0;
337 } else
338 *setval = 1;
339 return 1;
340 }
341
342 /*
343 * This needs to be outside parse_options() otherwise a remount will reset
344 * these unintentionally.
345 */
346 static void init_ntfs_super_block(ntfs_volume* vol)
347 {
348 vol->uid = vol->gid = 0;
349 vol->umask = 0077;
350 vol->ngt = ngt_nt;
351 vol->nls_map = (void*)-1;
352 vol->mft_zone_multiplier = -1;
353 }
354
355 /* Parse the (re)mount options. */
356 static int parse_options(ntfs_volume *vol, char *opt)
357 {
358 char *value; /* Defaults if not specified and !remount. */
359 ntfs_uid_t uid = -1; /* 0, root user only */
360 ntfs_gid_t gid = -1; /* 0, root user only */
361 int umask = -1; /* 0077, owner access only */
362 unsigned int ngt = -1; /* ngt_nt */
363 void *nls_map = NULL; /* Try to load the default NLS. */
364 int use_utf8 = -1; /* If no NLS specified and loading the default
365 NLS failed use utf8. */
366 int mft_zone_mul = -1; /* 1 */
367
368 if (!opt)
369 goto done;
370 for (opt = strtok(opt, ","); opt; opt = strtok(NULL, ",")) {
371 if ((value = strchr(opt, '=')) != NULL)
372 *value ++= '\0';
373 if (strcmp(opt, "uid") == 0) {
374 if (!value || !*value)
375 goto needs_arg;
376 uid = simple_strtoul(value, &value, 0);
377 if (*value) {
378 printk(KERN_ERR "NTFS: uid invalid argument\n");
379 return 0;
380 }
381 } else if (strcmp(opt, "gid") == 0) {
382 if (!value || !*value)
383 goto needs_arg;
384 gid = simple_strtoul(value, &value, 0);
385 if (*value) {
386 printk(KERN_ERR "NTFS: gid invalid argument\n");
387 return 0;
388 }
389 } else if (strcmp(opt, "umask") == 0) {
390 if (!value || !*value)
391 goto needs_arg;
392 umask = simple_strtoul(value, &value, 0);
393 if (*value) {
394 printk(KERN_ERR "NTFS: umask invalid "
395 "argument\n");
396 return 0;
397 }
398 } else if (strcmp(opt, "mft_zone_multiplier") == 0) {
399 unsigned long ul;
400
401 if (!value || !*value)
402 goto needs_arg;
403 ul = simple_strtoul(value, &value, 0);
404 if (*value) {
405 printk(KERN_ERR "NTFS: mft_zone_multiplier "
406 "invalid argument\n");
407 return 0;
408 }
409 if (ul >= 1 && ul <= 4)
410 mft_zone_mul = ul;
411 else {
412 mft_zone_mul = 1;
413 printk(KERN_WARNING "NTFS: mft_zone_multiplier "
414 "out of range. Setting to 1.\n");
415 }
416 } else if (strcmp(opt, "posix") == 0) {
417 int val;
418 if (!value || !*value)
419 goto needs_arg;
420 if (!simple_getbool(value, &val))
421 goto needs_bool;
422 ngt = val ? ngt_posix : ngt_nt;
423 } else if (strcmp(opt, "show_sys_files") == 0) {
424 int val = 0;
425 if (!value || !*value)
426 val = 1;
427 else if (!simple_getbool(value, &val))
428 goto needs_bool;
429 ngt = val ? ngt_full : ngt_nt;
430 } else if (strcmp(opt, "iocharset") == 0) {
431 if (!value || !*value)
432 goto needs_arg;
433 nls_map = load_nls(value);
434 if (!nls_map) {
435 printk(KERN_ERR "NTFS: charset not found");
436 return 0;
437 }
438 } else if (strcmp(opt, "utf8") == 0) {
439 int val = 0;
440 if (!value || !*value)
441 val = 1;
442 else if (!simple_getbool(value, &val))
443 goto needs_bool;
444 use_utf8 = val;
445 } else {
446 printk(KERN_ERR "NTFS: unkown option '%s'\n", opt);
447 return 0;
448 }
449 }
450 done:
451 if (use_utf8 == -1) {
452 /* utf8 was not specified at all. */
453 if (!nls_map) {
454 /*
455 * No NLS was specified. If first mount, load the
456 * default NLS, otherwise don't change the NLS setting.
457 */
458 if (vol->nls_map == (void*)-1)
459 vol->nls_map = load_nls_default();
460 } else {
461 /* If an NLS was already loaded, unload it first. */
462 if (vol->nls_map && vol->nls_map != (void*)-1)
463 unload_nls(vol->nls_map);
464 /* Use the specified NLS. */
465 vol->nls_map = nls_map;
466 }
467 } else {
468 /* utf8 was specified. */
469 if (use_utf8 && nls_map) {
470 unload_nls(nls_map);
471 printk(KERN_ERR "NTFS: utf8 cannot be combined with "
472 "iocharset.\n");
473 return 0;
474 }
475 /* If an NLS was already loaded, unload it first. */
476 if (vol->nls_map && vol->nls_map != (void*)-1)
477 unload_nls(vol->nls_map);
478 if (!use_utf8) {
479 /* utf8 was specified as false. */
480 if (!nls_map)
481 /* No NLS was specified, load the default. */
482 vol->nls_map = load_nls_default();
483 else
484 /* Use the specified NLS. */
485 vol->nls_map = nls_map;
486 } else
487 /* utf8 was specified as true. */
488 vol->nls_map = NULL;
489 }
490 if (uid != -1)
491 vol->uid = uid;
492 if (gid != -1)
493 vol->gid = gid;
494 if (umask != -1)
495 vol->umask = (ntmode_t)umask;
496 if (ngt != -1)
497 vol->ngt = ngt;
498 if (mft_zone_mul != -1) {
499 /* mft_zone_multiplier was specified. */
500 if (vol->mft_zone_multiplier != -1) {
501 /* This is a remount, ignore a change and warn user. */
502 if (vol->mft_zone_multiplier != mft_zone_mul)
503 printk(KERN_WARNING "NTFS: Ignoring changes in "
504 "mft_zone_multiplier on "
505 "remount. If you want to "
506 "change this you need to "
507 "umount and mount again.\n");
508 } else
509 /* Use the specified multiplier. */
510 vol->mft_zone_multiplier = mft_zone_mul;
511 } else if (vol->mft_zone_multiplier == -1)
512 /* No multiplier specified and first mount, so set default. */
513 vol->mft_zone_multiplier = 1;
514 return 1;
515 needs_arg:
516 printk(KERN_ERR "NTFS: %s needs an argument", opt);
517 return 0;
518 needs_bool:
519 printk(KERN_ERR "NTFS: %s needs boolean argument", opt);
520 return 0;
521 }
522
523 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *d)
524 {
525 struct inode *res = 0;
526 char *item = 0;
527 ntfs_iterate_s walk;
528 int err;
529
530 ntfs_debug(DEBUG_NAME1, __FUNCTION__ "(): Looking up %s in directory "
531 "ino 0x%x.\n", d->d_name.name, (unsigned)dir->i_ino);
532 walk.name = NULL;
533 walk.namelen = 0;
534 /* Convert to wide string. */
535 err = ntfs_decodeuni(NTFS_INO2VOL(dir), (char*)d->d_name.name,
536 d->d_name.len, &walk.name, &walk.namelen);
537 if (err)
538 goto err_ret;
539 item = ntfs_malloc(ITEM_SIZE);
540 if (!item) {
541 err = -ENOMEM;
542 goto err_ret;
543 }
544 /* ntfs_getdir will place the directory entry into item, and the first
545 * long long is the MFT record number. */
546 walk.type = BY_NAME;
547 walk.dir = NTFS_LINO2NINO(dir);
548 walk.result = item;
549 if (ntfs_getdir_byname(&walk))
550 res = iget(dir->i_sb, NTFS_GETU32(item));
551 d_add(d, res);
552 ntfs_free(item);
553 ntfs_free(walk.name);
554 /* Always return success, the dcache will handle negative entries. */
555 return NULL;
556 err_ret:
557 ntfs_free(walk.name);
558 return ERR_PTR(err);
559 }
560
561 static struct file_operations ntfs_file_operations = {
562 llseek: generic_file_llseek,
563 read: ntfs_read,
564 #ifdef CONFIG_NTFS_RW
565 write: ntfs_write,
566 #endif
567 open: generic_file_open,
568 };
569
570 static struct inode_operations ntfs_inode_operations;
571
572 #ifdef CONFIG_NTFS_RW
573 static int ntfs_create(struct inode* dir, struct dentry *d, int mode)
574 {
575 struct inode *r = 0;
576 ntfs_inode *ino = 0;
577 ntfs_volume *vol;
578 int error = 0;
579 ntfs_attribute *si;
580
581 r = new_inode(dir->i_sb);
582 if (!r) {
583 error = -ENOMEM;
584 goto fail;
585 }
586 ntfs_debug(DEBUG_OTHER, "ntfs_create %s\n", d->d_name.name);
587 vol = NTFS_INO2VOL(dir);
588 ino = NTFS_LINO2NINO(r);
589 error = ntfs_alloc_file(NTFS_LINO2NINO(dir), ino, (char*)d->d_name.name,
590 d->d_name.len);
591 if (error) {
592 ntfs_error("ntfs_alloc_file FAILED: error = %i", error);
593 goto fail;
594 }
595 /* Not doing this one was causing a huge amount of corruption! Now the
596 * bugger bytes the dust! (-8 (AIA) */
597 r->i_ino = ino->i_number;
598 error = ntfs_update_inode(ino);
599 if (error)
600 goto fail;
601 error = ntfs_update_inode(NTFS_LINO2NINO(dir));
602 if (error)
603 goto fail;
604 r->i_uid = vol->uid;
605 r->i_gid = vol->gid;
606 /* FIXME: dirty? dev? */
607 /* Get the file modification times from the standard information. */
608 si = ntfs_find_attr(ino, vol->at_standard_information, NULL);
609 if (si) {
610 char *attr = si->d.data;
611 r->i_atime = ntfs_ntutc2unixutc(NTFS_GETU64(attr + 0x18));
612 r->i_ctime = ntfs_ntutc2unixutc(NTFS_GETU64(attr));
613 r->i_mtime = ntfs_ntutc2unixutc(NTFS_GETU64(attr + 8));
614 }
615 /* It's not a directory */
616 r->i_op = &ntfs_inode_operations;
617 r->i_fop = &ntfs_file_operations;
618 r->i_mode = S_IFREG | S_IRUGO;
619 #ifdef CONFIG_NTFS_RW
620 r->i_mode |= S_IWUGO;
621 #endif
622 r->i_mode &= ~vol->umask;
623 insert_inode_hash(r);
624 d_instantiate(d, r);
625 return 0;
626 fail:
627 if (r)
628 iput(r);
629 return error;
630 }
631
632 static int _linux_ntfs_mkdir(struct inode *dir, struct dentry* d, int mode)
633 {
634 int error;
635 struct inode *r = 0;
636 ntfs_volume *vol;
637 ntfs_inode *ino;
638 ntfs_attribute *si;
639
640 ntfs_debug (DEBUG_DIR1, "mkdir %s in %x\n", d->d_name.name, dir->i_ino);
641 error = -ENAMETOOLONG;
642 if (d->d_name.len > /* FIXME: */ 255)
643 goto out;
644 error = -EIO;
645 r = new_inode(dir->i_sb);
646 if (!r)
647 goto out;
648 vol = NTFS_INO2VOL(dir);
649 ino = NTFS_LINO2NINO(r);
650 error = ntfs_mkdir(NTFS_LINO2NINO(dir), d->d_name.name, d->d_name.len,
651 ino);
652 if (error)
653 goto out;
654 /* Not doing this one was causing a huge amount of corruption! Now the
655 * bugger bytes the dust! (-8 (AIA) */
656 r->i_ino = ino->i_number;
657 r->i_uid = vol->uid;
658 r->i_gid = vol->gid;
659 si = ntfs_find_attr(ino, vol->at_standard_information, NULL);
660 if (si) {
661 char *attr = si->d.data;
662 r->i_atime = ntfs_ntutc2unixutc(NTFS_GETU64(attr + 0x18));
663 r->i_ctime = ntfs_ntutc2unixutc(NTFS_GETU64(attr));
664 r->i_mtime = ntfs_ntutc2unixutc(NTFS_GETU64(attr + 8));
665 }
666 /* It's a directory. */
667 r->i_op = &ntfs_dir_inode_operations;
668 r->i_fop = &ntfs_dir_operations;
669 r->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
670 #ifdef CONFIG_NTFS_RW
671 r->i_mode |= S_IWUGO;
672 #endif
673 r->i_mode &= ~vol->umask;
674
675 insert_inode_hash(r);
676 d_instantiate(d, r);
677 error = 0;
678 out:
679 ntfs_debug (DEBUG_DIR1, "mkdir returns %d\n", error);
680 return error;
681 }
682 #endif
683
684 static struct file_operations ntfs_dir_operations = {
685 read: generic_read_dir,
686 readdir: ntfs_readdir,
687 };
688
689 static struct inode_operations ntfs_dir_inode_operations = {
690 lookup: ntfs_lookup,
691 #ifdef CONFIG_NTFS_RW
692 create: ntfs_create,
693 mkdir: _linux_ntfs_mkdir,
694 #endif
695 };
696
697 /* ntfs_read_inode() is called by the Virtual File System (the kernel layer
698 * that deals with filesystems) when iget is called requesting an inode not
699 * already present in the inode table. Typically filesystems have separate
700 * inode_operations for directories, files and symlinks. */
701 static void ntfs_read_inode(struct inode* inode)
702 {
703 ntfs_volume *vol;
704 ntfs_inode *ino;
705 ntfs_attribute *data;
706 ntfs_attribute *si;
707
708 vol = NTFS_INO2VOL(inode);
709 inode->i_mode = 0;
710 ntfs_debug(DEBUG_OTHER, "ntfs_read_inode 0x%lx\n", inode->i_ino);
711 switch (inode->i_ino) {
712 /* Those are loaded special files. */
713 case FILE_Mft:
714 if (!vol->mft_ino || ((vol->ino_flags & 1) == 0))
715 goto sys_file_error;
716 ntfs_memcpy(&inode->u.ntfs_i, vol->mft_ino, sizeof(ntfs_inode));
717 ino = vol->mft_ino;
718 vol->mft_ino = &inode->u.ntfs_i;
719 vol->ino_flags &= ~1;
720 ntfs_free(ino);
721 ino = vol->mft_ino;
722 ntfs_debug(DEBUG_OTHER, "Opening $MFT!\n");
723 break;
724 case FILE_MftMirr:
725 if (!vol->mftmirr || ((vol->ino_flags & 2) == 0))
726 goto sys_file_error;
727 ntfs_memcpy(&inode->u.ntfs_i, vol->mftmirr, sizeof(ntfs_inode));
728 ino = vol->mftmirr;
729 vol->mftmirr = &inode->u.ntfs_i;
730 vol->ino_flags &= ~2;
731 ntfs_free(ino);
732 ino = vol->mftmirr;
733 ntfs_debug(DEBUG_OTHER, "Opening $MFTMirr!\n");
734 break;
735 case FILE_BitMap:
736 if (!vol->bitmap || ((vol->ino_flags & 4) == 0))
737 goto sys_file_error;
738 ntfs_memcpy(&inode->u.ntfs_i, vol->bitmap, sizeof(ntfs_inode));
739 ino = vol->bitmap;
740 vol->bitmap = &inode->u.ntfs_i;
741 vol->ino_flags &= ~4;
742 ntfs_free(ino);
743 ino = vol->bitmap;
744 ntfs_debug(DEBUG_OTHER, "Opening $Bitmap!\n");
745 break;
746 case FILE_LogFile ... FILE_AttrDef:
747 /* No need to log root directory accesses. */
748 case FILE_Boot ... FILE_UpCase:
749 ntfs_debug(DEBUG_OTHER, "Opening system file %i!\n",
750 inode->i_ino);
751 default:
752 ino = &inode->u.ntfs_i;
753 if (!ino || ntfs_init_inode(ino, NTFS_INO2VOL(inode),
754 inode->i_ino))
755 {
756 ntfs_debug(DEBUG_OTHER, "NTFS: Error loading inode "
757 "0x%x\n", (unsigned int)inode->i_ino);
758 return;
759 }
760 }
761 /* Set uid/gid from mount options */
762 inode->i_uid = vol->uid;
763 inode->i_gid = vol->gid;
764 inode->i_nlink = 1;
765 /* Use the size of the data attribute as file size */
766 data = ntfs_find_attr(ino, vol->at_data, NULL);
767 if (!data)
768 inode->i_size = 0;
769 else
770 inode->i_size = data->size;
771 /* Get the file modification times from the standard information. */
772 si = ntfs_find_attr(ino, vol->at_standard_information, NULL);
773 if (si) {
774 char *attr = si->d.data;
775 inode->i_atime = ntfs_ntutc2unixutc(NTFS_GETU64(attr + 0x18));
776 inode->i_ctime = ntfs_ntutc2unixutc(NTFS_GETU64(attr));
777 inode->i_mtime = ntfs_ntutc2unixutc(NTFS_GETU64(attr + 8));
778 }
779 /* If it has an index root, it's a directory. */
780 if (ntfs_find_attr(ino, vol->at_index_root, "$I30")) {
781 ntfs_attribute *at;
782 at = ntfs_find_attr(ino, vol->at_index_allocation, "$I30");
783 inode->i_size = at ? at->size : 0;
784 inode->i_op = &ntfs_dir_inode_operations;
785 inode->i_fop = &ntfs_dir_operations;
786 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
787 } else {
788 inode->i_op = &ntfs_inode_operations;
789 inode->i_fop = &ntfs_file_operations;
790 inode->i_mode = S_IFREG | S_IRUGO;
791 }
792 #ifdef CONFIG_NTFS_RW
793 if (!data || !(data->flags & (ATTR_IS_COMPRESSED | ATTR_IS_ENCRYPTED)))
794 inode->i_mode |= S_IWUGO;
795 #endif
796 inode->i_mode &= ~vol->umask;
797 return;
798 sys_file_error:
799 ntfs_error("Critical error. Tried to call ntfs_read_inode() before we "
800 "have completed read_super() or VFS error.\n");
801 // FIXME: Should we panic() at this stage?
802 }
803
804 #ifdef CONFIG_NTFS_RW
805 static void ntfs_write_inode(struct inode *ino, int unused)
806 {
807 lock_kernel();
808 ntfs_debug(DEBUG_LINUX, "ntfs_write_inode 0x%x\n", ino->i_ino);
809 ntfs_update_inode(NTFS_LINO2NINO(ino));
810 unlock_kernel();
811 }
812 #endif
813
814 static void _ntfs_clear_inode(struct inode *inode)
815 {
816 ntfs_inode *ino;
817 ntfs_volume *vol;
818
819 lock_kernel();
820 ntfs_debug(DEBUG_OTHER, "_ntfs_clear_inode 0x%x\n", inode->i_ino);
821 vol = NTFS_INO2VOL(inode);
822 if (!vol)
823 ntfs_error("_ntfs_clear_inode: vol = NTFS_INO2VOL(inode) is "
824 "NULL.\n");
825 switch (inode->i_ino) {
826 case FILE_Mft:
827 if (vol->mft_ino && ((vol->ino_flags & 1) == 0)) {
828 ino = (ntfs_inode*)ntfs_malloc(sizeof(ntfs_inode));
829 ntfs_memcpy(ino, &inode->u.ntfs_i, sizeof(ntfs_inode));
830 vol->mft_ino = ino;
831 vol->ino_flags |= 1;
832 goto unl_out;
833 }
834 break;
835 case FILE_MftMirr:
836 if (vol->mftmirr && ((vol->ino_flags & 2) == 0)) {
837 ino = (ntfs_inode*)ntfs_malloc(sizeof(ntfs_inode));
838 ntfs_memcpy(ino, &inode->u.ntfs_i, sizeof(ntfs_inode));
839 vol->mftmirr = ino;
840 vol->ino_flags |= 2;
841 goto unl_out;
842 }
843 break;
844 case FILE_BitMap:
845 if (vol->bitmap && ((vol->ino_flags & 4) == 0)) {
846 ino = (ntfs_inode*)ntfs_malloc(sizeof(ntfs_inode));
847 ntfs_memcpy(ino, &inode->u.ntfs_i, sizeof(ntfs_inode));
848 vol->bitmap = ino;
849 vol->ino_flags |= 4;
850 goto unl_out;
851 }
852 break;
853 default:
854 /* Nothing. Just clear the inode and exit. */
855 }
856 ntfs_clear_inode(&inode->u.ntfs_i);
857 unl_out:
858 unlock_kernel();
859 return;
860 }
861
862 /* Called when umounting a filesystem by do_umount() in fs/super.c. */
863 static void ntfs_put_super(struct super_block *sb)
864 {
865 ntfs_volume *vol;
866
867 ntfs_debug(DEBUG_OTHER, "ntfs_put_super\n");
868 vol = NTFS_SB2VOL(sb);
869 ntfs_release_volume(vol);
870 if (vol->nls_map)
871 unload_nls(vol->nls_map);
872 ntfs_debug(DEBUG_OTHER, "ntfs_put_super: done\n");
873 }
874
875 /* Called by the kernel when asking for stats. */
876 static int ntfs_statfs(struct super_block *sb, struct statfs *sf)
877 {
878 struct inode *mft;
879 ntfs_volume *vol;
880 __s64 size;
881 int error;
882
883 ntfs_debug(DEBUG_OTHER, "ntfs_statfs\n");
884 vol = NTFS_SB2VOL(sb);
885 sf->f_type = NTFS_SUPER_MAGIC;
886 sf->f_bsize = vol->cluster_size;
887 error = ntfs_get_volumesize(NTFS_SB2VOL(sb), &size);
888 if (error)
889 return error;
890 sf->f_blocks = size; /* Volumesize is in clusters. */
891 size = (__s64)ntfs_get_free_cluster_count(vol->bitmap);
892 /* Just say zero if the call failed. */
893 if (size < 0LL)
894 size = 0;
895 sf->f_bfree = sf->f_bavail = size;
896 ntfs_debug(DEBUG_OTHER, "ntfs_statfs: calling mft = iget(sb, "
897 "FILE_Mft)\n");
898 mft = iget(sb, FILE_Mft);
899 ntfs_debug(DEBUG_OTHER, "ntfs_statfs: iget(sb, FILE_Mft) returned "
900 "0x%x\n", mft);
901 if (!mft)
902 return -EIO;
903 sf->f_files = mft->i_size >> vol->mft_record_size_bits;
904 ntfs_debug(DEBUG_OTHER, "ntfs_statfs: calling iput(mft)\n");
905 iput(mft);
906 /* Should be read from volume. */
907 sf->f_namelen = 255;
908 return 0;
909 }
910
911 /* Called when remounting a filesystem by do_remount_sb() in fs/super.c. */
912 static int ntfs_remount_fs(struct super_block *sb, int *flags, char *options)
913 {
914 if (!parse_options(NTFS_SB2VOL(sb), options))
915 return -EINVAL;
916 return 0;
917 }
918
919 /* Define the super block operation that are implemented */
920 static struct super_operations ntfs_super_operations = {
921 read_inode: ntfs_read_inode,
922 #ifdef CONFIG_NTFS_RW
923 write_inode: ntfs_write_inode,
924 #endif
925 put_super: ntfs_put_super,
926 statfs: ntfs_statfs,
927 remount_fs: ntfs_remount_fs,
928 clear_inode: _ntfs_clear_inode,
929 };
930
931 /**
932 * is_boot_sector_ntfs - check an NTFS boot sector for validity
933 * @b: buffer containing bootsector to check
934 *
935 * Check whether @b contains a valid NTFS boot sector.
936 * Return 1 if @b is a valid NTFS bootsector or 0 if not.
937 */
938 static int is_boot_sector_ntfs(ntfs_u8 *b)
939 {
940 ntfs_u32 i;
941
942 /* FIXME: We don't use checksumming yet as NT4(SP6a) doesn't either...
943 * But we might as well have the code ready to do it. (AIA) */
944 #if 0
945 /* Calculate the checksum. */
946 if (b < b + 0x50) {
947 ntfs_u32 *u;
948 ntfs_u32 *bi = (ntfs_u32 *)(b + 0x50);
949
950 for (u = bi, i = 0; u < bi; ++u)
951 i += NTFS_GETU32(*u);
952 }
953 #endif
954 /* Check magic is "NTFS " */
955 if (b[3] != 0x4e) goto not_ntfs;
956 if (b[4] != 0x54) goto not_ntfs;
957 if (b[5] != 0x46) goto not_ntfs;
958 if (b[6] != 0x53) goto not_ntfs;
959 for (i = 7; i < 0xb; ++i)
960 if (b[i] != 0x20) goto not_ntfs;
961 /* Check bytes per sector value is between 512 and 4096. */
962 if (b[0xb] != 0) goto not_ntfs;
963 if (b[0xc] > 0x10) goto not_ntfs;
964 /* Check sectors per cluster value is valid. */
965 switch (b[0xd]) {
966 case 1: case 2: case 4: case 8: case 16:
967 case 32: case 64: case 128:
968 break;
969 default:
970 goto not_ntfs;
971 }
972 /* Check reserved sectors value and four other fields are zero. */
973 for (i = 0xe; i < 0x15; ++i)
974 if (b[i] != 0) goto not_ntfs;
975 if (b[0x16] != 0) goto not_ntfs;
976 if (b[0x17] != 0) goto not_ntfs;
977 for (i = 0x20; i < 0x24; ++i)
978 if (b[i] != 0) goto not_ntfs;
979 /* Check clusters per file record segment value is valid. */
980 if (b[0x40] < 0xe1 || b[0x40] > 0xf7) {
981 switch (b[0x40]) {
982 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
983 break;
984 default:
985 goto not_ntfs;
986 }
987 }
988 /* Check clusters per index block value is valid. */
989 if (b[0x44] < 0xe1 || b[0x44] > 0xf7) {
990 switch (b[0x44]) {
991 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
992 break;
993 default:
994 goto not_ntfs;
995 }
996 }
997 return 1;
998 not_ntfs:
999 return 0;
1000 }
1001
1002 /* Called to mount a filesystem by read_super() in fs/super.c.
1003 * Return a super block, the main structure of a filesystem.
1004 *
1005 * NOTE : Don't store a pointer to an option, as the page containing the
1006 * options is freed after ntfs_read_super() returns.
1007 *
1008 * NOTE : A context switch can happen in kernel code only if the code blocks
1009 * (= calls schedule() in kernel/sched.c). */
1010 struct super_block *ntfs_read_super(struct super_block *sb, void *options,
1011 int silent)
1012 {
1013 ntfs_volume *vol;
1014 struct buffer_head *bh;
1015 int i, to_read;
1016
1017 ntfs_debug(DEBUG_OTHER, "ntfs_read_super\n");
1018 vol = NTFS_SB2VOL(sb);
1019 init_ntfs_super_block(vol);
1020 if (!parse_options(vol, (char*)options))
1021 goto ntfs_read_super_vol;
1022 /* Assume a 512 bytes block device for now. */
1023 set_blocksize(sb->s_dev, 512);
1024 /* Read the super block (boot block). */
1025 if (!(bh = bread(sb->s_dev, 0, 512))) {
1026 ntfs_error("Reading super block failed\n");
1027 goto ntfs_read_super_unl;
1028 }
1029 ntfs_debug(DEBUG_OTHER, "Done reading boot block\n");
1030 /* Check for 'NTFS' magic number */
1031 if (!is_boot_sector_ntfs(bh->b_data)) {
1032 ntfs_debug(DEBUG_OTHER, "Not a NTFS volume\n");
1033 bforget(bh);
1034 goto ntfs_read_super_unl;
1035 }
1036 ntfs_debug(DEBUG_OTHER, "Going to init volume\n");
1037 if (ntfs_init_volume(vol, bh->b_data) < 0) {
1038 ntfs_debug(DEBUG_OTHER, "Init volume failed.\n");
1039 bforget(bh);
1040 goto ntfs_read_super_unl;
1041 }
1042 ntfs_debug(DEBUG_OTHER, "$Mft at cluster 0x%lx\n", vol->mft_lcn);
1043 bforget(bh);
1044 NTFS_SB(vol) = sb;
1045 if (vol->cluster_size > PAGE_SIZE) {
1046 ntfs_error("Partition cluster size is not supported yet (it "
1047 "is > max kernel blocksize).\n");
1048 goto ntfs_read_super_unl;
1049 }
1050 ntfs_debug(DEBUG_OTHER, "Done to init volume\n");
1051 /* Inform the kernel that a device block is a NTFS cluster. */
1052 sb->s_blocksize = vol->cluster_size;
1053 for (i = sb->s_blocksize, sb->s_blocksize_bits = 0; i != 1; i >>= 1)
1054 sb->s_blocksize_bits++;
1055 set_blocksize(sb->s_dev, sb->s_blocksize);
1056 ntfs_debug(DEBUG_OTHER, "set_blocksize\n");
1057 /* Allocate an MFT record (MFT record can be smaller than a cluster). */
1058 i = vol->cluster_size;
1059 if (i < vol->mft_record_size)
1060 i = vol->mft_record_size;
1061 if (!(vol->mft = ntfs_malloc(i)))
1062 goto ntfs_read_super_unl;
1063
1064 /* Read at least the MFT record for $Mft. */
1065 to_read = vol->mft_clusters_per_record;
1066 if (to_read < 1)
1067 to_read = 1;
1068 for (i = 0; i < to_read; i++) {
1069 if (!(bh = bread(sb->s_dev, vol->mft_lcn + i,
1070 vol->cluster_size))) {
1071 ntfs_error("Could not read $Mft record 0\n");
1072 goto ntfs_read_super_mft;
1073 }
1074 ntfs_memcpy(vol->mft + ((__s64)i << vol->cluster_size_bits),
1075 bh->b_data, vol->cluster_size);
1076 brelse(bh);
1077 ntfs_debug(DEBUG_OTHER, "Read cluster 0x%x\n",
1078 vol->mft_lcn + i);
1079 }
1080 /* Check and fixup this MFT record */
1081 if (!ntfs_check_mft_record(vol, vol->mft)){
1082 ntfs_error("Invalid $Mft record 0\n");
1083 goto ntfs_read_super_mft;
1084 }
1085 /* Inform the kernel about which super operations are available. */
1086 sb->s_op = &ntfs_super_operations;
1087 sb->s_magic = NTFS_SUPER_MAGIC;
1088 sb->s_maxbytes = ~0ULL >> 1;
1089 ntfs_debug(DEBUG_OTHER, "Reading special files\n");
1090 if (ntfs_load_special_files(vol)) {
1091 ntfs_error("Error loading special files\n");
1092 goto ntfs_read_super_mft;
1093 }
1094 ntfs_debug(DEBUG_OTHER, "Getting RootDir\n");
1095 /* Get the root directory. */
1096 if (!(sb->s_root = d_alloc_root(iget(sb, FILE_root)))) {
1097 ntfs_error("Could not get root dir inode\n");
1098 goto ntfs_read_super_mft;
1099 }
1100 ntfs_read_super_ret:
1101 ntfs_debug(DEBUG_OTHER, "read_super: done\n");
1102 return sb;
1103 ntfs_read_super_mft:
1104 ntfs_free(vol->mft);
1105 ntfs_read_super_unl:
1106 ntfs_read_super_vol:
1107 sb = NULL;
1108 goto ntfs_read_super_ret;
1109 }
1110
1111 /* Define the filesystem */
1112 static DECLARE_FSTYPE_DEV(ntfs_fs_type, "ntfs", ntfs_read_super);
1113
1114 static int __init init_ntfs_fs(void)
1115 {
1116 /* Comment this if you trust klogd. There are reasons not to trust it */
1117 #if defined(DEBUG) && !defined(MODULE)
1118 console_verbose();
1119 #endif
1120 printk(KERN_NOTICE "NTFS driver v" NTFS_VERSION " [Flags: R/"
1121 #ifdef CONFIG_NTFS_RW
1122 "W"
1123 #else
1124 "O"
1125 #endif
1126 #ifdef DEBUG
1127 " DEBUG"
1128 #endif
1129 #ifdef MODULE
1130 " MODULE"
1131 #endif
1132 "]\n");
1133 SYSCTL(1);
1134 ntfs_debug(DEBUG_OTHER, "registering %s\n", ntfs_fs_type.name);
1135 /* Add this filesystem to the kernel table of filesystems. */
1136 return register_filesystem(&ntfs_fs_type);
1137 }
1138
1139 static void __exit exit_ntfs_fs(void)
1140 {
1141 SYSCTL(0);
1142 ntfs_debug(DEBUG_OTHER, "unregistering %s\n", ntfs_fs_type.name);
1143 unregister_filesystem(&ntfs_fs_type);
1144 }
1145
1146 EXPORT_NO_SYMBOLS;
1147 /*
1148 * Not strictly true. The driver was written originally by Martin von Löwis.
1149 * I am just maintaining and rewriting it.
1150 */
1151 MODULE_AUTHOR("Anton Altaparmakov <aia21@cus.cam.ac.uk>");
1152 MODULE_DESCRIPTION("Linux NTFS driver");
1153 #ifdef DEBUG
1154 MODULE_PARM(ntdebug, "i");
1155 MODULE_PARM_DESC(ntdebug, "Debug level");
1156 #endif
1157
1158 module_init(init_ntfs_fs)
1159 module_exit(exit_ntfs_fs)
1160
1161