aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ntfs/super.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ntfs/super.c')
-rw-r--r--fs/ntfs/super.c692
1 files changed, 542 insertions, 150 deletions
diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index 212a3d0f2073..41aa8eb6755b 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project. 2 * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
3 * 3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov 4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * Copyright (c) 2001,2002 Richard Russon 5 * Copyright (c) 2001,2002 Richard Russon
6 * 6 *
7 * This program/include file is free software; you can redistribute it and/or 7 * This program/include file is free software; you can redistribute it and/or
@@ -34,14 +34,16 @@
34#include "sysctl.h" 34#include "sysctl.h"
35#include "logfile.h" 35#include "logfile.h"
36#include "quota.h" 36#include "quota.h"
37#include "usnjrnl.h"
37#include "dir.h" 38#include "dir.h"
38#include "debug.h" 39#include "debug.h"
39#include "index.h" 40#include "index.h"
40#include "aops.h" 41#include "aops.h"
42#include "layout.h"
41#include "malloc.h" 43#include "malloc.h"
42#include "ntfs.h" 44#include "ntfs.h"
43 45
44/* Number of mounted file systems which have compression enabled. */ 46/* Number of mounted filesystems which have compression enabled. */
45static unsigned long ntfs_nr_compression_users; 47static unsigned long ntfs_nr_compression_users;
46 48
47/* A global default upcase table and a corresponding reference count. */ 49/* A global default upcase table and a corresponding reference count. */
@@ -102,7 +104,7 @@ static BOOL parse_options(ntfs_volume *vol, char *opt)
102 gid_t gid = (gid_t)-1; 104 gid_t gid = (gid_t)-1;
103 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1; 105 mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
104 int mft_zone_multiplier = -1, on_errors = -1; 106 int mft_zone_multiplier = -1, on_errors = -1;
105 int show_sys_files = -1, case_sensitive = -1; 107 int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
106 struct nls_table *nls_map = NULL, *old_nls; 108 struct nls_table *nls_map = NULL, *old_nls;
107 109
108 /* I am lazy... (-8 */ 110 /* I am lazy... (-8 */
@@ -162,6 +164,7 @@ static BOOL parse_options(ntfs_volume *vol, char *opt)
162 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE) 164 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
163 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files) 165 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
164 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive) 166 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
167 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
165 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors, 168 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
166 on_errors_arr) 169 on_errors_arr)
167 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes")) 170 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
@@ -291,6 +294,21 @@ no_mount_options:
291 else 294 else
292 NVolClearCaseSensitive(vol); 295 NVolClearCaseSensitive(vol);
293 } 296 }
297 if (disable_sparse != -1) {
298 if (disable_sparse)
299 NVolClearSparseEnabled(vol);
300 else {
301 if (!NVolSparseEnabled(vol) &&
302 vol->major_ver && vol->major_ver < 3)
303 ntfs_warning(vol->sb, "Not enabling sparse "
304 "support due to NTFS volume "
305 "version %i.%i (need at least "
306 "version 3.0).", vol->major_ver,
307 vol->minor_ver);
308 else
309 NVolSetSparseEnabled(vol);
310 }
311 }
294 return TRUE; 312 return TRUE;
295needs_arg: 313needs_arg:
296 ntfs_error(vol->sb, "The %s option requires an argument.", p); 314 ntfs_error(vol->sb, "The %s option requires an argument.", p);
@@ -480,6 +498,12 @@ static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
480 NVolSetErrors(vol); 498 NVolSetErrors(vol);
481 return -EROFS; 499 return -EROFS;
482 } 500 }
501 if (!ntfs_stamp_usnjrnl(vol)) {
502 ntfs_error(sb, "Failed to stamp transation log "
503 "($UsnJrnl)%s", es);
504 NVolSetErrors(vol);
505 return -EROFS;
506 }
483 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) { 507 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
484 /* Remounting read-only. */ 508 /* Remounting read-only. */
485 if (!NVolErrors(vol)) { 509 if (!NVolErrors(vol)) {
@@ -516,16 +540,19 @@ static BOOL is_boot_sector_ntfs(const struct super_block *sb,
516{ 540{
517 /* 541 /*
518 * Check that checksum == sum of u32 values from b to the checksum 542 * Check that checksum == sum of u32 values from b to the checksum
519 * field. If checksum is zero, no checking is done. 543 * field. If checksum is zero, no checking is done. We will work when
544 * the checksum test fails, since some utilities update the boot sector
545 * ignoring the checksum which leaves the checksum out-of-date. We
546 * report a warning if this is the case.
520 */ 547 */
521 if ((void*)b < (void*)&b->checksum && b->checksum) { 548 if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
522 le32 *u; 549 le32 *u;
523 u32 i; 550 u32 i;
524 551
525 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u) 552 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
526 i += le32_to_cpup(u); 553 i += le32_to_cpup(u);
527 if (le32_to_cpu(b->checksum) != i) 554 if (le32_to_cpu(b->checksum) != i)
528 goto not_ntfs; 555 ntfs_warning(sb, "Invalid boot sector checksum.");
529 } 556 }
530 /* Check OEMidentifier is "NTFS " */ 557 /* Check OEMidentifier is "NTFS " */
531 if (b->oem_id != magicNTFS) 558 if (b->oem_id != magicNTFS)
@@ -541,9 +568,9 @@ static BOOL is_boot_sector_ntfs(const struct super_block *sb,
541 default: 568 default:
542 goto not_ntfs; 569 goto not_ntfs;
543 } 570 }
544 /* Check the cluster size is not above 65536 bytes. */ 571 /* Check the cluster size is not above the maximum (64kiB). */
545 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) * 572 if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
546 b->bpb.sectors_per_cluster > 0x10000) 573 b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
547 goto not_ntfs; 574 goto not_ntfs;
548 /* Check reserved/unused fields are really zero. */ 575 /* Check reserved/unused fields are really zero. */
549 if (le16_to_cpu(b->bpb.reserved_sectors) || 576 if (le16_to_cpu(b->bpb.reserved_sectors) ||
@@ -575,7 +602,7 @@ static BOOL is_boot_sector_ntfs(const struct super_block *sb,
575 * many BIOSes will refuse to boot from a bootsector if the magic is 602 * many BIOSes will refuse to boot from a bootsector if the magic is
576 * incorrect, so we emit a warning. 603 * incorrect, so we emit a warning.
577 */ 604 */
578 if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55)) 605 if (!silent && b->end_of_sector_marker != const_cpu_to_le16(0xaa55))
579 ntfs_warning(sb, "Invalid end of sector marker."); 606 ntfs_warning(sb, "Invalid end of sector marker.");
580 return TRUE; 607 return TRUE;
581not_ntfs: 608not_ntfs:
@@ -967,6 +994,7 @@ static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
967 tmp_ni = NTFS_I(tmp_ino); 994 tmp_ni = NTFS_I(tmp_ino);
968 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */ 995 /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
969 NInoSetMstProtected(tmp_ni); 996 NInoSetMstProtected(tmp_ni);
997 NInoSetSparseDisabled(tmp_ni);
970 /* 998 /*
971 * Set up our little cheat allowing us to reuse the async read io 999 * Set up our little cheat allowing us to reuse the async read io
972 * completion handler for directories. 1000 * completion handler for directories.
@@ -990,12 +1018,12 @@ static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
990 */ 1018 */
991static BOOL check_mft_mirror(ntfs_volume *vol) 1019static BOOL check_mft_mirror(ntfs_volume *vol)
992{ 1020{
993 unsigned long index;
994 struct super_block *sb = vol->sb; 1021 struct super_block *sb = vol->sb;
995 ntfs_inode *mirr_ni; 1022 ntfs_inode *mirr_ni;
996 struct page *mft_page, *mirr_page; 1023 struct page *mft_page, *mirr_page;
997 u8 *kmft, *kmirr; 1024 u8 *kmft, *kmirr;
998 runlist_element *rl, rl2[2]; 1025 runlist_element *rl, rl2[2];
1026 pgoff_t index;
999 int mrecs_per_page, i; 1027 int mrecs_per_page, i;
1000 1028
1001 ntfs_debug("Entering."); 1029 ntfs_debug("Entering.");
@@ -1122,11 +1150,130 @@ static BOOL load_and_check_logfile(ntfs_volume *vol)
1122 /* ntfs_check_logfile() will have displayed error output. */ 1150 /* ntfs_check_logfile() will have displayed error output. */
1123 return FALSE; 1151 return FALSE;
1124 } 1152 }
1153 NInoSetSparseDisabled(NTFS_I(tmp_ino));
1125 vol->logfile_ino = tmp_ino; 1154 vol->logfile_ino = tmp_ino;
1126 ntfs_debug("Done."); 1155 ntfs_debug("Done.");
1127 return TRUE; 1156 return TRUE;
1128} 1157}
1129 1158
1159#define NTFS_HIBERFIL_HEADER_SIZE 4096
1160
1161/**
1162 * check_windows_hibernation_status - check if Windows is suspended on a volume
1163 * @vol: ntfs super block of device to check
1164 *
1165 * Check if Windows is hibernated on the ntfs volume @vol. This is done by
1166 * looking for the file hiberfil.sys in the root directory of the volume. If
1167 * the file is not present Windows is definitely not suspended.
1168 *
1169 * If hiberfil.sys exists and is less than 4kiB in size it means Windows is
1170 * definitely suspended (this volume is not the system volume). Caveat: on a
1171 * system with many volumes it is possible that the < 4kiB check is bogus but
1172 * for now this should do fine.
1173 *
1174 * If hiberfil.sys exists and is larger than 4kiB in size, we need to read the
1175 * hiberfil header (which is the first 4kiB). If this begins with "hibr",
1176 * Windows is definitely suspended. If it is completely full of zeroes,
1177 * Windows is definitely not hibernated. Any other case is treated as if
1178 * Windows is suspended. This caters for the above mentioned caveat of a
1179 * system with many volumes where no "hibr" magic would be present and there is
1180 * no zero header.
1181 *
1182 * Return 0 if Windows is not hibernated on the volume, >0 if Windows is
1183 * hibernated on the volume, and -errno on error.
1184 */
1185static int check_windows_hibernation_status(ntfs_volume *vol)
1186{
1187 MFT_REF mref;
1188 struct inode *vi;
1189 ntfs_inode *ni;
1190 struct page *page;
1191 u32 *kaddr, *kend;
1192 ntfs_name *name = NULL;
1193 int ret = 1;
1194 static const ntfschar hiberfil[13] = { const_cpu_to_le16('h'),
1195 const_cpu_to_le16('i'), const_cpu_to_le16('b'),
1196 const_cpu_to_le16('e'), const_cpu_to_le16('r'),
1197 const_cpu_to_le16('f'), const_cpu_to_le16('i'),
1198 const_cpu_to_le16('l'), const_cpu_to_le16('.'),
1199 const_cpu_to_le16('s'), const_cpu_to_le16('y'),
1200 const_cpu_to_le16('s'), 0 };
1201
1202 ntfs_debug("Entering.");
1203 /*
1204 * Find the inode number for the hibernation file by looking up the
1205 * filename hiberfil.sys in the root directory.
1206 */
1207 down(&vol->root_ino->i_sem);
1208 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
1209 &name);
1210 up(&vol->root_ino->i_sem);
1211 if (IS_ERR_MREF(mref)) {
1212 ret = MREF_ERR(mref);
1213 /* If the file does not exist, Windows is not hibernated. */
1214 if (ret == -ENOENT) {
1215 ntfs_debug("hiberfil.sys not present. Windows is not "
1216 "hibernated on the volume.");
1217 return 0;
1218 }
1219 /* A real error occured. */
1220 ntfs_error(vol->sb, "Failed to find inode number for "
1221 "hiberfil.sys.");
1222 return ret;
1223 }
1224 /* We do not care for the type of match that was found. */
1225 kfree(name);
1226 /* Get the inode. */
1227 vi = ntfs_iget(vol->sb, MREF(mref));
1228 if (IS_ERR(vi) || is_bad_inode(vi)) {
1229 if (!IS_ERR(vi))
1230 iput(vi);
1231 ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
1232 return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
1233 }
1234 if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
1235 ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). "
1236 "Windows is hibernated on the volume. This "
1237 "is not the system volume.", i_size_read(vi));
1238 goto iput_out;
1239 }
1240 ni = NTFS_I(vi);
1241 page = ntfs_map_page(vi->i_mapping, 0);
1242 if (IS_ERR(page)) {
1243 ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
1244 ret = PTR_ERR(page);
1245 goto iput_out;
1246 }
1247 kaddr = (u32*)page_address(page);
1248 if (*(le32*)kaddr == const_cpu_to_le32(0x72626968)/*'hibr'*/) {
1249 ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is "
1250 "hibernated on the volume. This is the "
1251 "system volume.");
1252 goto unm_iput_out;
1253 }
1254 kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
1255 do {
1256 if (unlikely(*kaddr)) {
1257 ntfs_debug("hiberfil.sys is larger than 4kiB "
1258 "(0x%llx), does not contain the "
1259 "\"hibr\" magic, and does not have a "
1260 "zero header. Windows is hibernated "
1261 "on the volume. This is not the "
1262 "system volume.", i_size_read(vi));
1263 goto unm_iput_out;
1264 }
1265 } while (++kaddr < kend);
1266 ntfs_debug("hiberfil.sys contains a zero header. Windows is not "
1267 "hibernated on the volume. This is the system "
1268 "volume.");
1269 ret = 0;
1270unm_iput_out:
1271 ntfs_unmap_page(page);
1272iput_out:
1273 iput(vi);
1274 return ret;
1275}
1276
1130/** 1277/**
1131 * load_and_init_quota - load and setup the quota file for a volume if present 1278 * load_and_init_quota - load and setup the quota file for a volume if present
1132 * @vol: ntfs super block describing device whose quota file to load 1279 * @vol: ntfs super block describing device whose quota file to load
@@ -1175,8 +1322,7 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1175 return FALSE; 1322 return FALSE;
1176 } 1323 }
1177 /* We do not care for the type of match that was found. */ 1324 /* We do not care for the type of match that was found. */
1178 if (name) 1325 kfree(name);
1179 kfree(name);
1180 /* Get the inode. */ 1326 /* Get the inode. */
1181 tmp_ino = ntfs_iget(vol->sb, MREF(mref)); 1327 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1182 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) { 1328 if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
@@ -1198,6 +1344,167 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1198} 1344}
1199 1345
1200/** 1346/**
1347 * load_and_init_usnjrnl - load and setup the transaction log if present
1348 * @vol: ntfs super block describing device whose usnjrnl file to load
1349 *
1350 * Return TRUE on success or FALSE on error.
1351 *
1352 * If $UsnJrnl is not present or in the process of being disabled, we set
1353 * NVolUsnJrnlStamped() and return success.
1354 *
1355 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1356 * i.e. transaction logging has only just been enabled or the journal has been
1357 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1358 * and return success.
1359 */
1360static BOOL load_and_init_usnjrnl(ntfs_volume *vol)
1361{
1362 MFT_REF mref;
1363 struct inode *tmp_ino;
1364 ntfs_inode *tmp_ni;
1365 struct page *page;
1366 ntfs_name *name = NULL;
1367 USN_HEADER *uh;
1368 static const ntfschar UsnJrnl[9] = { const_cpu_to_le16('$'),
1369 const_cpu_to_le16('U'), const_cpu_to_le16('s'),
1370 const_cpu_to_le16('n'), const_cpu_to_le16('J'),
1371 const_cpu_to_le16('r'), const_cpu_to_le16('n'),
1372 const_cpu_to_le16('l'), 0 };
1373 static ntfschar Max[5] = { const_cpu_to_le16('$'),
1374 const_cpu_to_le16('M'), const_cpu_to_le16('a'),
1375 const_cpu_to_le16('x'), 0 };
1376 static ntfschar J[3] = { const_cpu_to_le16('$'),
1377 const_cpu_to_le16('J'), 0 };
1378
1379 ntfs_debug("Entering.");
1380 /*
1381 * Find the inode number for the transaction log file by looking up the
1382 * filename $UsnJrnl in the extended system files directory $Extend.
1383 */
1384 down(&vol->extend_ino->i_sem);
1385 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1386 &name);
1387 up(&vol->extend_ino->i_sem);
1388 if (IS_ERR_MREF(mref)) {
1389 /*
1390 * If the file does not exist, transaction logging is disabled,
1391 * just return success.
1392 */
1393 if (MREF_ERR(mref) == -ENOENT) {
1394 ntfs_debug("$UsnJrnl not present. Volume does not "
1395 "have transaction logging enabled.");
1396not_enabled:
1397 /*
1398 * No need to try to stamp the transaction log if
1399 * transaction logging is not enabled.
1400 */
1401 NVolSetUsnJrnlStamped(vol);
1402 return TRUE;
1403 }
1404 /* A real error occured. */
1405 ntfs_error(vol->sb, "Failed to find inode number for "
1406 "$UsnJrnl.");
1407 return FALSE;
1408 }
1409 /* We do not care for the type of match that was found. */
1410 kfree(name);
1411 /* Get the inode. */
1412 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1413 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1414 if (!IS_ERR(tmp_ino))
1415 iput(tmp_ino);
1416 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1417 return FALSE;
1418 }
1419 vol->usnjrnl_ino = tmp_ino;
1420 /*
1421 * If the transaction log is in the process of being deleted, we can
1422 * ignore it.
1423 */
1424 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1425 ntfs_debug("$UsnJrnl in the process of being disabled. "
1426 "Volume does not have transaction logging "
1427 "enabled.");
1428 goto not_enabled;
1429 }
1430 /* Get the $DATA/$Max attribute. */
1431 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1432 if (IS_ERR(tmp_ino)) {
1433 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1434 "attribute.");
1435 return FALSE;
1436 }
1437 vol->usnjrnl_max_ino = tmp_ino;
1438 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1439 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1440 "attribute (size is 0x%llx but should be at "
1441 "least 0x%x bytes).", i_size_read(tmp_ino),
1442 sizeof(USN_HEADER));
1443 return FALSE;
1444 }
1445 /* Get the $DATA/$J attribute. */
1446 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1447 if (IS_ERR(tmp_ino)) {
1448 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1449 "attribute.");
1450 return FALSE;
1451 }
1452 vol->usnjrnl_j_ino = tmp_ino;
1453 /* Verify $J is non-resident and sparse. */
1454 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1455 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1456 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1457 "and/or not sparse.");
1458 return FALSE;
1459 }
1460 /* Read the USN_HEADER from $DATA/$Max. */
1461 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1462 if (IS_ERR(page)) {
1463 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1464 "attribute.");
1465 return FALSE;
1466 }
1467 uh = (USN_HEADER*)page_address(page);
1468 /* Sanity check the $Max. */
1469 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1470 sle64_to_cpu(uh->maximum_size))) {
1471 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1472 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1473 (long long)sle64_to_cpu(uh->allocation_delta),
1474 (long long)sle64_to_cpu(uh->maximum_size));
1475 ntfs_unmap_page(page);
1476 return FALSE;
1477 }
1478 /*
1479 * If the transaction log has been stamped and nothing has been written
1480 * to it since, we do not need to stamp it.
1481 */
1482 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1483 i_size_read(vol->usnjrnl_j_ino))) {
1484 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1485 i_size_read(vol->usnjrnl_j_ino))) {
1486 ntfs_unmap_page(page);
1487 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1488 "logged since it was last stamped. "
1489 "Treating this as if the volume does "
1490 "not have transaction logging "
1491 "enabled.");
1492 goto not_enabled;
1493 }
1494 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1495 "which is out of bounds (0x%llx). $UsnJrnl "
1496 "is corrupt.",
1497 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1498 i_size_read(vol->usnjrnl_j_ino));
1499 ntfs_unmap_page(page);
1500 return FALSE;
1501 }
1502 ntfs_unmap_page(page);
1503 ntfs_debug("Done.");
1504 return TRUE;
1505}
1506
1507/**
1201 * load_and_init_attrdef - load the attribute definitions table for a volume 1508 * load_and_init_attrdef - load the attribute definitions table for a volume
1202 * @vol: ntfs super block describing device whose attrdef to load 1509 * @vol: ntfs super block describing device whose attrdef to load
1203 * 1510 *
@@ -1205,10 +1512,11 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1205 */ 1512 */
1206static BOOL load_and_init_attrdef(ntfs_volume *vol) 1513static BOOL load_and_init_attrdef(ntfs_volume *vol)
1207{ 1514{
1515 loff_t i_size;
1208 struct super_block *sb = vol->sb; 1516 struct super_block *sb = vol->sb;
1209 struct inode *ino; 1517 struct inode *ino;
1210 struct page *page; 1518 struct page *page;
1211 unsigned long index, max_index; 1519 pgoff_t index, max_index;
1212 unsigned int size; 1520 unsigned int size;
1213 1521
1214 ntfs_debug("Entering."); 1522 ntfs_debug("Entering.");
@@ -1219,14 +1527,16 @@ static BOOL load_and_init_attrdef(ntfs_volume *vol)
1219 iput(ino); 1527 iput(ino);
1220 goto failed; 1528 goto failed;
1221 } 1529 }
1530 NInoSetSparseDisabled(NTFS_I(ino));
1222 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */ 1531 /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1223 if (!ino->i_size || ino->i_size > 0x7fffffff) 1532 i_size = i_size_read(ino);
1533 if (i_size <= 0 || i_size > 0x7fffffff)
1224 goto iput_failed; 1534 goto iput_failed;
1225 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(ino->i_size); 1535 vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1226 if (!vol->attrdef) 1536 if (!vol->attrdef)
1227 goto iput_failed; 1537 goto iput_failed;
1228 index = 0; 1538 index = 0;
1229 max_index = ino->i_size >> PAGE_CACHE_SHIFT; 1539 max_index = i_size >> PAGE_CACHE_SHIFT;
1230 size = PAGE_CACHE_SIZE; 1540 size = PAGE_CACHE_SIZE;
1231 while (index < max_index) { 1541 while (index < max_index) {
1232 /* Read the attrdef table and copy it into the linear buffer. */ 1542 /* Read the attrdef table and copy it into the linear buffer. */
@@ -1239,12 +1549,12 @@ read_partial_attrdef_page:
1239 ntfs_unmap_page(page); 1549 ntfs_unmap_page(page);
1240 }; 1550 };
1241 if (size == PAGE_CACHE_SIZE) { 1551 if (size == PAGE_CACHE_SIZE) {
1242 size = ino->i_size & ~PAGE_CACHE_MASK; 1552 size = i_size & ~PAGE_CACHE_MASK;
1243 if (size) 1553 if (size)
1244 goto read_partial_attrdef_page; 1554 goto read_partial_attrdef_page;
1245 } 1555 }
1246 vol->attrdef_size = ino->i_size; 1556 vol->attrdef_size = i_size;
1247 ntfs_debug("Read %llu bytes from $AttrDef.", ino->i_size); 1557 ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1248 iput(ino); 1558 iput(ino);
1249 return TRUE; 1559 return TRUE;
1250free_iput_failed: 1560free_iput_failed:
@@ -1267,10 +1577,11 @@ failed:
1267 */ 1577 */
1268static BOOL load_and_init_upcase(ntfs_volume *vol) 1578static BOOL load_and_init_upcase(ntfs_volume *vol)
1269{ 1579{
1580 loff_t i_size;
1270 struct super_block *sb = vol->sb; 1581 struct super_block *sb = vol->sb;
1271 struct inode *ino; 1582 struct inode *ino;
1272 struct page *page; 1583 struct page *page;
1273 unsigned long index, max_index; 1584 pgoff_t index, max_index;
1274 unsigned int size; 1585 unsigned int size;
1275 int i, max; 1586 int i, max;
1276 1587
@@ -1286,14 +1597,15 @@ static BOOL load_and_init_upcase(ntfs_volume *vol)
1286 * The upcase size must not be above 64k Unicode characters, must not 1597 * The upcase size must not be above 64k Unicode characters, must not
1287 * be zero and must be a multiple of sizeof(ntfschar). 1598 * be zero and must be a multiple of sizeof(ntfschar).
1288 */ 1599 */
1289 if (!ino->i_size || ino->i_size & (sizeof(ntfschar) - 1) || 1600 i_size = i_size_read(ino);
1290 ino->i_size > 64ULL * 1024 * sizeof(ntfschar)) 1601 if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1602 i_size > 64ULL * 1024 * sizeof(ntfschar))
1291 goto iput_upcase_failed; 1603 goto iput_upcase_failed;
1292 vol->upcase = (ntfschar*)ntfs_malloc_nofs(ino->i_size); 1604 vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1293 if (!vol->upcase) 1605 if (!vol->upcase)
1294 goto iput_upcase_failed; 1606 goto iput_upcase_failed;
1295 index = 0; 1607 index = 0;
1296 max_index = ino->i_size >> PAGE_CACHE_SHIFT; 1608 max_index = i_size >> PAGE_CACHE_SHIFT;
1297 size = PAGE_CACHE_SIZE; 1609 size = PAGE_CACHE_SIZE;
1298 while (index < max_index) { 1610 while (index < max_index) {
1299 /* Read the upcase table and copy it into the linear buffer. */ 1611 /* Read the upcase table and copy it into the linear buffer. */
@@ -1306,13 +1618,13 @@ read_partial_upcase_page:
1306 ntfs_unmap_page(page); 1618 ntfs_unmap_page(page);
1307 }; 1619 };
1308 if (size == PAGE_CACHE_SIZE) { 1620 if (size == PAGE_CACHE_SIZE) {
1309 size = ino->i_size & ~PAGE_CACHE_MASK; 1621 size = i_size & ~PAGE_CACHE_MASK;
1310 if (size) 1622 if (size)
1311 goto read_partial_upcase_page; 1623 goto read_partial_upcase_page;
1312 } 1624 }
1313 vol->upcase_len = ino->i_size >> UCHAR_T_SIZE_BITS; 1625 vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1314 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).", 1626 ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1315 ino->i_size, 64 * 1024 * sizeof(ntfschar)); 1627 i_size, 64 * 1024 * sizeof(ntfschar));
1316 iput(ino); 1628 iput(ino);
1317 down(&ntfs_lock); 1629 down(&ntfs_lock);
1318 if (!default_upcase) { 1630 if (!default_upcase) {
@@ -1376,6 +1688,9 @@ static BOOL load_system_files(ntfs_volume *vol)
1376 MFT_RECORD *m; 1688 MFT_RECORD *m;
1377 VOLUME_INFORMATION *vi; 1689 VOLUME_INFORMATION *vi;
1378 ntfs_attr_search_ctx *ctx; 1690 ntfs_attr_search_ctx *ctx;
1691#ifdef NTFS_RW
1692 int err;
1693#endif /* NTFS_RW */
1379 1694
1380 ntfs_debug("Entering."); 1695 ntfs_debug("Entering.");
1381#ifdef NTFS_RW 1696#ifdef NTFS_RW
@@ -1435,7 +1750,8 @@ static BOOL load_system_files(ntfs_volume *vol)
1435 iput(vol->lcnbmp_ino); 1750 iput(vol->lcnbmp_ino);
1436 goto bitmap_failed; 1751 goto bitmap_failed;
1437 } 1752 }
1438 if ((vol->nr_clusters + 7) >> 3 > vol->lcnbmp_ino->i_size) { 1753 NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1754 if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1439 iput(vol->lcnbmp_ino); 1755 iput(vol->lcnbmp_ino);
1440bitmap_failed: 1756bitmap_failed:
1441 ntfs_error(sb, "Failed to load $Bitmap."); 1757 ntfs_error(sb, "Failed to load $Bitmap.");
@@ -1486,6 +1802,12 @@ get_ctx_vol_failed:
1486 unmap_mft_record(NTFS_I(vol->vol_ino)); 1802 unmap_mft_record(NTFS_I(vol->vol_ino));
1487 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver, 1803 printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1488 vol->minor_ver); 1804 vol->minor_ver);
1805 if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1806 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1807 "volume version %i.%i (need at least version "
1808 "3.0).", vol->major_ver, vol->minor_ver);
1809 NVolClearSparseEnabled(vol);
1810 }
1489#ifdef NTFS_RW 1811#ifdef NTFS_RW
1490 /* Make sure that no unsupported volume flags are set. */ 1812 /* Make sure that no unsupported volume flags are set. */
1491 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) { 1813 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
@@ -1545,6 +1867,50 @@ get_ctx_vol_failed:
1545 /* This will prevent a read-write remount. */ 1867 /* This will prevent a read-write remount. */
1546 NVolSetErrors(vol); 1868 NVolSetErrors(vol);
1547 } 1869 }
1870#endif /* NTFS_RW */
1871 /* Get the root directory inode so we can do path lookups. */
1872 vol->root_ino = ntfs_iget(sb, FILE_root);
1873 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1874 if (!IS_ERR(vol->root_ino))
1875 iput(vol->root_ino);
1876 ntfs_error(sb, "Failed to load root directory.");
1877 goto iput_logfile_err_out;
1878 }
1879#ifdef NTFS_RW
1880 /*
1881 * Check if Windows is suspended to disk on the target volume. If it
1882 * is hibernated, we must not write *anything* to the disk so set
1883 * NVolErrors() without setting the dirty volume flag and mount
1884 * read-only. This will prevent read-write remounting and it will also
1885 * prevent all writes.
1886 */
1887 err = check_windows_hibernation_status(vol);
1888 if (unlikely(err)) {
1889 static const char *es1a = "Failed to determine if Windows is "
1890 "hibernated";
1891 static const char *es1b = "Windows is hibernated";
1892 static const char *es2 = ". Run chkdsk.";
1893 const char *es1;
1894
1895 es1 = err < 0 ? es1a : es1b;
1896 /* If a read-write mount, convert it to a read-only mount. */
1897 if (!(sb->s_flags & MS_RDONLY)) {
1898 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1899 ON_ERRORS_CONTINUE))) {
1900 ntfs_error(sb, "%s and neither on_errors="
1901 "continue nor on_errors="
1902 "remount-ro was specified%s",
1903 es1, es2);
1904 goto iput_root_err_out;
1905 }
1906 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1907 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1908 } else
1909 ntfs_warning(sb, "%s. Will not be able to remount "
1910 "read-write%s", es1, es2);
1911 /* This will prevent a read-write remount. */
1912 NVolSetErrors(vol);
1913 }
1548 /* If (still) a read-write mount, mark the volume dirty. */ 1914 /* If (still) a read-write mount, mark the volume dirty. */
1549 if (!(sb->s_flags & MS_RDONLY) && 1915 if (!(sb->s_flags & MS_RDONLY) &&
1550 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) { 1916 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
@@ -1558,7 +1924,7 @@ get_ctx_vol_failed:
1558 ntfs_error(sb, "%s and neither on_errors=continue nor " 1924 ntfs_error(sb, "%s and neither on_errors=continue nor "
1559 "on_errors=remount-ro was specified%s", 1925 "on_errors=remount-ro was specified%s",
1560 es1, es2); 1926 es1, es2);
1561 goto iput_logfile_err_out; 1927 goto iput_root_err_out;
1562 } 1928 }
1563 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1929 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1564 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 1930 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
@@ -1585,7 +1951,7 @@ get_ctx_vol_failed:
1585 ntfs_error(sb, "%s and neither on_errors=continue nor " 1951 ntfs_error(sb, "%s and neither on_errors=continue nor "
1586 "on_errors=remount-ro was specified%s", 1952 "on_errors=remount-ro was specified%s",
1587 es1, es2); 1953 es1, es2);
1588 goto iput_logfile_err_out; 1954 goto iput_root_err_out;
1589 } 1955 }
1590 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1956 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1591 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 1957 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
@@ -1604,23 +1970,15 @@ get_ctx_vol_failed:
1604 ntfs_error(sb, "%s and neither on_errors=continue nor " 1970 ntfs_error(sb, "%s and neither on_errors=continue nor "
1605 "on_errors=remount-ro was specified%s", 1971 "on_errors=remount-ro was specified%s",
1606 es1, es2); 1972 es1, es2);
1607 goto iput_logfile_err_out; 1973 goto iput_root_err_out;
1608 } 1974 }
1609 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2); 1975 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1610 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 1976 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1611 NVolSetErrors(vol); 1977 NVolSetErrors(vol);
1612 } 1978 }
1613#endif /* NTFS_RW */ 1979#endif /* NTFS_RW */
1614 /* Get the root directory inode. */
1615 vol->root_ino = ntfs_iget(sb, FILE_root);
1616 if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1617 if (!IS_ERR(vol->root_ino))
1618 iput(vol->root_ino);
1619 ntfs_error(sb, "Failed to load root directory.");
1620 goto iput_logfile_err_out;
1621 }
1622 /* If on NTFS versions before 3.0, we are done. */ 1980 /* If on NTFS versions before 3.0, we are done. */
1623 if (vol->major_ver < 3) 1981 if (unlikely(vol->major_ver < 3))
1624 return TRUE; 1982 return TRUE;
1625 /* NTFS 3.0+ specific initialization. */ 1983 /* NTFS 3.0+ specific initialization. */
1626 /* Get the security descriptors inode. */ 1984 /* Get the security descriptors inode. */
@@ -1631,7 +1989,7 @@ get_ctx_vol_failed:
1631 ntfs_error(sb, "Failed to load $Secure."); 1989 ntfs_error(sb, "Failed to load $Secure.");
1632 goto iput_root_err_out; 1990 goto iput_root_err_out;
1633 } 1991 }
1634 // FIXME: Initialize security. 1992 // TODO: Initialize security.
1635 /* Get the extended system files' directory inode. */ 1993 /* Get the extended system files' directory inode. */
1636 vol->extend_ino = ntfs_iget(sb, FILE_Extend); 1994 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
1637 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) { 1995 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
@@ -1682,10 +2040,60 @@ get_ctx_vol_failed:
1682 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 2040 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1683 NVolSetErrors(vol); 2041 NVolSetErrors(vol);
1684 } 2042 }
1685 // TODO: Delete or checkpoint the $UsnJrnl if it exists. 2043 /*
2044 * Find the transaction log file ($UsnJrnl), load it if present, check
2045 * it, and set it up.
2046 */
2047 if (!load_and_init_usnjrnl(vol)) {
2048 static const char *es1 = "Failed to load $UsnJrnl";
2049 static const char *es2 = ". Run chkdsk.";
2050
2051 /* If a read-write mount, convert it to a read-only mount. */
2052 if (!(sb->s_flags & MS_RDONLY)) {
2053 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2054 ON_ERRORS_CONTINUE))) {
2055 ntfs_error(sb, "%s and neither on_errors="
2056 "continue nor on_errors="
2057 "remount-ro was specified%s",
2058 es1, es2);
2059 goto iput_usnjrnl_err_out;
2060 }
2061 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2062 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2063 } else
2064 ntfs_warning(sb, "%s. Will not be able to remount "
2065 "read-write%s", es1, es2);
2066 /* This will prevent a read-write remount. */
2067 NVolSetErrors(vol);
2068 }
2069 /* If (still) a read-write mount, stamp the transaction log. */
2070 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
2071 static const char *es1 = "Failed to stamp transaction log "
2072 "($UsnJrnl)";
2073 static const char *es2 = ". Run chkdsk.";
2074
2075 /* Convert to a read-only mount. */
2076 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
2077 ON_ERRORS_CONTINUE))) {
2078 ntfs_error(sb, "%s and neither on_errors=continue nor "
2079 "on_errors=remount-ro was specified%s",
2080 es1, es2);
2081 goto iput_usnjrnl_err_out;
2082 }
2083 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
2084 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2085 NVolSetErrors(vol);
2086 }
1686#endif /* NTFS_RW */ 2087#endif /* NTFS_RW */
1687 return TRUE; 2088 return TRUE;
1688#ifdef NTFS_RW 2089#ifdef NTFS_RW
2090iput_usnjrnl_err_out:
2091 if (vol->usnjrnl_j_ino)
2092 iput(vol->usnjrnl_j_ino);
2093 if (vol->usnjrnl_max_ino)
2094 iput(vol->usnjrnl_max_ino);
2095 if (vol->usnjrnl_ino)
2096 iput(vol->usnjrnl_ino);
1689iput_quota_err_out: 2097iput_quota_err_out:
1690 if (vol->quota_q_ino) 2098 if (vol->quota_q_ino)
1691 iput(vol->quota_q_ino); 2099 iput(vol->quota_q_ino);
@@ -1759,6 +2167,12 @@ static void ntfs_put_super(struct super_block *sb)
1759 2167
1760 /* NTFS 3.0+ specific. */ 2168 /* NTFS 3.0+ specific. */
1761 if (vol->major_ver >= 3) { 2169 if (vol->major_ver >= 3) {
2170 if (vol->usnjrnl_j_ino)
2171 ntfs_commit_inode(vol->usnjrnl_j_ino);
2172 if (vol->usnjrnl_max_ino)
2173 ntfs_commit_inode(vol->usnjrnl_max_ino);
2174 if (vol->usnjrnl_ino)
2175 ntfs_commit_inode(vol->usnjrnl_ino);
1762 if (vol->quota_q_ino) 2176 if (vol->quota_q_ino)
1763 ntfs_commit_inode(vol->quota_q_ino); 2177 ntfs_commit_inode(vol->quota_q_ino);
1764 if (vol->quota_ino) 2178 if (vol->quota_ino)
@@ -1814,6 +2228,18 @@ static void ntfs_put_super(struct super_block *sb)
1814 /* NTFS 3.0+ specific clean up. */ 2228 /* NTFS 3.0+ specific clean up. */
1815 if (vol->major_ver >= 3) { 2229 if (vol->major_ver >= 3) {
1816#ifdef NTFS_RW 2230#ifdef NTFS_RW
2231 if (vol->usnjrnl_j_ino) {
2232 iput(vol->usnjrnl_j_ino);
2233 vol->usnjrnl_j_ino = NULL;
2234 }
2235 if (vol->usnjrnl_max_ino) {
2236 iput(vol->usnjrnl_max_ino);
2237 vol->usnjrnl_max_ino = NULL;
2238 }
2239 if (vol->usnjrnl_ino) {
2240 iput(vol->usnjrnl_ino);
2241 vol->usnjrnl_ino = NULL;
2242 }
1817 if (vol->quota_q_ino) { 2243 if (vol->quota_q_ino) {
1818 iput(vol->quota_q_ino); 2244 iput(vol->quota_q_ino);
1819 vol->quota_q_ino = NULL; 2245 vol->quota_q_ino = NULL;
@@ -1959,8 +2385,7 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
1959 struct address_space *mapping = vol->lcnbmp_ino->i_mapping; 2385 struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
1960 filler_t *readpage = (filler_t*)mapping->a_ops->readpage; 2386 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
1961 struct page *page; 2387 struct page *page;
1962 unsigned long index, max_index; 2388 pgoff_t index, max_index;
1963 unsigned int max_size;
1964 2389
1965 ntfs_debug("Entering."); 2390 ntfs_debug("Entering.");
1966 /* Serialize accesses to the cluster bitmap. */ 2391 /* Serialize accesses to the cluster bitmap. */
@@ -1972,11 +2397,10 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
1972 */ 2397 */
1973 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> 2398 max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
1974 PAGE_CACHE_SHIFT; 2399 PAGE_CACHE_SHIFT;
1975 /* Use multiples of 4 bytes. */ 2400 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
1976 max_size = PAGE_CACHE_SIZE >> 2; 2401 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
1977 ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%x.", 2402 max_index, PAGE_CACHE_SIZE / 4);
1978 max_index, max_size); 2403 for (index = 0; index < max_index; index++) {
1979 for (index = 0UL; index < max_index; index++) {
1980 unsigned int i; 2404 unsigned int i;
1981 /* 2405 /*
1982 * Read the page from page cache, getting it from backing store 2406 * Read the page from page cache, getting it from backing store
@@ -2008,7 +2432,7 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2008 * the result as all out of range bytes are set to zero by 2432 * the result as all out of range bytes are set to zero by
2009 * ntfs_readpage(). 2433 * ntfs_readpage().
2010 */ 2434 */
2011 for (i = 0; i < max_size; i++) 2435 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2012 nr_free -= (s64)hweight32(kaddr[i]); 2436 nr_free -= (s64)hweight32(kaddr[i]);
2013 kunmap_atomic(kaddr, KM_USER0); 2437 kunmap_atomic(kaddr, KM_USER0);
2014 page_cache_release(page); 2438 page_cache_release(page);
@@ -2031,6 +2455,8 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2031/** 2455/**
2032 * __get_nr_free_mft_records - return the number of free inodes on a volume 2456 * __get_nr_free_mft_records - return the number of free inodes on a volume
2033 * @vol: ntfs volume for which to obtain free inode count 2457 * @vol: ntfs volume for which to obtain free inode count
2458 * @nr_free: number of mft records in filesystem
2459 * @max_index: maximum number of pages containing set bits
2034 * 2460 *
2035 * Calculate the number of free mft records (inodes) on the mounted NTFS 2461 * Calculate the number of free mft records (inodes) on the mounted NTFS
2036 * volume @vol. We actually calculate the number of mft records in use instead 2462 * volume @vol. We actually calculate the number of mft records in use instead
@@ -2043,32 +2469,20 @@ static s64 get_nr_free_clusters(ntfs_volume *vol)
2043 * 2469 *
2044 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing. 2470 * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2045 */ 2471 */
2046static unsigned long __get_nr_free_mft_records(ntfs_volume *vol) 2472static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2473 s64 nr_free, const pgoff_t max_index)
2047{ 2474{
2048 s64 nr_free;
2049 u32 *kaddr; 2475 u32 *kaddr;
2050 struct address_space *mapping = vol->mftbmp_ino->i_mapping; 2476 struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2051 filler_t *readpage = (filler_t*)mapping->a_ops->readpage; 2477 filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2052 struct page *page; 2478 struct page *page;
2053 unsigned long index, max_index; 2479 pgoff_t index;
2054 unsigned int max_size;
2055 2480
2056 ntfs_debug("Entering."); 2481 ntfs_debug("Entering.");
2057 /* Number of mft records in file system (at this point in time). */ 2482 /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2058 nr_free = vol->mft_ino->i_size >> vol->mft_record_size_bits;
2059 /*
2060 * Convert the maximum number of set bits into bytes rounded up, then
2061 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2062 * have one full and one partial page max_index = 2.
2063 */
2064 max_index = ((((NTFS_I(vol->mft_ino)->initialized_size >>
2065 vol->mft_record_size_bits) + 7) >> 3) +
2066 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2067 /* Use multiples of 4 bytes. */
2068 max_size = PAGE_CACHE_SIZE >> 2;
2069 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = " 2483 ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2070 "0x%x.", max_index, max_size); 2484 "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2071 for (index = 0UL; index < max_index; index++) { 2485 for (index = 0; index < max_index; index++) {
2072 unsigned int i; 2486 unsigned int i;
2073 /* 2487 /*
2074 * Read the page from page cache, getting it from backing store 2488 * Read the page from page cache, getting it from backing store
@@ -2100,7 +2514,7 @@ static unsigned long __get_nr_free_mft_records(ntfs_volume *vol)
2100 * the result as all out of range bytes are set to zero by 2514 * the result as all out of range bytes are set to zero by
2101 * ntfs_readpage(). 2515 * ntfs_readpage().
2102 */ 2516 */
2103 for (i = 0; i < max_size; i++) 2517 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2104 nr_free -= (s64)hweight32(kaddr[i]); 2518 nr_free -= (s64)hweight32(kaddr[i]);
2105 kunmap_atomic(kaddr, KM_USER0); 2519 kunmap_atomic(kaddr, KM_USER0);
2106 page_cache_release(page); 2520 page_cache_release(page);
@@ -2134,8 +2548,11 @@ static unsigned long __get_nr_free_mft_records(ntfs_volume *vol)
2134 */ 2548 */
2135static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs) 2549static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2136{ 2550{
2137 ntfs_volume *vol = NTFS_SB(sb);
2138 s64 size; 2551 s64 size;
2552 ntfs_volume *vol = NTFS_SB(sb);
2553 ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2554 pgoff_t max_index;
2555 unsigned long flags;
2139 2556
2140 ntfs_debug("Entering."); 2557 ntfs_debug("Entering.");
2141 /* Type of filesystem. */ 2558 /* Type of filesystem. */
@@ -2143,13 +2560,13 @@ static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2143 /* Optimal transfer block size. */ 2560 /* Optimal transfer block size. */
2144 sfs->f_bsize = PAGE_CACHE_SIZE; 2561 sfs->f_bsize = PAGE_CACHE_SIZE;
2145 /* 2562 /*
2146 * Total data blocks in file system in units of f_bsize and since 2563 * Total data blocks in filesystem in units of f_bsize and since
2147 * inodes are also stored in data blocs ($MFT is a file) this is just 2564 * inodes are also stored in data blocs ($MFT is a file) this is just
2148 * the total clusters. 2565 * the total clusters.
2149 */ 2566 */
2150 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >> 2567 sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2151 PAGE_CACHE_SHIFT; 2568 PAGE_CACHE_SHIFT;
2152 /* Free data blocks in file system in units of f_bsize. */ 2569 /* Free data blocks in filesystem in units of f_bsize. */
2153 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >> 2570 size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2154 PAGE_CACHE_SHIFT; 2571 PAGE_CACHE_SHIFT;
2155 if (size < 0LL) 2572 if (size < 0LL)
@@ -2158,17 +2575,27 @@ static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2158 sfs->f_bavail = sfs->f_bfree = size; 2575 sfs->f_bavail = sfs->f_bfree = size;
2159 /* Serialize accesses to the inode bitmap. */ 2576 /* Serialize accesses to the inode bitmap. */
2160 down_read(&vol->mftbmp_lock); 2577 down_read(&vol->mftbmp_lock);
2161 /* Number of inodes in file system (at this point in time). */ 2578 read_lock_irqsave(&mft_ni->size_lock, flags);
2162 sfs->f_files = vol->mft_ino->i_size >> vol->mft_record_size_bits; 2579 size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2580 /*
2581 * Convert the maximum number of set bits into bytes rounded up, then
2582 * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2583 * have one full and one partial page max_index = 2.
2584 */
2585 max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2586 + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2587 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2588 /* Number of inodes in filesystem (at this point in time). */
2589 sfs->f_files = size;
2163 /* Free inodes in fs (based on current total count). */ 2590 /* Free inodes in fs (based on current total count). */
2164 sfs->f_ffree = __get_nr_free_mft_records(vol); 2591 sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2165 up_read(&vol->mftbmp_lock); 2592 up_read(&vol->mftbmp_lock);
2166 /* 2593 /*
2167 * File system id. This is extremely *nix flavour dependent and even 2594 * File system id. This is extremely *nix flavour dependent and even
2168 * within Linux itself all fs do their own thing. I interpret this to 2595 * within Linux itself all fs do their own thing. I interpret this to
2169 * mean a unique id associated with the mounted fs and not the id 2596 * mean a unique id associated with the mounted fs and not the id
2170 * associated with the file system driver, the latter is already given 2597 * associated with the filesystem driver, the latter is already given
2171 * by the file system type in sfs->f_type. Thus we use the 64-bit 2598 * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2172 * volume serial number splitting it into two 32-bit parts. We enter 2599 * volume serial number splitting it into two 32-bit parts. We enter
2173 * the least significant 32-bits in f_fsid[0] and the most significant 2600 * the least significant 32-bits in f_fsid[0] and the most significant
2174 * 32-bits in f_fsid[1]. 2601 * 32-bits in f_fsid[1].
@@ -2219,53 +2646,19 @@ static struct super_operations ntfs_sops = {
2219 proc. */ 2646 proc. */
2220}; 2647};
2221 2648
2222
2223/** 2649/**
2224 * Declarations for NTFS specific export operations (fs/ntfs/namei.c). 2650 * ntfs_fill_super - mount an ntfs filesystem
2225 */ 2651 * @sb: super block of ntfs filesystem to mount
2226extern struct dentry *ntfs_get_parent(struct dentry *child_dent);
2227extern struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh);
2228
2229/**
2230 * Export operations allowing NFS exporting of mounted NTFS partitions.
2231 *
2232 * We use the default ->decode_fh() and ->encode_fh() for now. Note that they
2233 * use 32 bits to store the inode number which is an unsigned long so on 64-bit
2234 * architectures is usually 64 bits so it would all fail horribly on huge
2235 * volumes. I guess we need to define our own encode and decode fh functions
2236 * that store 64-bit inode numbers at some point but for now we will ignore the
2237 * problem...
2238 *
2239 * We also use the default ->get_name() helper (used by ->decode_fh() via
2240 * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
2241 * independent.
2242 *
2243 * The default ->get_parent() just returns -EACCES so we have to provide our
2244 * own and the default ->get_dentry() is incompatible with NTFS due to not
2245 * allowing the inode number 0 which is used in NTFS for the system file $MFT
2246 * and due to using iget() whereas NTFS needs ntfs_iget().
2247 */
2248static struct export_operations ntfs_export_ops = {
2249 .get_parent = ntfs_get_parent, /* Find the parent of a given
2250 directory. */
2251 .get_dentry = ntfs_get_dentry, /* Find a dentry for the inode
2252 given a file handle
2253 sub-fragment. */
2254};
2255
2256/**
2257 * ntfs_fill_super - mount an ntfs files system
2258 * @sb: super block of ntfs file system to mount
2259 * @opt: string containing the mount options 2652 * @opt: string containing the mount options
2260 * @silent: silence error output 2653 * @silent: silence error output
2261 * 2654 *
2262 * ntfs_fill_super() is called by the VFS to mount the device described by @sb 2655 * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2263 * with the mount otions in @data with the NTFS file system. 2656 * with the mount otions in @data with the NTFS filesystem.
2264 * 2657 *
2265 * If @silent is true, remain silent even if errors are detected. This is used 2658 * If @silent is true, remain silent even if errors are detected. This is used
2266 * during bootup, when the kernel tries to mount the root file system with all 2659 * during bootup, when the kernel tries to mount the root filesystem with all
2267 * registered file systems one after the other until one succeeds. This implies 2660 * registered filesystems one after the other until one succeeds. This implies
2268 * that all file systems except the correct one will quite correctly and 2661 * that all filesystems except the correct one will quite correctly and
2269 * expectedly return an error, but nobody wants to see error messages when in 2662 * expectedly return an error, but nobody wants to see error messages when in
2270 * fact this is what is supposed to happen. 2663 * fact this is what is supposed to happen.
2271 * 2664 *
@@ -2292,39 +2685,25 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2292 return -ENOMEM; 2685 return -ENOMEM;
2293 } 2686 }
2294 /* Initialize ntfs_volume structure. */ 2687 /* Initialize ntfs_volume structure. */
2295 memset(vol, 0, sizeof(ntfs_volume)); 2688 *vol = (ntfs_volume) {
2296 vol->sb = sb; 2689 .sb = sb,
2297 vol->upcase = NULL; 2690 /*
2298 vol->attrdef = NULL; 2691 * Default is group and other don't have any access to files or
2299 vol->mft_ino = NULL; 2692 * directories while owner has full access. Further, files by
2300 vol->mftbmp_ino = NULL; 2693 * default are not executable but directories are of course
2694 * browseable.
2695 */
2696 .fmask = 0177,
2697 .dmask = 0077,
2698 };
2301 init_rwsem(&vol->mftbmp_lock); 2699 init_rwsem(&vol->mftbmp_lock);
2302#ifdef NTFS_RW
2303 vol->mftmirr_ino = NULL;
2304 vol->logfile_ino = NULL;
2305#endif /* NTFS_RW */
2306 vol->lcnbmp_ino = NULL;
2307 init_rwsem(&vol->lcnbmp_lock); 2700 init_rwsem(&vol->lcnbmp_lock);
2308 vol->vol_ino = NULL;
2309 vol->root_ino = NULL;
2310 vol->secure_ino = NULL;
2311 vol->extend_ino = NULL;
2312#ifdef NTFS_RW
2313 vol->quota_ino = NULL;
2314 vol->quota_q_ino = NULL;
2315#endif /* NTFS_RW */
2316 vol->nls_map = NULL;
2317
2318 /*
2319 * Default is group and other don't have any access to files or
2320 * directories while owner has full access. Further, files by default
2321 * are not executable but directories are of course browseable.
2322 */
2323 vol->fmask = 0177;
2324 vol->dmask = 0077;
2325 2701
2326 unlock_kernel(); 2702 unlock_kernel();
2327 2703
2704 /* By default, enable sparse support. */
2705 NVolSetSparseEnabled(vol);
2706
2328 /* Important to get the mount options dealt with now. */ 2707 /* Important to get the mount options dealt with now. */
2329 if (!parse_options(vol, (char*)opt)) 2708 if (!parse_options(vol, (char*)opt))
2330 goto err_out_now; 2709 goto err_out_now;
@@ -2347,7 +2726,8 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2347 } 2726 }
2348 2727
2349 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */ 2728 /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2350 vol->nr_blocks = sb->s_bdev->bd_inode->i_size >> NTFS_BLOCK_SIZE_BITS; 2729 vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2730 NTFS_BLOCK_SIZE_BITS;
2351 2731
2352 /* Read the boot sector and return unlocked buffer head to it. */ 2732 /* Read the boot sector and return unlocked buffer head to it. */
2353 if (!(bh = read_ntfs_boot_sector(sb, silent))) { 2733 if (!(bh = read_ntfs_boot_sector(sb, silent))) {
@@ -2476,6 +2856,18 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2476 /* NTFS 3.0+ specific clean up. */ 2856 /* NTFS 3.0+ specific clean up. */
2477 if (vol->major_ver >= 3) { 2857 if (vol->major_ver >= 3) {
2478#ifdef NTFS_RW 2858#ifdef NTFS_RW
2859 if (vol->usnjrnl_j_ino) {
2860 iput(vol->usnjrnl_j_ino);
2861 vol->usnjrnl_j_ino = NULL;
2862 }
2863 if (vol->usnjrnl_max_ino) {
2864 iput(vol->usnjrnl_max_ino);
2865 vol->usnjrnl_max_ino = NULL;
2866 }
2867 if (vol->usnjrnl_ino) {
2868 iput(vol->usnjrnl_ino);
2869 vol->usnjrnl_ino = NULL;
2870 }
2479 if (vol->quota_q_ino) { 2871 if (vol->quota_q_ino) {
2480 iput(vol->quota_q_ino); 2872 iput(vol->quota_q_ino);
2481 vol->quota_q_ino = NULL; 2873 vol->quota_q_ino = NULL;
@@ -2581,7 +2973,7 @@ err_out_now:
2581 */ 2973 */
2582kmem_cache_t *ntfs_name_cache; 2974kmem_cache_t *ntfs_name_cache;
2583 2975
2584/* Slab caches for efficient allocation/deallocation of of inodes. */ 2976/* Slab caches for efficient allocation/deallocation of inodes. */
2585kmem_cache_t *ntfs_inode_cache; 2977kmem_cache_t *ntfs_inode_cache;
2586kmem_cache_t *ntfs_big_inode_cache; 2978kmem_cache_t *ntfs_big_inode_cache;
2587 2979
@@ -2705,7 +3097,7 @@ static int __init init_ntfs_fs(void)
2705 ntfs_debug("NTFS driver registered successfully."); 3097 ntfs_debug("NTFS driver registered successfully.");
2706 return 0; /* Success! */ 3098 return 0; /* Success! */
2707 } 3099 }
2708 printk(KERN_CRIT "NTFS: Failed to register NTFS file system driver!\n"); 3100 printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
2709 3101
2710sysctl_err_out: 3102sysctl_err_out:
2711 kmem_cache_destroy(ntfs_big_inode_cache); 3103 kmem_cache_destroy(ntfs_big_inode_cache);
@@ -2719,7 +3111,7 @@ actx_err_out:
2719 kmem_cache_destroy(ntfs_index_ctx_cache); 3111 kmem_cache_destroy(ntfs_index_ctx_cache);
2720ictx_err_out: 3112ictx_err_out:
2721 if (!err) { 3113 if (!err) {
2722 printk(KERN_CRIT "NTFS: Aborting NTFS file system driver " 3114 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
2723 "registration...\n"); 3115 "registration...\n");
2724 err = -ENOMEM; 3116 err = -ENOMEM;
2725 } 3117 }
@@ -2759,7 +3151,7 @@ static void __exit exit_ntfs_fs(void)
2759} 3151}
2760 3152
2761MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>"); 3153MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
2762MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2004 Anton Altaparmakov"); 3154MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
2763MODULE_VERSION(NTFS_VERSION); 3155MODULE_VERSION(NTFS_VERSION);
2764MODULE_LICENSE("GPL"); 3156MODULE_LICENSE("GPL");
2765#ifdef DEBUG 3157#ifdef DEBUG