aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/ntfs/ChangeLog4
-rw-r--r--fs/ntfs/Makefile2
-rw-r--r--fs/ntfs/layout.h22
-rw-r--r--fs/ntfs/super.c254
-rw-r--r--fs/ntfs/types.h10
-rw-r--r--fs/ntfs/usnjrnl.c84
-rw-r--r--fs/ntfs/usnjrnl.h205
-rw-r--r--fs/ntfs/volume.h6
8 files changed, 566 insertions, 21 deletions
diff --git a/fs/ntfs/ChangeLog b/fs/ntfs/ChangeLog
index cb86140aa2a3..08c8c04b0216 100644
--- a/fs/ntfs/ChangeLog
+++ b/fs/ntfs/ChangeLog
@@ -1,6 +1,5 @@
1ToDo/Notes: 1ToDo/Notes:
2 - Find and fix bugs. 2 - Find and fix bugs.
3 - Checkpoint or disable the user space journal ($UsnJrnl).
4 - In between ntfs_prepare/commit_write, need exclusion between 3 - In between ntfs_prepare/commit_write, need exclusion between
5 simultaneous file extensions. This is given to us by holding i_sem 4 simultaneous file extensions. This is given to us by holding i_sem
6 on the inode. The only places in the kernel when a file is resized 5 on the inode. The only places in the kernel when a file is resized
@@ -119,6 +118,9 @@ ToDo/Notes:
119 - Use C99 style structure initialization after memory allocation where 118 - Use C99 style structure initialization after memory allocation where
120 possible (fs/ntfs/{attrib.c,index.c,super.c}). Thanks to Al Viro and 119 possible (fs/ntfs/{attrib.c,index.c,super.c}). Thanks to Al Viro and
121 Pekka Enberg. 120 Pekka Enberg.
121 - Stamp the transaction log ($UsnJrnl), aka user space journal, if it
122 is active on the volume and we are mounting read-write or remounting
123 from read-only to read-write.
122 124
1232.1.22 - Many bug and race fixes and error handling improvements. 1252.1.22 - Many bug and race fixes and error handling improvements.
124 126
diff --git a/fs/ntfs/Makefile b/fs/ntfs/Makefile
index f8c97d41226b..59f9606a82a1 100644
--- a/fs/ntfs/Makefile
+++ b/fs/ntfs/Makefile
@@ -15,5 +15,5 @@ endif
15ifeq ($(CONFIG_NTFS_RW),y) 15ifeq ($(CONFIG_NTFS_RW),y)
16EXTRA_CFLAGS += -DNTFS_RW 16EXTRA_CFLAGS += -DNTFS_RW
17 17
18ntfs-objs += bitmap.o lcnalloc.o logfile.o quota.o 18ntfs-objs += bitmap.o lcnalloc.o logfile.o quota.o usnjrnl.o
19endif 19endif
diff --git a/fs/ntfs/layout.h b/fs/ntfs/layout.h
index 458cb541d4dd..03c3e8612e7c 100644
--- a/fs/ntfs/layout.h
+++ b/fs/ntfs/layout.h
@@ -936,20 +936,12 @@ typedef struct {
936 /* 56*/ le64 quota_charged; /* Byte size of the charge to 936 /* 56*/ le64 quota_charged; /* Byte size of the charge to
937 the quota for all streams of the file. Note: Is 937 the quota for all streams of the file. Note: Is
938 zero if quotas are disabled. */ 938 zero if quotas are disabled. */
939 /* 64*/ le64 usn; /* Last update sequence number 939 /* 64*/ USN usn; /* Last update sequence number
940 of the file. This is a direct index into the 940 of the file. This is a direct index into the
941 change (aka usn) journal file. It is zero if 941 transaction log file ($UsnJrnl). It is zero if
942 the usn journal is disabled. 942 the usn journal is disabled or this file has
943 NOTE: To disable the journal need to delete 943 not been subject to logging yet. See usnjrnl.h
944 the journal file itself and to then walk the 944 for details. */
945 whole mft and set all Usn entries in all mft
946 records to zero! (This can take a while!)
947 The journal is FILE_Extend/$UsnJrnl. Win2k
948 will recreate the journal and initiate
949 logging if necessary when mounting the
950 partition. This, in contrast to disabling the
951 journal is a very fast process, so the user
952 won't even notice it. */
953 } __attribute__ ((__packed__)) v3; 945 } __attribute__ ((__packed__)) v3;
954 /* sizeof() = 72 bytes (NTFS 3.x) */ 946 /* sizeof() = 72 bytes (NTFS 3.x) */
955 } __attribute__ ((__packed__)) ver; 947 } __attribute__ ((__packed__)) ver;
@@ -1912,7 +1904,7 @@ enum {
1912 VOLUME_FLAGS_MASK = const_cpu_to_le16(0x803f), 1904 VOLUME_FLAGS_MASK = const_cpu_to_le16(0x803f),
1913 1905
1914 /* To make our life easier when checking if we must mount read-only. */ 1906 /* To make our life easier when checking if we must mount read-only. */
1915 VOLUME_MUST_MOUNT_RO_MASK = const_cpu_to_le16(0x8037), 1907 VOLUME_MUST_MOUNT_RO_MASK = const_cpu_to_le16(0x8027),
1916} __attribute__ ((__packed__)); 1908} __attribute__ ((__packed__));
1917 1909
1918typedef le16 VOLUME_FLAGS; 1910typedef le16 VOLUME_FLAGS;
diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
index 455cbe0a6296..92e1d28219b3 100644
--- a/fs/ntfs/super.c
+++ b/fs/ntfs/super.c
@@ -34,6 +34,7 @@
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"
@@ -497,6 +498,12 @@ static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
497 NVolSetErrors(vol); 498 NVolSetErrors(vol);
498 return -EROFS; 499 return -EROFS;
499 } 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 }
500 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) { 507 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
501 /* Remounting read-only. */ 508 /* Remounting read-only. */
502 if (!NVolErrors(vol)) { 509 if (!NVolErrors(vol)) {
@@ -1219,6 +1226,167 @@ static BOOL load_and_init_quota(ntfs_volume *vol)
1219} 1226}
1220 1227
1221/** 1228/**
1229 * load_and_init_usnjrnl - load and setup the transaction log if present
1230 * @vol: ntfs super block describing device whose usnjrnl file to load
1231 *
1232 * Return TRUE on success or FALSE on error.
1233 *
1234 * If $UsnJrnl is not present or in the process of being disabled, we set
1235 * NVolUsnJrnlStamped() and return success.
1236 *
1237 * If the $UsnJrnl $DATA/$J attribute has a size equal to the lowest valid usn,
1238 * i.e. transaction logging has only just been enabled or the journal has been
1239 * stamped and nothing has been logged since, we also set NVolUsnJrnlStamped()
1240 * and return success.
1241 */
1242static BOOL load_and_init_usnjrnl(ntfs_volume *vol)
1243{
1244 MFT_REF mref;
1245 struct inode *tmp_ino;
1246 ntfs_inode *tmp_ni;
1247 struct page *page;
1248 ntfs_name *name = NULL;
1249 USN_HEADER *uh;
1250 static const ntfschar UsnJrnl[9] = { const_cpu_to_le16('$'),
1251 const_cpu_to_le16('U'), const_cpu_to_le16('s'),
1252 const_cpu_to_le16('n'), const_cpu_to_le16('J'),
1253 const_cpu_to_le16('r'), const_cpu_to_le16('n'),
1254 const_cpu_to_le16('l'), 0 };
1255 static ntfschar Max[5] = { const_cpu_to_le16('$'),
1256 const_cpu_to_le16('M'), const_cpu_to_le16('a'),
1257 const_cpu_to_le16('x'), 0 };
1258 static ntfschar J[3] = { const_cpu_to_le16('$'),
1259 const_cpu_to_le16('J'), 0 };
1260
1261 ntfs_debug("Entering.");
1262 /*
1263 * Find the inode number for the transaction log file by looking up the
1264 * filename $UsnJrnl in the extended system files directory $Extend.
1265 */
1266 down(&vol->extend_ino->i_sem);
1267 mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), UsnJrnl, 8,
1268 &name);
1269 up(&vol->extend_ino->i_sem);
1270 if (IS_ERR_MREF(mref)) {
1271 /*
1272 * If the file does not exist, transaction logging is disabled,
1273 * just return success.
1274 */
1275 if (MREF_ERR(mref) == -ENOENT) {
1276 ntfs_debug("$UsnJrnl not present. Volume does not "
1277 "have transaction logging enabled.");
1278not_enabled:
1279 /*
1280 * No need to try to stamp the transaction log if
1281 * transaction logging is not enabled.
1282 */
1283 NVolSetUsnJrnlStamped(vol);
1284 return TRUE;
1285 }
1286 /* A real error occured. */
1287 ntfs_error(vol->sb, "Failed to find inode number for "
1288 "$UsnJrnl.");
1289 return FALSE;
1290 }
1291 /* We do not care for the type of match that was found. */
1292 kfree(name);
1293 /* Get the inode. */
1294 tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1295 if (unlikely(IS_ERR(tmp_ino) || is_bad_inode(tmp_ino))) {
1296 if (!IS_ERR(tmp_ino))
1297 iput(tmp_ino);
1298 ntfs_error(vol->sb, "Failed to load $UsnJrnl.");
1299 return FALSE;
1300 }
1301 vol->usnjrnl_ino = tmp_ino;
1302 /*
1303 * If the transaction log is in the process of being deleted, we can
1304 * ignore it.
1305 */
1306 if (unlikely(vol->vol_flags & VOLUME_DELETE_USN_UNDERWAY)) {
1307 ntfs_debug("$UsnJrnl in the process of being disabled. "
1308 "Volume does not have transaction logging "
1309 "enabled.");
1310 goto not_enabled;
1311 }
1312 /* Get the $DATA/$Max attribute. */
1313 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, Max, 4);
1314 if (IS_ERR(tmp_ino)) {
1315 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$Max "
1316 "attribute.");
1317 return FALSE;
1318 }
1319 vol->usnjrnl_max_ino = tmp_ino;
1320 if (unlikely(i_size_read(tmp_ino) < sizeof(USN_HEADER))) {
1321 ntfs_error(vol->sb, "Found corrupt $UsnJrnl/$DATA/$Max "
1322 "attribute (size is 0x%llx but should be at "
1323 "least 0x%x bytes).", i_size_read(tmp_ino),
1324 sizeof(USN_HEADER));
1325 return FALSE;
1326 }
1327 /* Get the $DATA/$J attribute. */
1328 tmp_ino = ntfs_attr_iget(vol->usnjrnl_ino, AT_DATA, J, 2);
1329 if (IS_ERR(tmp_ino)) {
1330 ntfs_error(vol->sb, "Failed to load $UsnJrnl/$DATA/$J "
1331 "attribute.");
1332 return FALSE;
1333 }
1334 vol->usnjrnl_j_ino = tmp_ino;
1335 /* Verify $J is non-resident and sparse. */
1336 tmp_ni = NTFS_I(vol->usnjrnl_j_ino);
1337 if (unlikely(!NInoNonResident(tmp_ni) || !NInoSparse(tmp_ni))) {
1338 ntfs_error(vol->sb, "$UsnJrnl/$DATA/$J attribute is resident "
1339 "and/or not sparse.");
1340 return FALSE;
1341 }
1342 /* Read the USN_HEADER from $DATA/$Max. */
1343 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
1344 if (IS_ERR(page)) {
1345 ntfs_error(vol->sb, "Failed to read from $UsnJrnl/$DATA/$Max "
1346 "attribute.");
1347 return FALSE;
1348 }
1349 uh = (USN_HEADER*)page_address(page);
1350 /* Sanity check the $Max. */
1351 if (unlikely(sle64_to_cpu(uh->allocation_delta) >
1352 sle64_to_cpu(uh->maximum_size))) {
1353 ntfs_error(vol->sb, "Allocation delta (0x%llx) exceeds "
1354 "maximum size (0x%llx). $UsnJrnl is corrupt.",
1355 (long long)sle64_to_cpu(uh->allocation_delta),
1356 (long long)sle64_to_cpu(uh->maximum_size));
1357 ntfs_unmap_page(page);
1358 return FALSE;
1359 }
1360 /*
1361 * If the transaction log has been stamped and nothing has been written
1362 * to it since, we do not need to stamp it.
1363 */
1364 if (unlikely(sle64_to_cpu(uh->lowest_valid_usn) >=
1365 i_size_read(vol->usnjrnl_j_ino))) {
1366 if (likely(sle64_to_cpu(uh->lowest_valid_usn) ==
1367 i_size_read(vol->usnjrnl_j_ino))) {
1368 ntfs_unmap_page(page);
1369 ntfs_debug("$UsnJrnl is enabled but nothing has been "
1370 "logged since it was last stamped. "
1371 "Treating this as if the volume does "
1372 "not have transaction logging "
1373 "enabled.");
1374 goto not_enabled;
1375 }
1376 ntfs_error(vol->sb, "$UsnJrnl has lowest valid usn (0x%llx) "
1377 "which is out of bounds (0x%llx). $UsnJrnl "
1378 "is corrupt.",
1379 (long long)sle64_to_cpu(uh->lowest_valid_usn),
1380 i_size_read(vol->usnjrnl_j_ino));
1381 ntfs_unmap_page(page);
1382 return FALSE;
1383 }
1384 ntfs_unmap_page(page);
1385 ntfs_debug("Done.");
1386 return TRUE;
1387}
1388
1389/**
1222 * load_and_init_attrdef - load the attribute definitions table for a volume 1390 * load_and_init_attrdef - load the attribute definitions table for a volume
1223 * @vol: ntfs super block describing device whose attrdef to load 1391 * @vol: ntfs super block describing device whose attrdef to load
1224 * 1392 *
@@ -1653,7 +1821,7 @@ get_ctx_vol_failed:
1653 goto iput_logfile_err_out; 1821 goto iput_logfile_err_out;
1654 } 1822 }
1655 /* If on NTFS versions before 3.0, we are done. */ 1823 /* If on NTFS versions before 3.0, we are done. */
1656 if (vol->major_ver < 3) 1824 if (unlikely(vol->major_ver < 3))
1657 return TRUE; 1825 return TRUE;
1658 /* NTFS 3.0+ specific initialization. */ 1826 /* NTFS 3.0+ specific initialization. */
1659 /* Get the security descriptors inode. */ 1827 /* Get the security descriptors inode. */
@@ -1664,7 +1832,7 @@ get_ctx_vol_failed:
1664 ntfs_error(sb, "Failed to load $Secure."); 1832 ntfs_error(sb, "Failed to load $Secure.");
1665 goto iput_root_err_out; 1833 goto iput_root_err_out;
1666 } 1834 }
1667 // FIXME: Initialize security. 1835 // TODO: Initialize security.
1668 /* Get the extended system files' directory inode. */ 1836 /* Get the extended system files' directory inode. */
1669 vol->extend_ino = ntfs_iget(sb, FILE_Extend); 1837 vol->extend_ino = ntfs_iget(sb, FILE_Extend);
1670 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) { 1838 if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
@@ -1715,10 +1883,60 @@ get_ctx_vol_failed:
1715 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME; 1883 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1716 NVolSetErrors(vol); 1884 NVolSetErrors(vol);
1717 } 1885 }
1718 // TODO: Delete or checkpoint the $UsnJrnl if it exists. 1886 /*
1887 * Find the transaction log file ($UsnJrnl), load it if present, check
1888 * it, and set it up.
1889 */
1890 if (!load_and_init_usnjrnl(vol)) {
1891 static const char *es1 = "Failed to load $UsnJrnl";
1892 static const char *es2 = ". Run chkdsk.";
1893
1894 /* If a read-write mount, convert it to a read-only mount. */
1895 if (!(sb->s_flags & MS_RDONLY)) {
1896 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1897 ON_ERRORS_CONTINUE))) {
1898 ntfs_error(sb, "%s and neither on_errors="
1899 "continue nor on_errors="
1900 "remount-ro was specified%s",
1901 es1, es2);
1902 goto iput_usnjrnl_err_out;
1903 }
1904 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1905 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1906 } else
1907 ntfs_warning(sb, "%s. Will not be able to remount "
1908 "read-write%s", es1, es2);
1909 /* This will prevent a read-write remount. */
1910 NVolSetErrors(vol);
1911 }
1912 /* If (still) a read-write mount, stamp the transaction log. */
1913 if (!(sb->s_flags & MS_RDONLY) && !ntfs_stamp_usnjrnl(vol)) {
1914 static const char *es1 = "Failed to stamp transaction log "
1915 "($UsnJrnl)";
1916 static const char *es2 = ". Run chkdsk.";
1917
1918 /* Convert to a read-only mount. */
1919 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1920 ON_ERRORS_CONTINUE))) {
1921 ntfs_error(sb, "%s and neither on_errors=continue nor "
1922 "on_errors=remount-ro was specified%s",
1923 es1, es2);
1924 goto iput_usnjrnl_err_out;
1925 }
1926 ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
1927 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1928 NVolSetErrors(vol);
1929 }
1719#endif /* NTFS_RW */ 1930#endif /* NTFS_RW */
1720 return TRUE; 1931 return TRUE;
1721#ifdef NTFS_RW 1932#ifdef NTFS_RW
1933iput_usnjrnl_err_out:
1934 if (vol->usnjrnl_j_ino)
1935 iput(vol->usnjrnl_j_ino);
1936 if (vol->usnjrnl_max_ino)
1937 iput(vol->usnjrnl_max_ino);
1938 if (vol->usnjrnl_ino)
1939 iput(vol->usnjrnl_ino);
1722iput_quota_err_out: 1940iput_quota_err_out:
1723 if (vol->quota_q_ino) 1941 if (vol->quota_q_ino)
1724 iput(vol->quota_q_ino); 1942 iput(vol->quota_q_ino);
@@ -1792,6 +2010,12 @@ static void ntfs_put_super(struct super_block *sb)
1792 2010
1793 /* NTFS 3.0+ specific. */ 2011 /* NTFS 3.0+ specific. */
1794 if (vol->major_ver >= 3) { 2012 if (vol->major_ver >= 3) {
2013 if (vol->usnjrnl_j_ino)
2014 ntfs_commit_inode(vol->usnjrnl_j_ino);
2015 if (vol->usnjrnl_max_ino)
2016 ntfs_commit_inode(vol->usnjrnl_max_ino);
2017 if (vol->usnjrnl_ino)
2018 ntfs_commit_inode(vol->usnjrnl_ino);
1795 if (vol->quota_q_ino) 2019 if (vol->quota_q_ino)
1796 ntfs_commit_inode(vol->quota_q_ino); 2020 ntfs_commit_inode(vol->quota_q_ino);
1797 if (vol->quota_ino) 2021 if (vol->quota_ino)
@@ -1847,6 +2071,18 @@ static void ntfs_put_super(struct super_block *sb)
1847 /* NTFS 3.0+ specific clean up. */ 2071 /* NTFS 3.0+ specific clean up. */
1848 if (vol->major_ver >= 3) { 2072 if (vol->major_ver >= 3) {
1849#ifdef NTFS_RW 2073#ifdef NTFS_RW
2074 if (vol->usnjrnl_j_ino) {
2075 iput(vol->usnjrnl_j_ino);
2076 vol->usnjrnl_j_ino = NULL;
2077 }
2078 if (vol->usnjrnl_max_ino) {
2079 iput(vol->usnjrnl_max_ino);
2080 vol->usnjrnl_max_ino = NULL;
2081 }
2082 if (vol->usnjrnl_ino) {
2083 iput(vol->usnjrnl_ino);
2084 vol->usnjrnl_ino = NULL;
2085 }
1850 if (vol->quota_q_ino) { 2086 if (vol->quota_q_ino) {
1851 iput(vol->quota_q_ino); 2087 iput(vol->quota_q_ino);
1852 vol->quota_q_ino = NULL; 2088 vol->quota_q_ino = NULL;
@@ -2463,6 +2699,18 @@ static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2463 /* NTFS 3.0+ specific clean up. */ 2699 /* NTFS 3.0+ specific clean up. */
2464 if (vol->major_ver >= 3) { 2700 if (vol->major_ver >= 3) {
2465#ifdef NTFS_RW 2701#ifdef NTFS_RW
2702 if (vol->usnjrnl_j_ino) {
2703 iput(vol->usnjrnl_j_ino);
2704 vol->usnjrnl_j_ino = NULL;
2705 }
2706 if (vol->usnjrnl_max_ino) {
2707 iput(vol->usnjrnl_max_ino);
2708 vol->usnjrnl_max_ino = NULL;
2709 }
2710 if (vol->usnjrnl_ino) {
2711 iput(vol->usnjrnl_ino);
2712 vol->usnjrnl_ino = NULL;
2713 }
2466 if (vol->quota_q_ino) { 2714 if (vol->quota_q_ino) {
2467 iput(vol->quota_q_ino); 2715 iput(vol->quota_q_ino);
2468 vol->quota_q_ino = NULL; 2716 vol->quota_q_ino = NULL;
diff --git a/fs/ntfs/types.h b/fs/ntfs/types.h
index 08a55aa53d4e..6e4a7e3343f2 100644
--- a/fs/ntfs/types.h
+++ b/fs/ntfs/types.h
@@ -2,7 +2,7 @@
2 * types.h - Defines for NTFS Linux kernel driver specific types. 2 * types.h - Defines for NTFS Linux kernel driver specific types.
3 * Part of the Linux-NTFS project. 3 * Part of the Linux-NTFS project.
4 * 4 *
5 * Copyright (c) 2001-2004 Anton Altaparmakov 5 * Copyright (c) 2001-2005 Anton Altaparmakov
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
8 * modify it under the terms of the GNU General Public License as published 8 * modify it under the terms of the GNU General Public License as published
@@ -53,6 +53,14 @@ typedef sle64 leLCN;
53typedef s64 LSN; 53typedef s64 LSN;
54typedef sle64 leLSN; 54typedef sle64 leLSN;
55 55
56/*
57 * The NTFS transaction log $UsnJrnl uses usn which are signed 64-bit values.
58 * We define our own type USN, to allow for type checking and better code
59 * readability.
60 */
61typedef s64 USN;
62typedef sle64 leUSN;
63
56typedef enum { 64typedef enum {
57 FALSE = 0, 65 FALSE = 0,
58 TRUE = 1 66 TRUE = 1
diff --git a/fs/ntfs/usnjrnl.c b/fs/ntfs/usnjrnl.c
new file mode 100644
index 000000000000..77773240d139
--- /dev/null
+++ b/fs/ntfs/usnjrnl.c
@@ -0,0 +1,84 @@
1/*
2 * usnjrnl.h - NTFS kernel transaction log ($UsnJrnl) handling. Part of the
3 * Linux-NTFS project.
4 *
5 * Copyright (c) 2005 Anton Altaparmakov
6 *
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#ifdef NTFS_RW
24
25#include <linux/fs.h>
26#include <linux/highmem.h>
27#include <linux/mm.h>
28
29#include "aops.h"
30#include "debug.h"
31#include "endian.h"
32#include "time.h"
33#include "types.h"
34#include "usnjrnl.h"
35#include "volume.h"
36
37/**
38 * ntfs_stamp_usnjrnl - stamp the transaction log ($UsnJrnl) on an ntfs volume
39 * @vol: ntfs volume on which to stamp the transaction log
40 *
41 * Stamp the transaction log ($UsnJrnl) on the ntfs volume @vol and return
42 * TRUE on success and FALSE on error.
43 *
44 * This function assumes that the transaction log has already been loaded and
45 * consistency checked by a call to fs/ntfs/super.c::load_and_init_usnjrnl().
46 */
47BOOL ntfs_stamp_usnjrnl(ntfs_volume *vol)
48{
49 ntfs_debug("Entering.");
50 if (likely(!NVolUsnJrnlStamped(vol))) {
51 sle64 stamp;
52 struct page *page;
53 USN_HEADER *uh;
54
55 page = ntfs_map_page(vol->usnjrnl_max_ino->i_mapping, 0);
56 if (IS_ERR(page)) {
57 ntfs_error(vol->sb, "Failed to read from "
58 "$UsnJrnl/$DATA/$Max attribute.");
59 return FALSE;
60 }
61 uh = (USN_HEADER*)page_address(page);
62 stamp = get_current_ntfs_time();
63 ntfs_debug("Stamping transaction log ($UsnJrnl): old "
64 "journal_id 0x%llx, old lowest_valid_usn "
65 "0x%llx, new journal_id 0x%llx, new "
66 "lowest_valid_usn 0x%llx.",
67 (long long)sle64_to_cpu(uh->journal_id),
68 (long long)sle64_to_cpu(uh->lowest_valid_usn),
69 (long long)sle64_to_cpu(stamp),
70 i_size_read(vol->usnjrnl_j_ino));
71 uh->lowest_valid_usn =
72 cpu_to_sle64(i_size_read(vol->usnjrnl_j_ino));
73 uh->journal_id = stamp;
74 flush_dcache_page(page);
75 set_page_dirty(page);
76 ntfs_unmap_page(page);
77 /* Set the flag so we do not have to do it again on remount. */
78 NVolSetUsnJrnlStamped(vol);
79 }
80 ntfs_debug("Done.");
81 return TRUE;
82}
83
84#endif /* NTFS_RW */
diff --git a/fs/ntfs/usnjrnl.h b/fs/ntfs/usnjrnl.h
new file mode 100644
index 000000000000..ff988b0deb45
--- /dev/null
+++ b/fs/ntfs/usnjrnl.h
@@ -0,0 +1,205 @@
1/*
2 * usnjrnl.h - Defines for NTFS kernel transaction log ($UsnJrnl) handling.
3 * Part of the Linux-NTFS project.
4 *
5 * Copyright (c) 2005 Anton Altaparmakov
6 *
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#ifndef _LINUX_NTFS_USNJRNL_H
24#define _LINUX_NTFS_USNJRNL_H
25
26#ifdef NTFS_RW
27
28#include "types.h"
29#include "endian.h"
30#include "layout.h"
31#include "volume.h"
32
33/*
34 * Transaction log ($UsnJrnl) organization:
35 *
36 * The transaction log records whenever a file is modified in any way. So for
37 * example it will record that file "blah" was written to at a particular time
38 * but not what was written. If will record that a file was deleted or
39 * created, that a file was truncated, etc. See below for all the reason
40 * codes used.
41 *
42 * The transaction log is in the $Extend directory which is in the root
43 * directory of each volume. If it is not present it means transaction
44 * logging is disabled. If it is present it means transaction logging is
45 * either enabled or in the process of being disabled in which case we can
46 * ignore it as it will go away as soon as Windows gets its hands on it.
47 *
48 * To determine whether the transaction logging is enabled or in the process
49 * of being disabled, need to check the volume flags in the
50 * $VOLUME_INFORMATION attribute in the $Volume system file (which is present
51 * in the root directory and has a fixed mft record number, see layout.h).
52 * If the flag VOLUME_DELETE_USN_UNDERWAY is set it means the transaction log
53 * is in the process of being disabled and if this flag is clear it means the
54 * transaction log is enabled.
55 *
56 * The transaction log consists of two parts; the $DATA/$Max attribute as well
57 * as the $DATA/$J attribute. $Max is a header describing the transaction
58 * log whilst $J is the transaction log data itself as a sequence of variable
59 * sized USN_RECORDs (see below for all the structures).
60 *
61 * We do not care about transaction logging at this point in time but we still
62 * need to let windows know that the transaction log is out of date. To do
63 * this we need to stamp the transaction log. This involves setting the
64 * lowest_valid_usn field in the $DATA/$Max attribute to the usn to be used
65 * for the next added USN_RECORD to the $DATA/$J attribute as well as
66 * generating a new journal_id in $DATA/$Max.
67 *
68 * The journal_id is as of the current version (2.0) of the transaction log
69 * simply the 64-bit timestamp of when the journal was either created or last
70 * stamped.
71 *
72 * To determine the next usn there are two ways. The first is to parse
73 * $DATA/$J and to find the last USN_RECORD in it and to add its record_length
74 * to its usn (which is the byte offset in the $DATA/$J attribute). The
75 * second is simply to take the data size of the attribute. Since the usns
76 * are simply byte offsets into $DATA/$J, this is exactly the next usn. For
77 * obvious reasons we use the second method as it is much simpler and faster.
78 *
79 * As an aside, note that to actually disable the transaction log, one would
80 * need to set the VOLUME_DELETE_USN_UNDERWAY flag (see above), then go
81 * through all the mft records on the volume and set the usn field in their
82 * $STANDARD_INFORMATION attribute to zero. Once that is done, one would need
83 * to delete the transaction log file, i.e. \$Extent\$UsnJrnl, and finally,
84 * one would need to clear the VOLUME_DELETE_USN_UNDERWAY flag.
85 *
86 * Note that if a volume is unmounted whilst the transaction log is being
87 * disabled, the process will continue the next time the volume is mounted.
88 * This is why we can safely mount read-write when we see a transaction log
89 * in the process of being deleted.
90 */
91
92/* Some $UsnJrnl related constants. */
93#define UsnJrnlMajorVer 2
94#define UsnJrnlMinorVer 0
95
96/*
97 * $DATA/$Max attribute. This is (always?) resident and has a fixed size of
98 * 32 bytes. It contains the header describing the transaction log.
99 */
100typedef struct {
101/*Ofs*/
102/* 0*/sle64 maximum_size; /* The maximum on-disk size of the $DATA/$J
103 attribute. */
104/* 8*/sle64 allocation_delta; /* Number of bytes by which to increase the
105 size of the $DATA/$J attribute. */
106/*0x10*/sle64 journal_id; /* Current id of the transaction log. */
107/*0x18*/leUSN lowest_valid_usn; /* Lowest valid usn in $DATA/$J for the
108 current journal_id. */
109/* sizeof() = 32 (0x20) bytes */
110} __attribute__ ((__packed__)) USN_HEADER;
111
112/*
113 * Reason flags (32-bit). Cumulative flags describing the change(s) to the
114 * file since it was last opened. I think the names speak for themselves but
115 * if you disagree check out the descriptions in the Linux NTFS project NTFS
116 * documentation: http://linux-ntfs.sourceforge.net/ntfs/files/usnjrnl.html
117 */
118enum {
119 USN_REASON_DATA_OVERWRITE = const_cpu_to_le32(0x00000001),
120 USN_REASON_DATA_EXTEND = const_cpu_to_le32(0x00000002),
121 USN_REASON_DATA_TRUNCATION = const_cpu_to_le32(0x00000004),
122 USN_REASON_NAMED_DATA_OVERWRITE = const_cpu_to_le32(0x00000010),
123 USN_REASON_NAMED_DATA_EXTEND = const_cpu_to_le32(0x00000020),
124 USN_REASON_NAMED_DATA_TRUNCATION= const_cpu_to_le32(0x00000040),
125 USN_REASON_FILE_CREATE = const_cpu_to_le32(0x00000100),
126 USN_REASON_FILE_DELETE = const_cpu_to_le32(0x00000200),
127 USN_REASON_EA_CHANGE = const_cpu_to_le32(0x00000400),
128 USN_REASON_SECURITY_CHANGE = const_cpu_to_le32(0x00000800),
129 USN_REASON_RENAME_OLD_NAME = const_cpu_to_le32(0x00001000),
130 USN_REASON_RENAME_NEW_NAME = const_cpu_to_le32(0x00002000),
131 USN_REASON_INDEXABLE_CHANGE = const_cpu_to_le32(0x00004000),
132 USN_REASON_BASIC_INFO_CHANGE = const_cpu_to_le32(0x00008000),
133 USN_REASON_HARD_LINK_CHANGE = const_cpu_to_le32(0x00010000),
134 USN_REASON_COMPRESSION_CHANGE = const_cpu_to_le32(0x00020000),
135 USN_REASON_ENCRYPTION_CHANGE = const_cpu_to_le32(0x00040000),
136 USN_REASON_OBJECT_ID_CHANGE = const_cpu_to_le32(0x00080000),
137 USN_REASON_REPARSE_POINT_CHANGE = const_cpu_to_le32(0x00100000),
138 USN_REASON_STREAM_CHANGE = const_cpu_to_le32(0x00200000),
139 USN_REASON_CLOSE = const_cpu_to_le32(0x80000000),
140};
141
142typedef le32 USN_REASON_FLAGS;
143
144/*
145 * Source info flags (32-bit). Information about the source of the change(s)
146 * to the file. For detailed descriptions of what these mean, see the Linux
147 * NTFS project NTFS documentation:
148 * http://linux-ntfs.sourceforge.net/ntfs/files/usnjrnl.html
149 */
150enum {
151 USN_SOURCE_DATA_MANAGEMENT = const_cpu_to_le32(0x00000001),
152 USN_SOURCE_AUXILIARY_DATA = const_cpu_to_le32(0x00000002),
153 USN_SOURCE_REPLICATION_MANAGEMENT = const_cpu_to_le32(0x00000004),
154};
155
156typedef le32 USN_SOURCE_INFO_FLAGS;
157
158/*
159 * $DATA/$J attribute. This is always non-resident, is marked as sparse, and
160 * is of variabled size. It consists of a sequence of variable size
161 * USN_RECORDS. The minimum allocated_size is allocation_delta as
162 * specified in $DATA/$Max. When the maximum_size specified in $DATA/$Max is
163 * exceeded by more than allocation_delta bytes, allocation_delta bytes are
164 * allocated and appended to the $DATA/$J attribute and an equal number of
165 * bytes at the beginning of the attribute are freed and made sparse. Note the
166 * making sparse only happens at volume checkpoints and hence the actual
167 * $DATA/$J size can exceed maximum_size + allocation_delta temporarily.
168 */
169typedef struct {
170/*Ofs*/
171/* 0*/le32 length; /* Byte size of this record (8-byte
172 aligned). */
173/* 4*/le16 major_ver; /* Major version of the transaction log used
174 for this record. */
175/* 6*/le16 minor_ver; /* Minor version of the transaction log used
176 for this record. */
177/* 8*/leMFT_REF mft_reference;/* The mft reference of the file (or
178 directory) described by this record. */
179/*0x10*/leMFT_REF parent_directory;/* The mft reference of the parent
180 directory of the file described by this
181 record. */
182/*0x18*/leUSN usn; /* The usn of this record. Equals the offset
183 within the $DATA/$J attribute. */
184/*0x20*/sle64 time; /* Time when this record was created. */
185/*0x28*/USN_REASON_FLAGS reason;/* Reason flags (see above). */
186/*0x2c*/USN_SOURCE_INFO_FLAGS source_info;/* Source info flags (see above). */
187/*0x30*/le32 security_id; /* File security_id copied from
188 $STANDARD_INFORMATION. */
189/*0x34*/FILE_ATTR_FLAGS file_attributes; /* File attributes copied from
190 $STANDARD_INFORMATION or $FILE_NAME (not
191 sure which). */
192/*0x38*/le16 file_name_size; /* Size of the file name in bytes. */
193/*0x3a*/le16 file_name_offset; /* Offset to the file name in bytes from the
194 start of this record. */
195/*0x3c*/ntfschar file_name[0]; /* Use when creating only. When reading use
196 file_name_offset to determine the location
197 of the name. */
198/* sizeof() = 60 (0x3c) bytes */
199} __attribute__ ((__packed__)) USN_RECORD;
200
201extern BOOL ntfs_stamp_usnjrnl(ntfs_volume *vol);
202
203#endif /* NTFS_RW */
204
205#endif /* _LINUX_NTFS_USNJRNL_H */
diff --git a/fs/ntfs/volume.h b/fs/ntfs/volume.h
index 62be73ad0156..375cd20a9f61 100644
--- a/fs/ntfs/volume.h
+++ b/fs/ntfs/volume.h
@@ -125,6 +125,10 @@ typedef struct {
125 /* $Quota stuff is NTFS3.0+ specific. Unused/NULL otherwise. */ 125 /* $Quota stuff is NTFS3.0+ specific. Unused/NULL otherwise. */
126 struct inode *quota_ino; /* The VFS inode of $Quota. */ 126 struct inode *quota_ino; /* The VFS inode of $Quota. */
127 struct inode *quota_q_ino; /* Attribute inode for $Quota/$Q. */ 127 struct inode *quota_q_ino; /* Attribute inode for $Quota/$Q. */
128 /* $UsnJrnl stuff is NTFS3.0+ specific. Unused/NULL otherwise. */
129 struct inode *usnjrnl_ino; /* The VFS inode of $UsnJrnl. */
130 struct inode *usnjrnl_max_ino; /* Attribute inode for $UsnJrnl/$Max. */
131 struct inode *usnjrnl_j_ino; /* Attribute inode for $UsnJrnl/$J. */
128#endif /* NTFS_RW */ 132#endif /* NTFS_RW */
129 struct nls_table *nls_map; 133 struct nls_table *nls_map;
130} ntfs_volume; 134} ntfs_volume;
@@ -141,6 +145,7 @@ typedef enum {
141 file names in WIN32 namespace. */ 145 file names in WIN32 namespace. */
142 NV_LogFileEmpty, /* 1: $LogFile journal is empty. */ 146 NV_LogFileEmpty, /* 1: $LogFile journal is empty. */
143 NV_QuotaOutOfDate, /* 1: $Quota is out of date. */ 147 NV_QuotaOutOfDate, /* 1: $Quota is out of date. */
148 NV_UsnJrnlStamped, /* 1: $UsnJrnl has been stamped. */
144 NV_SparseEnabled, /* 1: May create sparse files. */ 149 NV_SparseEnabled, /* 1: May create sparse files. */
145} ntfs_volume_flags; 150} ntfs_volume_flags;
146 151
@@ -168,6 +173,7 @@ NVOL_FNS(ShowSystemFiles)
168NVOL_FNS(CaseSensitive) 173NVOL_FNS(CaseSensitive)
169NVOL_FNS(LogFileEmpty) 174NVOL_FNS(LogFileEmpty)
170NVOL_FNS(QuotaOutOfDate) 175NVOL_FNS(QuotaOutOfDate)
176NVOL_FNS(UsnJrnlStamped)
171NVOL_FNS(SparseEnabled) 177NVOL_FNS(SparseEnabled)
172 178
173#endif /* _LINUX_NTFS_VOLUME_H */ 179#endif /* _LINUX_NTFS_VOLUME_H */