Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project. |
| 3 | * |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 4 | * Copyright (c) 2001-2005 Anton Altaparmakov |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright (c) 2002 Richard Russon |
| 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 | #include <linux/buffer_head.h> |
Anton Altaparmakov | 1ef334d | 2005-04-04 14:59:42 +0100 | [diff] [blame] | 24 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include "attrib.h" |
| 27 | #include "debug.h" |
| 28 | #include "layout.h" |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 29 | #include "lcnalloc.h" |
| 30 | #include "malloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "mft.h" |
| 32 | #include "ntfs.h" |
| 33 | #include "types.h" |
| 34 | |
| 35 | /** |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 36 | * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * @ni: ntfs inode for which to map (part of) a runlist |
| 38 | * @vcn: map runlist part containing this vcn |
| 39 | * |
| 40 | * Map the part of a runlist containing the @vcn of the ntfs inode @ni. |
| 41 | * |
| 42 | * Return 0 on success and -errno on error. |
| 43 | * |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 44 | * Locking: - The runlist must be locked for writing. |
| 45 | * - This function modifies the runlist. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | */ |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 47 | int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | ntfs_inode *base_ni; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | MFT_RECORD *mrec; |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 51 | ntfs_attr_search_ctx *ctx; |
| 52 | runlist_element *rl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | int err = 0; |
| 54 | |
| 55 | ntfs_debug("Mapping runlist part containing vcn 0x%llx.", |
| 56 | (unsigned long long)vcn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | if (!NInoAttr(ni)) |
| 58 | base_ni = ni; |
| 59 | else |
| 60 | base_ni = ni->ext.base_ntfs_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | mrec = map_mft_record(base_ni); |
| 62 | if (IS_ERR(mrec)) |
| 63 | return PTR_ERR(mrec); |
| 64 | ctx = ntfs_attr_get_search_ctx(base_ni, mrec); |
| 65 | if (unlikely(!ctx)) { |
| 66 | err = -ENOMEM; |
| 67 | goto err_out; |
| 68 | } |
| 69 | err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, |
| 70 | CASE_SENSITIVE, vcn, NULL, 0, ctx); |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 71 | if (likely(!err)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | rl = ntfs_mapping_pairs_decompress(ni->vol, ctx->attr, |
| 73 | ni->runlist.rl); |
| 74 | if (IS_ERR(rl)) |
| 75 | err = PTR_ERR(rl); |
| 76 | else |
| 77 | ni->runlist.rl = rl; |
| 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | ntfs_attr_put_search_ctx(ctx); |
| 80 | err_out: |
| 81 | unmap_mft_record(base_ni); |
| 82 | return err; |
| 83 | } |
| 84 | |
| 85 | /** |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 86 | * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode |
| 87 | * @ni: ntfs inode for which to map (part of) a runlist |
| 88 | * @vcn: map runlist part containing this vcn |
| 89 | * |
| 90 | * Map the part of a runlist containing the @vcn of the ntfs inode @ni. |
| 91 | * |
| 92 | * Return 0 on success and -errno on error. |
| 93 | * |
| 94 | * Locking: - The runlist must be unlocked on entry and is unlocked on return. |
| 95 | * - This function takes the runlist lock for writing and modifies the |
| 96 | * runlist. |
| 97 | */ |
| 98 | int ntfs_map_runlist(ntfs_inode *ni, VCN vcn) |
| 99 | { |
| 100 | int err = 0; |
| 101 | |
| 102 | down_write(&ni->runlist.lock); |
| 103 | /* Make sure someone else didn't do the work while we were sleeping. */ |
| 104 | if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <= |
| 105 | LCN_RL_NOT_MAPPED)) |
| 106 | err = ntfs_map_runlist_nolock(ni, vcn); |
| 107 | up_write(&ni->runlist.lock); |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | /** |
Anton Altaparmakov | 271849a | 2005-03-07 21:36:18 +0000 | [diff] [blame] | 112 | * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode |
| 113 | * @ni: ntfs inode of the attribute whose runlist to search |
| 114 | * @vcn: vcn to convert |
| 115 | * @write_locked: true if the runlist is locked for writing |
| 116 | * |
| 117 | * Find the virtual cluster number @vcn in the runlist of the ntfs attribute |
| 118 | * described by the ntfs inode @ni and return the corresponding logical cluster |
| 119 | * number (lcn). |
| 120 | * |
| 121 | * If the @vcn is not mapped yet, the attempt is made to map the attribute |
| 122 | * extent containing the @vcn and the vcn to lcn conversion is retried. |
| 123 | * |
| 124 | * If @write_locked is true the caller has locked the runlist for writing and |
| 125 | * if false for reading. |
| 126 | * |
| 127 | * Since lcns must be >= 0, we use negative return codes with special meaning: |
| 128 | * |
| 129 | * Return code Meaning / Description |
| 130 | * ========================================== |
| 131 | * LCN_HOLE Hole / not allocated on disk. |
| 132 | * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds. |
| 133 | * LCN_ENOMEM Not enough memory to map runlist. |
| 134 | * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc). |
| 135 | * |
| 136 | * Locking: - The runlist must be locked on entry and is left locked on return. |
| 137 | * - If @write_locked is FALSE, i.e. the runlist is locked for reading, |
| 138 | * the lock may be dropped inside the function so you cannot rely on |
| 139 | * the runlist still being the same when this function returns. |
| 140 | */ |
| 141 | LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn, |
| 142 | const BOOL write_locked) |
| 143 | { |
| 144 | LCN lcn; |
| 145 | BOOL is_retry = FALSE; |
| 146 | |
| 147 | ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.", |
| 148 | ni->mft_no, (unsigned long long)vcn, |
| 149 | write_locked ? "write" : "read"); |
| 150 | BUG_ON(!ni); |
| 151 | BUG_ON(!NInoNonResident(ni)); |
| 152 | BUG_ON(vcn < 0); |
| 153 | retry_remap: |
| 154 | /* Convert vcn to lcn. If that fails map the runlist and retry once. */ |
| 155 | lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn); |
| 156 | if (likely(lcn >= LCN_HOLE)) { |
| 157 | ntfs_debug("Done, lcn 0x%llx.", (long long)lcn); |
| 158 | return lcn; |
| 159 | } |
| 160 | if (lcn != LCN_RL_NOT_MAPPED) { |
| 161 | if (lcn != LCN_ENOENT) |
| 162 | lcn = LCN_EIO; |
| 163 | } else if (!is_retry) { |
| 164 | int err; |
| 165 | |
| 166 | if (!write_locked) { |
| 167 | up_read(&ni->runlist.lock); |
| 168 | down_write(&ni->runlist.lock); |
| 169 | if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) != |
| 170 | LCN_RL_NOT_MAPPED)) { |
| 171 | up_write(&ni->runlist.lock); |
| 172 | down_read(&ni->runlist.lock); |
| 173 | goto retry_remap; |
| 174 | } |
| 175 | } |
| 176 | err = ntfs_map_runlist_nolock(ni, vcn); |
| 177 | if (!write_locked) { |
| 178 | up_write(&ni->runlist.lock); |
| 179 | down_read(&ni->runlist.lock); |
| 180 | } |
| 181 | if (likely(!err)) { |
| 182 | is_retry = TRUE; |
| 183 | goto retry_remap; |
| 184 | } |
| 185 | if (err == -ENOENT) |
| 186 | lcn = LCN_ENOENT; |
| 187 | else if (err == -ENOMEM) |
| 188 | lcn = LCN_ENOMEM; |
| 189 | else |
| 190 | lcn = LCN_EIO; |
| 191 | } |
| 192 | if (lcn != LCN_ENOENT) |
| 193 | ntfs_error(ni->vol->sb, "Failed with error code %lli.", |
| 194 | (long long)lcn); |
| 195 | return lcn; |
| 196 | } |
| 197 | |
| 198 | /** |
Anton Altaparmakov | c0c1cc0 | 2005-03-07 21:43:38 +0000 | [diff] [blame] | 199 | * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 200 | * @ni: ntfs inode describing the runlist to search |
| 201 | * @vcn: vcn to find |
| 202 | * @write_locked: true if the runlist is locked for writing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | * |
| 204 | * Find the virtual cluster number @vcn in the runlist described by the ntfs |
| 205 | * inode @ni and return the address of the runlist element containing the @vcn. |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 206 | * |
Anton Altaparmakov | c0c1cc0 | 2005-03-07 21:43:38 +0000 | [diff] [blame] | 207 | * If the @vcn is not mapped yet, the attempt is made to map the attribute |
| 208 | * extent containing the @vcn and the vcn to lcn conversion is retried. |
| 209 | * |
| 210 | * If @write_locked is true the caller has locked the runlist for writing and |
| 211 | * if false for reading. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | * |
| 213 | * Note you need to distinguish between the lcn of the returned runlist element |
| 214 | * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on |
| 215 | * read and allocate clusters on write. |
| 216 | * |
| 217 | * Return the runlist element containing the @vcn on success and |
| 218 | * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR() |
| 219 | * to decide if the return is success or failure and PTR_ERR() to get to the |
| 220 | * error code if IS_ERR() is true. |
| 221 | * |
| 222 | * The possible error return codes are: |
| 223 | * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds. |
| 224 | * -ENOMEM - Not enough memory to map runlist. |
| 225 | * -EIO - Critical error (runlist/file is corrupt, i/o error, etc). |
| 226 | * |
Anton Altaparmakov | c0c1cc0 | 2005-03-07 21:43:38 +0000 | [diff] [blame] | 227 | * Locking: - The runlist must be locked on entry and is left locked on return. |
| 228 | * - If @write_locked is FALSE, i.e. the runlist is locked for reading, |
| 229 | * the lock may be dropped inside the function so you cannot rely on |
| 230 | * the runlist still being the same when this function returns. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | */ |
Anton Altaparmakov | c0c1cc0 | 2005-03-07 21:43:38 +0000 | [diff] [blame] | 232 | runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn, |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 233 | const BOOL write_locked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | runlist_element *rl; |
| 236 | int err = 0; |
| 237 | BOOL is_retry = FALSE; |
| 238 | |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 239 | ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | ni->mft_no, (unsigned long long)vcn, |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 241 | write_locked ? "write" : "read"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | BUG_ON(!ni); |
| 243 | BUG_ON(!NInoNonResident(ni)); |
| 244 | BUG_ON(vcn < 0); |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 245 | retry_remap: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | rl = ni->runlist.rl; |
| 247 | if (likely(rl && vcn >= rl[0].vcn)) { |
| 248 | while (likely(rl->length)) { |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 249 | if (unlikely(vcn < rl[1].vcn)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | if (likely(rl->lcn >= LCN_HOLE)) { |
| 251 | ntfs_debug("Done."); |
| 252 | return rl; |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | rl++; |
| 257 | } |
| 258 | if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) { |
| 259 | if (likely(rl->lcn == LCN_ENOENT)) |
| 260 | err = -ENOENT; |
| 261 | else |
| 262 | err = -EIO; |
| 263 | } |
| 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | if (!err && !is_retry) { |
| 266 | /* |
| 267 | * The @vcn is in an unmapped region, map the runlist and |
| 268 | * retry. |
| 269 | */ |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 270 | if (!write_locked) { |
| 271 | up_read(&ni->runlist.lock); |
| 272 | down_write(&ni->runlist.lock); |
Anton Altaparmakov | c0c1cc0 | 2005-03-07 21:43:38 +0000 | [diff] [blame] | 273 | if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) != |
| 274 | LCN_RL_NOT_MAPPED)) { |
| 275 | up_write(&ni->runlist.lock); |
| 276 | down_read(&ni->runlist.lock); |
| 277 | goto retry_remap; |
| 278 | } |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 279 | } |
| 280 | err = ntfs_map_runlist_nolock(ni, vcn); |
| 281 | if (!write_locked) { |
| 282 | up_write(&ni->runlist.lock); |
| 283 | down_read(&ni->runlist.lock); |
| 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (likely(!err)) { |
| 286 | is_retry = TRUE; |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 287 | goto retry_remap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | /* |
| 290 | * -EINVAL and -ENOENT coming from a failed mapping attempt are |
| 291 | * equivalent to i/o errors for us as they should not happen in |
| 292 | * our code paths. |
| 293 | */ |
| 294 | if (err == -EINVAL || err == -ENOENT) |
| 295 | err = -EIO; |
| 296 | } else if (!err) |
| 297 | err = -EIO; |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 298 | if (err != -ENOENT) |
| 299 | ntfs_error(ni->vol->sb, "Failed with error code %i.", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | return ERR_PTR(err); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * ntfs_attr_find - find (next) attribute in mft record |
| 305 | * @type: attribute type to find |
| 306 | * @name: attribute name to find (optional, i.e. NULL means don't care) |
| 307 | * @name_len: attribute name length (only needed if @name present) |
| 308 | * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) |
| 309 | * @val: attribute value to find (optional, resident attributes only) |
| 310 | * @val_len: attribute value length |
| 311 | * @ctx: search context with mft record and attribute to search from |
| 312 | * |
| 313 | * You should not need to call this function directly. Use ntfs_attr_lookup() |
| 314 | * instead. |
| 315 | * |
| 316 | * ntfs_attr_find() takes a search context @ctx as parameter and searches the |
| 317 | * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an |
| 318 | * attribute of @type, optionally @name and @val. |
| 319 | * |
| 320 | * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will |
| 321 | * point to the found attribute. |
| 322 | * |
| 323 | * If the attribute is not found, ntfs_attr_find() returns -ENOENT and |
| 324 | * @ctx->attr will point to the attribute before which the attribute being |
| 325 | * searched for would need to be inserted if such an action were to be desired. |
| 326 | * |
| 327 | * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is |
| 328 | * undefined and in particular do not rely on it not changing. |
| 329 | * |
| 330 | * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it |
| 331 | * is FALSE, the search begins after @ctx->attr. |
| 332 | * |
| 333 | * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and |
| 334 | * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record |
| 335 | * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at |
| 336 | * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case |
| 337 | * sensitive. When @name is present, @name_len is the @name length in Unicode |
| 338 | * characters. |
| 339 | * |
| 340 | * If @name is not present (NULL), we assume that the unnamed attribute is |
| 341 | * being searched for. |
| 342 | * |
| 343 | * Finally, the resident attribute value @val is looked for, if present. If |
| 344 | * @val is not present (NULL), @val_len is ignored. |
| 345 | * |
| 346 | * ntfs_attr_find() only searches the specified mft record and it ignores the |
| 347 | * presence of an attribute list attribute (unless it is the one being searched |
| 348 | * for, obviously). If you need to take attribute lists into consideration, |
| 349 | * use ntfs_attr_lookup() instead (see below). This also means that you cannot |
| 350 | * use ntfs_attr_find() to search for extent records of non-resident |
| 351 | * attributes, as extents with lowest_vcn != 0 are usually described by the |
| 352 | * attribute list attribute only. - Note that it is possible that the first |
| 353 | * extent is only in the attribute list while the last extent is in the base |
| 354 | * mft record, so do not rely on being able to find the first extent in the |
| 355 | * base mft record. |
| 356 | * |
| 357 | * Warning: Never use @val when looking for attribute types which can be |
| 358 | * non-resident as this most likely will result in a crash! |
| 359 | */ |
| 360 | static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name, |
| 361 | const u32 name_len, const IGNORE_CASE_BOOL ic, |
| 362 | const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx) |
| 363 | { |
| 364 | ATTR_RECORD *a; |
| 365 | ntfs_volume *vol = ctx->ntfs_ino->vol; |
| 366 | ntfschar *upcase = vol->upcase; |
| 367 | u32 upcase_len = vol->upcase_len; |
| 368 | |
| 369 | /* |
| 370 | * Iterate over attributes in mft record starting at @ctx->attr, or the |
| 371 | * attribute following that, if @ctx->is_first is TRUE. |
| 372 | */ |
| 373 | if (ctx->is_first) { |
| 374 | a = ctx->attr; |
| 375 | ctx->is_first = FALSE; |
| 376 | } else |
| 377 | a = (ATTR_RECORD*)((u8*)ctx->attr + |
| 378 | le32_to_cpu(ctx->attr->length)); |
| 379 | for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) { |
| 380 | if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec + |
| 381 | le32_to_cpu(ctx->mrec->bytes_allocated)) |
| 382 | break; |
| 383 | ctx->attr = a; |
| 384 | if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) || |
| 385 | a->type == AT_END)) |
| 386 | return -ENOENT; |
| 387 | if (unlikely(!a->length)) |
| 388 | break; |
| 389 | if (a->type != type) |
| 390 | continue; |
| 391 | /* |
| 392 | * If @name is present, compare the two names. If @name is |
| 393 | * missing, assume we want an unnamed attribute. |
| 394 | */ |
| 395 | if (!name) { |
| 396 | /* The search failed if the found attribute is named. */ |
| 397 | if (a->name_length) |
| 398 | return -ENOENT; |
| 399 | } else if (!ntfs_are_names_equal(name, name_len, |
| 400 | (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)), |
| 401 | a->name_length, ic, upcase, upcase_len)) { |
| 402 | register int rc; |
| 403 | |
| 404 | rc = ntfs_collate_names(name, name_len, |
| 405 | (ntfschar*)((u8*)a + |
| 406 | le16_to_cpu(a->name_offset)), |
| 407 | a->name_length, 1, IGNORE_CASE, |
| 408 | upcase, upcase_len); |
| 409 | /* |
| 410 | * If @name collates before a->name, there is no |
| 411 | * matching attribute. |
| 412 | */ |
| 413 | if (rc == -1) |
| 414 | return -ENOENT; |
| 415 | /* If the strings are not equal, continue search. */ |
| 416 | if (rc) |
| 417 | continue; |
| 418 | rc = ntfs_collate_names(name, name_len, |
| 419 | (ntfschar*)((u8*)a + |
| 420 | le16_to_cpu(a->name_offset)), |
| 421 | a->name_length, 1, CASE_SENSITIVE, |
| 422 | upcase, upcase_len); |
| 423 | if (rc == -1) |
| 424 | return -ENOENT; |
| 425 | if (rc) |
| 426 | continue; |
| 427 | } |
| 428 | /* |
| 429 | * The names match or @name not present and attribute is |
| 430 | * unnamed. If no @val specified, we have found the attribute |
| 431 | * and are done. |
| 432 | */ |
| 433 | if (!val) |
| 434 | return 0; |
| 435 | /* @val is present; compare values. */ |
| 436 | else { |
| 437 | register int rc; |
| 438 | |
| 439 | rc = memcmp(val, (u8*)a + le16_to_cpu( |
| 440 | a->data.resident.value_offset), |
| 441 | min_t(u32, val_len, le32_to_cpu( |
| 442 | a->data.resident.value_length))); |
| 443 | /* |
| 444 | * If @val collates before the current attribute's |
| 445 | * value, there is no matching attribute. |
| 446 | */ |
| 447 | if (!rc) { |
| 448 | register u32 avl; |
| 449 | |
| 450 | avl = le32_to_cpu( |
| 451 | a->data.resident.value_length); |
| 452 | if (val_len == avl) |
| 453 | return 0; |
| 454 | if (val_len < avl) |
| 455 | return -ENOENT; |
| 456 | } else if (rc < 0) |
| 457 | return -ENOENT; |
| 458 | } |
| 459 | } |
| 460 | ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk."); |
| 461 | NVolSetErrors(vol); |
| 462 | return -EIO; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * load_attribute_list - load an attribute list into memory |
| 467 | * @vol: ntfs volume from which to read |
| 468 | * @runlist: runlist of the attribute list |
| 469 | * @al_start: destination buffer |
| 470 | * @size: size of the destination buffer in bytes |
| 471 | * @initialized_size: initialized size of the attribute list |
| 472 | * |
| 473 | * Walk the runlist @runlist and load all clusters from it copying them into |
| 474 | * the linear buffer @al. The maximum number of bytes copied to @al is @size |
| 475 | * bytes. Note, @size does not need to be a multiple of the cluster size. If |
| 476 | * @initialized_size is less than @size, the region in @al between |
| 477 | * @initialized_size and @size will be zeroed and not read from disk. |
| 478 | * |
| 479 | * Return 0 on success or -errno on error. |
| 480 | */ |
| 481 | int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start, |
| 482 | const s64 size, const s64 initialized_size) |
| 483 | { |
| 484 | LCN lcn; |
| 485 | u8 *al = al_start; |
| 486 | u8 *al_end = al + initialized_size; |
| 487 | runlist_element *rl; |
| 488 | struct buffer_head *bh; |
| 489 | struct super_block *sb; |
| 490 | unsigned long block_size; |
| 491 | unsigned long block, max_block; |
| 492 | int err = 0; |
| 493 | unsigned char block_size_bits; |
| 494 | |
| 495 | ntfs_debug("Entering."); |
| 496 | if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 || |
| 497 | initialized_size > size) |
| 498 | return -EINVAL; |
| 499 | if (!initialized_size) { |
| 500 | memset(al, 0, size); |
| 501 | return 0; |
| 502 | } |
| 503 | sb = vol->sb; |
| 504 | block_size = sb->s_blocksize; |
| 505 | block_size_bits = sb->s_blocksize_bits; |
| 506 | down_read(&runlist->lock); |
| 507 | rl = runlist->rl; |
| 508 | /* Read all clusters specified by the runlist one run at a time. */ |
| 509 | while (rl->length) { |
| 510 | lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn); |
| 511 | ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.", |
| 512 | (unsigned long long)rl->vcn, |
| 513 | (unsigned long long)lcn); |
| 514 | /* The attribute list cannot be sparse. */ |
| 515 | if (lcn < 0) { |
| 516 | ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot " |
| 517 | "read attribute list."); |
| 518 | goto err_out; |
| 519 | } |
| 520 | block = lcn << vol->cluster_size_bits >> block_size_bits; |
| 521 | /* Read the run from device in chunks of block_size bytes. */ |
| 522 | max_block = block + (rl->length << vol->cluster_size_bits >> |
| 523 | block_size_bits); |
| 524 | ntfs_debug("max_block = 0x%lx.", max_block); |
| 525 | do { |
| 526 | ntfs_debug("Reading block = 0x%lx.", block); |
| 527 | bh = sb_bread(sb, block); |
| 528 | if (!bh) { |
| 529 | ntfs_error(sb, "sb_bread() failed. Cannot " |
| 530 | "read attribute list."); |
| 531 | goto err_out; |
| 532 | } |
| 533 | if (al + block_size >= al_end) |
| 534 | goto do_final; |
| 535 | memcpy(al, bh->b_data, block_size); |
| 536 | brelse(bh); |
| 537 | al += block_size; |
| 538 | } while (++block < max_block); |
| 539 | rl++; |
| 540 | } |
| 541 | if (initialized_size < size) { |
| 542 | initialize: |
| 543 | memset(al_start + initialized_size, 0, size - initialized_size); |
| 544 | } |
| 545 | done: |
| 546 | up_read(&runlist->lock); |
| 547 | return err; |
| 548 | do_final: |
| 549 | if (al < al_end) { |
| 550 | /* |
| 551 | * Partial block. |
| 552 | * |
| 553 | * Note: The attribute list can be smaller than its allocation |
| 554 | * by multiple clusters. This has been encountered by at least |
| 555 | * two people running Windows XP, thus we cannot do any |
| 556 | * truncation sanity checking here. (AIA) |
| 557 | */ |
| 558 | memcpy(al, bh->b_data, al_end - al); |
| 559 | brelse(bh); |
| 560 | if (initialized_size < size) |
| 561 | goto initialize; |
| 562 | goto done; |
| 563 | } |
| 564 | brelse(bh); |
| 565 | /* Real overflow! */ |
| 566 | ntfs_error(sb, "Attribute list buffer overflow. Read attribute list " |
| 567 | "is truncated."); |
| 568 | err_out: |
| 569 | err = -EIO; |
| 570 | goto done; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * ntfs_external_attr_find - find an attribute in the attribute list of an inode |
| 575 | * @type: attribute type to find |
| 576 | * @name: attribute name to find (optional, i.e. NULL means don't care) |
| 577 | * @name_len: attribute name length (only needed if @name present) |
| 578 | * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) |
| 579 | * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) |
| 580 | * @val: attribute value to find (optional, resident attributes only) |
| 581 | * @val_len: attribute value length |
| 582 | * @ctx: search context with mft record and attribute to search from |
| 583 | * |
| 584 | * You should not need to call this function directly. Use ntfs_attr_lookup() |
| 585 | * instead. |
| 586 | * |
| 587 | * Find an attribute by searching the attribute list for the corresponding |
| 588 | * attribute list entry. Having found the entry, map the mft record if the |
| 589 | * attribute is in a different mft record/inode, ntfs_attr_find() the attribute |
| 590 | * in there and return it. |
| 591 | * |
| 592 | * On first search @ctx->ntfs_ino must be the base mft record and @ctx must |
| 593 | * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent |
| 594 | * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is |
| 595 | * then the base inode). |
| 596 | * |
| 597 | * After finishing with the attribute/mft record you need to call |
| 598 | * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any |
| 599 | * mapped inodes, etc). |
| 600 | * |
| 601 | * If the attribute is found, ntfs_external_attr_find() returns 0 and |
| 602 | * @ctx->attr will point to the found attribute. @ctx->mrec will point to the |
| 603 | * mft record in which @ctx->attr is located and @ctx->al_entry will point to |
| 604 | * the attribute list entry for the attribute. |
| 605 | * |
| 606 | * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and |
| 607 | * @ctx->attr will point to the attribute in the base mft record before which |
| 608 | * the attribute being searched for would need to be inserted if such an action |
| 609 | * were to be desired. @ctx->mrec will point to the mft record in which |
| 610 | * @ctx->attr is located and @ctx->al_entry will point to the attribute list |
| 611 | * entry of the attribute before which the attribute being searched for would |
| 612 | * need to be inserted if such an action were to be desired. |
| 613 | * |
| 614 | * Thus to insert the not found attribute, one wants to add the attribute to |
| 615 | * @ctx->mrec (the base mft record) and if there is not enough space, the |
| 616 | * attribute should be placed in a newly allocated extent mft record. The |
| 617 | * attribute list entry for the inserted attribute should be inserted in the |
| 618 | * attribute list attribute at @ctx->al_entry. |
| 619 | * |
| 620 | * On actual error, ntfs_external_attr_find() returns -EIO. In this case |
| 621 | * @ctx->attr is undefined and in particular do not rely on it not changing. |
| 622 | */ |
| 623 | static int ntfs_external_attr_find(const ATTR_TYPE type, |
| 624 | const ntfschar *name, const u32 name_len, |
| 625 | const IGNORE_CASE_BOOL ic, const VCN lowest_vcn, |
| 626 | const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx) |
| 627 | { |
| 628 | ntfs_inode *base_ni, *ni; |
| 629 | ntfs_volume *vol; |
| 630 | ATTR_LIST_ENTRY *al_entry, *next_al_entry; |
| 631 | u8 *al_start, *al_end; |
| 632 | ATTR_RECORD *a; |
| 633 | ntfschar *al_name; |
| 634 | u32 al_name_len; |
| 635 | int err = 0; |
| 636 | static const char *es = " Unmount and run chkdsk."; |
| 637 | |
| 638 | ni = ctx->ntfs_ino; |
| 639 | base_ni = ctx->base_ntfs_ino; |
| 640 | ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type); |
| 641 | if (!base_ni) { |
| 642 | /* First call happens with the base mft record. */ |
| 643 | base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino; |
| 644 | ctx->base_mrec = ctx->mrec; |
| 645 | } |
| 646 | if (ni == base_ni) |
| 647 | ctx->base_attr = ctx->attr; |
| 648 | if (type == AT_END) |
| 649 | goto not_found; |
| 650 | vol = base_ni->vol; |
| 651 | al_start = base_ni->attr_list; |
| 652 | al_end = al_start + base_ni->attr_list_size; |
| 653 | if (!ctx->al_entry) |
| 654 | ctx->al_entry = (ATTR_LIST_ENTRY*)al_start; |
| 655 | /* |
| 656 | * Iterate over entries in attribute list starting at @ctx->al_entry, |
| 657 | * or the entry following that, if @ctx->is_first is TRUE. |
| 658 | */ |
| 659 | if (ctx->is_first) { |
| 660 | al_entry = ctx->al_entry; |
| 661 | ctx->is_first = FALSE; |
| 662 | } else |
| 663 | al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry + |
| 664 | le16_to_cpu(ctx->al_entry->length)); |
| 665 | for (;; al_entry = next_al_entry) { |
| 666 | /* Out of bounds check. */ |
| 667 | if ((u8*)al_entry < base_ni->attr_list || |
| 668 | (u8*)al_entry > al_end) |
| 669 | break; /* Inode is corrupt. */ |
| 670 | ctx->al_entry = al_entry; |
| 671 | /* Catch the end of the attribute list. */ |
| 672 | if ((u8*)al_entry == al_end) |
| 673 | goto not_found; |
| 674 | if (!al_entry->length) |
| 675 | break; |
| 676 | if ((u8*)al_entry + 6 > al_end || (u8*)al_entry + |
| 677 | le16_to_cpu(al_entry->length) > al_end) |
| 678 | break; |
| 679 | next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry + |
| 680 | le16_to_cpu(al_entry->length)); |
| 681 | if (le32_to_cpu(al_entry->type) > le32_to_cpu(type)) |
| 682 | goto not_found; |
| 683 | if (type != al_entry->type) |
| 684 | continue; |
| 685 | /* |
| 686 | * If @name is present, compare the two names. If @name is |
| 687 | * missing, assume we want an unnamed attribute. |
| 688 | */ |
| 689 | al_name_len = al_entry->name_length; |
| 690 | al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset); |
| 691 | if (!name) { |
| 692 | if (al_name_len) |
| 693 | goto not_found; |
| 694 | } else if (!ntfs_are_names_equal(al_name, al_name_len, name, |
| 695 | name_len, ic, vol->upcase, vol->upcase_len)) { |
| 696 | register int rc; |
| 697 | |
| 698 | rc = ntfs_collate_names(name, name_len, al_name, |
| 699 | al_name_len, 1, IGNORE_CASE, |
| 700 | vol->upcase, vol->upcase_len); |
| 701 | /* |
| 702 | * If @name collates before al_name, there is no |
| 703 | * matching attribute. |
| 704 | */ |
| 705 | if (rc == -1) |
| 706 | goto not_found; |
| 707 | /* If the strings are not equal, continue search. */ |
| 708 | if (rc) |
| 709 | continue; |
| 710 | /* |
| 711 | * FIXME: Reverse engineering showed 0, IGNORE_CASE but |
| 712 | * that is inconsistent with ntfs_attr_find(). The |
| 713 | * subsequent rc checks were also different. Perhaps I |
| 714 | * made a mistake in one of the two. Need to recheck |
| 715 | * which is correct or at least see what is going on... |
| 716 | * (AIA) |
| 717 | */ |
| 718 | rc = ntfs_collate_names(name, name_len, al_name, |
| 719 | al_name_len, 1, CASE_SENSITIVE, |
| 720 | vol->upcase, vol->upcase_len); |
| 721 | if (rc == -1) |
| 722 | goto not_found; |
| 723 | if (rc) |
| 724 | continue; |
| 725 | } |
| 726 | /* |
| 727 | * The names match or @name not present and attribute is |
| 728 | * unnamed. Now check @lowest_vcn. Continue search if the |
| 729 | * next attribute list entry still fits @lowest_vcn. Otherwise |
| 730 | * we have reached the right one or the search has failed. |
| 731 | */ |
| 732 | if (lowest_vcn && (u8*)next_al_entry >= al_start && |
| 733 | (u8*)next_al_entry + 6 < al_end && |
| 734 | (u8*)next_al_entry + le16_to_cpu( |
| 735 | next_al_entry->length) <= al_end && |
| 736 | sle64_to_cpu(next_al_entry->lowest_vcn) <= |
| 737 | lowest_vcn && |
| 738 | next_al_entry->type == al_entry->type && |
| 739 | next_al_entry->name_length == al_name_len && |
| 740 | ntfs_are_names_equal((ntfschar*)((u8*) |
| 741 | next_al_entry + |
| 742 | next_al_entry->name_offset), |
| 743 | next_al_entry->name_length, |
| 744 | al_name, al_name_len, CASE_SENSITIVE, |
| 745 | vol->upcase, vol->upcase_len)) |
| 746 | continue; |
| 747 | if (MREF_LE(al_entry->mft_reference) == ni->mft_no) { |
| 748 | if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) { |
| 749 | ntfs_error(vol->sb, "Found stale mft " |
| 750 | "reference in attribute list " |
| 751 | "of base inode 0x%lx.%s", |
| 752 | base_ni->mft_no, es); |
| 753 | err = -EIO; |
| 754 | break; |
| 755 | } |
| 756 | } else { /* Mft references do not match. */ |
| 757 | /* If there is a mapped record unmap it first. */ |
| 758 | if (ni != base_ni) |
| 759 | unmap_extent_mft_record(ni); |
| 760 | /* Do we want the base record back? */ |
| 761 | if (MREF_LE(al_entry->mft_reference) == |
| 762 | base_ni->mft_no) { |
| 763 | ni = ctx->ntfs_ino = base_ni; |
| 764 | ctx->mrec = ctx->base_mrec; |
| 765 | } else { |
| 766 | /* We want an extent record. */ |
| 767 | ctx->mrec = map_extent_mft_record(base_ni, |
| 768 | le64_to_cpu( |
| 769 | al_entry->mft_reference), &ni); |
| 770 | if (IS_ERR(ctx->mrec)) { |
| 771 | ntfs_error(vol->sb, "Failed to map " |
| 772 | "extent mft record " |
| 773 | "0x%lx of base inode " |
| 774 | "0x%lx.%s", |
| 775 | MREF_LE(al_entry-> |
| 776 | mft_reference), |
| 777 | base_ni->mft_no, es); |
| 778 | err = PTR_ERR(ctx->mrec); |
| 779 | if (err == -ENOENT) |
| 780 | err = -EIO; |
| 781 | /* Cause @ctx to be sanitized below. */ |
| 782 | ni = NULL; |
| 783 | break; |
| 784 | } |
| 785 | ctx->ntfs_ino = ni; |
| 786 | } |
| 787 | ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + |
| 788 | le16_to_cpu(ctx->mrec->attrs_offset)); |
| 789 | } |
| 790 | /* |
| 791 | * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the |
| 792 | * mft record containing the attribute represented by the |
| 793 | * current al_entry. |
| 794 | */ |
| 795 | /* |
| 796 | * We could call into ntfs_attr_find() to find the right |
| 797 | * attribute in this mft record but this would be less |
| 798 | * efficient and not quite accurate as ntfs_attr_find() ignores |
| 799 | * the attribute instance numbers for example which become |
| 800 | * important when one plays with attribute lists. Also, |
| 801 | * because a proper match has been found in the attribute list |
| 802 | * entry above, the comparison can now be optimized. So it is |
| 803 | * worth re-implementing a simplified ntfs_attr_find() here. |
| 804 | */ |
| 805 | a = ctx->attr; |
| 806 | /* |
| 807 | * Use a manual loop so we can still use break and continue |
| 808 | * with the same meanings as above. |
| 809 | */ |
| 810 | do_next_attr_loop: |
| 811 | if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec + |
| 812 | le32_to_cpu(ctx->mrec->bytes_allocated)) |
| 813 | break; |
| 814 | if (a->type == AT_END) |
| 815 | continue; |
| 816 | if (!a->length) |
| 817 | break; |
| 818 | if (al_entry->instance != a->instance) |
| 819 | goto do_next_attr; |
| 820 | /* |
| 821 | * If the type and/or the name are mismatched between the |
| 822 | * attribute list entry and the attribute record, there is |
| 823 | * corruption so we break and return error EIO. |
| 824 | */ |
| 825 | if (al_entry->type != a->type) |
| 826 | break; |
| 827 | if (!ntfs_are_names_equal((ntfschar*)((u8*)a + |
| 828 | le16_to_cpu(a->name_offset)), a->name_length, |
| 829 | al_name, al_name_len, CASE_SENSITIVE, |
| 830 | vol->upcase, vol->upcase_len)) |
| 831 | break; |
| 832 | ctx->attr = a; |
| 833 | /* |
| 834 | * If no @val specified or @val specified and it matches, we |
| 835 | * have found it! |
| 836 | */ |
| 837 | if (!val || (!a->non_resident && le32_to_cpu( |
| 838 | a->data.resident.value_length) == val_len && |
| 839 | !memcmp((u8*)a + |
| 840 | le16_to_cpu(a->data.resident.value_offset), |
| 841 | val, val_len))) { |
| 842 | ntfs_debug("Done, found."); |
| 843 | return 0; |
| 844 | } |
| 845 | do_next_attr: |
| 846 | /* Proceed to the next attribute in the current mft record. */ |
| 847 | a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length)); |
| 848 | goto do_next_attr_loop; |
| 849 | } |
| 850 | if (!err) { |
| 851 | ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt " |
| 852 | "attribute list attribute.%s", base_ni->mft_no, |
| 853 | es); |
| 854 | err = -EIO; |
| 855 | } |
| 856 | if (ni != base_ni) { |
| 857 | if (ni) |
| 858 | unmap_extent_mft_record(ni); |
| 859 | ctx->ntfs_ino = base_ni; |
| 860 | ctx->mrec = ctx->base_mrec; |
| 861 | ctx->attr = ctx->base_attr; |
| 862 | } |
| 863 | if (err != -ENOMEM) |
| 864 | NVolSetErrors(vol); |
| 865 | return err; |
| 866 | not_found: |
| 867 | /* |
| 868 | * If we were looking for AT_END, we reset the search context @ctx and |
| 869 | * use ntfs_attr_find() to seek to the end of the base mft record. |
| 870 | */ |
| 871 | if (type == AT_END) { |
| 872 | ntfs_attr_reinit_search_ctx(ctx); |
| 873 | return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len, |
| 874 | ctx); |
| 875 | } |
| 876 | /* |
| 877 | * The attribute was not found. Before we return, we want to ensure |
| 878 | * @ctx->mrec and @ctx->attr indicate the position at which the |
| 879 | * attribute should be inserted in the base mft record. Since we also |
| 880 | * want to preserve @ctx->al_entry we cannot reinitialize the search |
| 881 | * context using ntfs_attr_reinit_search_ctx() as this would set |
| 882 | * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see |
| 883 | * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve |
| 884 | * @ctx->al_entry as the remaining fields (base_*) are identical to |
| 885 | * their non base_ counterparts and we cannot set @ctx->base_attr |
| 886 | * correctly yet as we do not know what @ctx->attr will be set to by |
| 887 | * the call to ntfs_attr_find() below. |
| 888 | */ |
| 889 | if (ni != base_ni) |
| 890 | unmap_extent_mft_record(ni); |
| 891 | ctx->mrec = ctx->base_mrec; |
| 892 | ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + |
| 893 | le16_to_cpu(ctx->mrec->attrs_offset)); |
| 894 | ctx->is_first = TRUE; |
| 895 | ctx->ntfs_ino = base_ni; |
| 896 | ctx->base_ntfs_ino = NULL; |
| 897 | ctx->base_mrec = NULL; |
| 898 | ctx->base_attr = NULL; |
| 899 | /* |
| 900 | * In case there are multiple matches in the base mft record, need to |
| 901 | * keep enumerating until we get an attribute not found response (or |
| 902 | * another error), otherwise we would keep returning the same attribute |
| 903 | * over and over again and all programs using us for enumeration would |
| 904 | * lock up in a tight loop. |
| 905 | */ |
| 906 | do { |
| 907 | err = ntfs_attr_find(type, name, name_len, ic, val, val_len, |
| 908 | ctx); |
| 909 | } while (!err); |
| 910 | ntfs_debug("Done, not found."); |
| 911 | return err; |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * ntfs_attr_lookup - find an attribute in an ntfs inode |
| 916 | * @type: attribute type to find |
| 917 | * @name: attribute name to find (optional, i.e. NULL means don't care) |
| 918 | * @name_len: attribute name length (only needed if @name present) |
| 919 | * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) |
| 920 | * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) |
| 921 | * @val: attribute value to find (optional, resident attributes only) |
| 922 | * @val_len: attribute value length |
| 923 | * @ctx: search context with mft record and attribute to search from |
| 924 | * |
| 925 | * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must |
| 926 | * be the base mft record and @ctx must have been obtained from a call to |
| 927 | * ntfs_attr_get_search_ctx(). |
| 928 | * |
| 929 | * This function transparently handles attribute lists and @ctx is used to |
| 930 | * continue searches where they were left off at. |
| 931 | * |
| 932 | * After finishing with the attribute/mft record you need to call |
| 933 | * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any |
| 934 | * mapped inodes, etc). |
| 935 | * |
| 936 | * Return 0 if the search was successful and -errno if not. |
| 937 | * |
| 938 | * When 0, @ctx->attr is the found attribute and it is in mft record |
| 939 | * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is |
| 940 | * the attribute list entry of the found attribute. |
| 941 | * |
| 942 | * When -ENOENT, @ctx->attr is the attribute which collates just after the |
| 943 | * attribute being searched for, i.e. if one wants to add the attribute to the |
| 944 | * mft record this is the correct place to insert it into. If an attribute |
| 945 | * list attribute is present, @ctx->al_entry is the attribute list entry which |
| 946 | * collates just after the attribute list entry of the attribute being searched |
| 947 | * for, i.e. if one wants to add the attribute to the mft record this is the |
| 948 | * correct place to insert its attribute list entry into. |
| 949 | * |
| 950 | * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is |
| 951 | * then undefined and in particular you should not rely on it not changing. |
| 952 | */ |
| 953 | int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name, |
| 954 | const u32 name_len, const IGNORE_CASE_BOOL ic, |
| 955 | const VCN lowest_vcn, const u8 *val, const u32 val_len, |
| 956 | ntfs_attr_search_ctx *ctx) |
| 957 | { |
| 958 | ntfs_inode *base_ni; |
| 959 | |
| 960 | ntfs_debug("Entering."); |
| 961 | if (ctx->base_ntfs_ino) |
| 962 | base_ni = ctx->base_ntfs_ino; |
| 963 | else |
| 964 | base_ni = ctx->ntfs_ino; |
| 965 | /* Sanity check, just for debugging really. */ |
| 966 | BUG_ON(!base_ni); |
| 967 | if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST) |
| 968 | return ntfs_attr_find(type, name, name_len, ic, val, val_len, |
| 969 | ctx); |
| 970 | return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn, |
| 971 | val, val_len, ctx); |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * ntfs_attr_init_search_ctx - initialize an attribute search context |
| 976 | * @ctx: attribute search context to initialize |
| 977 | * @ni: ntfs inode with which to initialize the search context |
| 978 | * @mrec: mft record with which to initialize the search context |
| 979 | * |
| 980 | * Initialize the attribute search context @ctx with @ni and @mrec. |
| 981 | */ |
| 982 | static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx, |
| 983 | ntfs_inode *ni, MFT_RECORD *mrec) |
| 984 | { |
Anton Altaparmakov | 442d207 | 2005-05-27 16:42:56 +0100 | [diff] [blame] | 985 | *ctx = (ntfs_attr_search_ctx) { |
| 986 | .mrec = mrec, |
| 987 | /* Sanity checks are performed elsewhere. */ |
| 988 | .attr = (ATTR_RECORD*)((u8*)mrec + |
| 989 | le16_to_cpu(mrec->attrs_offset)), |
| 990 | .is_first = TRUE, |
| 991 | .ntfs_ino = ni, |
| 992 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /** |
| 996 | * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context |
| 997 | * @ctx: attribute search context to reinitialize |
| 998 | * |
| 999 | * Reinitialize the attribute search context @ctx, unmapping an associated |
| 1000 | * extent mft record if present, and initialize the search context again. |
| 1001 | * |
| 1002 | * This is used when a search for a new attribute is being started to reset |
| 1003 | * the search context to the beginning. |
| 1004 | */ |
| 1005 | void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx) |
| 1006 | { |
| 1007 | if (likely(!ctx->base_ntfs_ino)) { |
| 1008 | /* No attribute list. */ |
| 1009 | ctx->is_first = TRUE; |
| 1010 | /* Sanity checks are performed elsewhere. */ |
| 1011 | ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + |
| 1012 | le16_to_cpu(ctx->mrec->attrs_offset)); |
| 1013 | /* |
| 1014 | * This needs resetting due to ntfs_external_attr_find() which |
| 1015 | * can leave it set despite having zeroed ctx->base_ntfs_ino. |
| 1016 | */ |
| 1017 | ctx->al_entry = NULL; |
| 1018 | return; |
| 1019 | } /* Attribute list. */ |
| 1020 | if (ctx->ntfs_ino != ctx->base_ntfs_ino) |
| 1021 | unmap_extent_mft_record(ctx->ntfs_ino); |
| 1022 | ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context |
| 1028 | * @ni: ntfs inode with which to initialize the search context |
| 1029 | * @mrec: mft record with which to initialize the search context |
| 1030 | * |
| 1031 | * Allocate a new attribute search context, initialize it with @ni and @mrec, |
| 1032 | * and return it. Return NULL if allocation failed. |
| 1033 | */ |
| 1034 | ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec) |
| 1035 | { |
| 1036 | ntfs_attr_search_ctx *ctx; |
| 1037 | |
| 1038 | ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS); |
| 1039 | if (ctx) |
| 1040 | ntfs_attr_init_search_ctx(ctx, ni, mrec); |
| 1041 | return ctx; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * ntfs_attr_put_search_ctx - release an attribute search context |
| 1046 | * @ctx: attribute search context to free |
| 1047 | * |
| 1048 | * Release the attribute search context @ctx, unmapping an associated extent |
| 1049 | * mft record if present. |
| 1050 | */ |
| 1051 | void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx) |
| 1052 | { |
| 1053 | if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino) |
| 1054 | unmap_extent_mft_record(ctx->ntfs_ino); |
| 1055 | kmem_cache_free(ntfs_attr_ctx_cache, ctx); |
| 1056 | return; |
| 1057 | } |
| 1058 | |
Anton Altaparmakov | 53d59aa | 2005-03-17 10:51:33 +0000 | [diff] [blame] | 1059 | #ifdef NTFS_RW |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | /** |
| 1062 | * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file |
| 1063 | * @vol: ntfs volume to which the attribute belongs |
| 1064 | * @type: attribute type which to find |
| 1065 | * |
| 1066 | * Search for the attribute definition record corresponding to the attribute |
| 1067 | * @type in the $AttrDef system file. |
| 1068 | * |
| 1069 | * Return the attribute type definition record if found and NULL if not found. |
| 1070 | */ |
| 1071 | static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol, |
| 1072 | const ATTR_TYPE type) |
| 1073 | { |
| 1074 | ATTR_DEF *ad; |
| 1075 | |
| 1076 | BUG_ON(!vol->attrdef); |
| 1077 | BUG_ON(!type); |
| 1078 | for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef < |
| 1079 | vol->attrdef_size && ad->type; ++ad) { |
| 1080 | /* We have not found it yet, carry on searching. */ |
| 1081 | if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type))) |
| 1082 | continue; |
| 1083 | /* We found the attribute; return it. */ |
| 1084 | if (likely(ad->type == type)) |
| 1085 | return ad; |
| 1086 | /* We have gone too far already. No point in continuing. */ |
| 1087 | break; |
| 1088 | } |
| 1089 | /* Attribute not found. */ |
| 1090 | ntfs_debug("Attribute type 0x%x not found in $AttrDef.", |
| 1091 | le32_to_cpu(type)); |
| 1092 | return NULL; |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * ntfs_attr_size_bounds_check - check a size of an attribute type for validity |
| 1097 | * @vol: ntfs volume to which the attribute belongs |
| 1098 | * @type: attribute type which to check |
| 1099 | * @size: size which to check |
| 1100 | * |
| 1101 | * Check whether the @size in bytes is valid for an attribute of @type on the |
| 1102 | * ntfs volume @vol. This information is obtained from $AttrDef system file. |
| 1103 | * |
| 1104 | * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not |
| 1105 | * listed in $AttrDef. |
| 1106 | */ |
| 1107 | int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type, |
| 1108 | const s64 size) |
| 1109 | { |
| 1110 | ATTR_DEF *ad; |
| 1111 | |
| 1112 | BUG_ON(size < 0); |
| 1113 | /* |
| 1114 | * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not |
| 1115 | * listed in $AttrDef. |
| 1116 | */ |
| 1117 | if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024)) |
| 1118 | return -ERANGE; |
| 1119 | /* Get the $AttrDef entry for the attribute @type. */ |
| 1120 | ad = ntfs_attr_find_in_attrdef(vol, type); |
| 1121 | if (unlikely(!ad)) |
| 1122 | return -ENOENT; |
| 1123 | /* Do the bounds check. */ |
| 1124 | if (((sle64_to_cpu(ad->min_size) > 0) && |
| 1125 | size < sle64_to_cpu(ad->min_size)) || |
| 1126 | ((sle64_to_cpu(ad->max_size) > 0) && size > |
| 1127 | sle64_to_cpu(ad->max_size))) |
| 1128 | return -ERANGE; |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident |
| 1134 | * @vol: ntfs volume to which the attribute belongs |
| 1135 | * @type: attribute type which to check |
| 1136 | * |
| 1137 | * Check whether the attribute of @type on the ntfs volume @vol is allowed to |
| 1138 | * be non-resident. This information is obtained from $AttrDef system file. |
| 1139 | * |
Anton Altaparmakov | bb3cf33 | 2005-04-06 13:34:31 +0100 | [diff] [blame] | 1140 | * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | * -ENOENT if the attribute is not listed in $AttrDef. |
| 1142 | */ |
| 1143 | int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type) |
| 1144 | { |
| 1145 | ATTR_DEF *ad; |
| 1146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | /* Find the attribute definition record in $AttrDef. */ |
| 1148 | ad = ntfs_attr_find_in_attrdef(vol, type); |
| 1149 | if (unlikely(!ad)) |
| 1150 | return -ENOENT; |
| 1151 | /* Check the flags and return the result. */ |
Anton Altaparmakov | bb3cf33 | 2005-04-06 13:34:31 +0100 | [diff] [blame] | 1152 | if (ad->flags & ATTR_DEF_RESIDENT) |
| 1153 | return -EPERM; |
| 1154 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * ntfs_attr_can_be_resident - check if an attribute can be resident |
| 1159 | * @vol: ntfs volume to which the attribute belongs |
| 1160 | * @type: attribute type which to check |
| 1161 | * |
| 1162 | * Check whether the attribute of @type on the ntfs volume @vol is allowed to |
| 1163 | * be resident. This information is derived from our ntfs knowledge and may |
| 1164 | * not be completely accurate, especially when user defined attributes are |
| 1165 | * present. Basically we allow everything to be resident except for index |
| 1166 | * allocation and $EA attributes. |
| 1167 | * |
| 1168 | * Return 0 if the attribute is allowed to be non-resident and -EPERM if not. |
| 1169 | * |
| 1170 | * Warning: In the system file $MFT the attribute $Bitmap must be non-resident |
| 1171 | * otherwise windows will not boot (blue screen of death)! We cannot |
| 1172 | * check for this here as we do not know which inode's $Bitmap is |
| 1173 | * being asked about so the caller needs to special case this. |
| 1174 | */ |
| 1175 | int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type) |
| 1176 | { |
Anton Altaparmakov | bb3cf33 | 2005-04-06 13:34:31 +0100 | [diff] [blame] | 1177 | if (type == AT_INDEX_ALLOCATION || type == AT_EA) |
| 1178 | return -EPERM; |
| 1179 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * ntfs_attr_record_resize - resize an attribute record |
| 1184 | * @m: mft record containing attribute record |
| 1185 | * @a: attribute record to resize |
| 1186 | * @new_size: new size in bytes to which to resize the attribute record @a |
| 1187 | * |
| 1188 | * Resize the attribute record @a, i.e. the resident part of the attribute, in |
| 1189 | * the mft record @m to @new_size bytes. |
| 1190 | * |
| 1191 | * Return 0 on success and -errno on error. The following error codes are |
| 1192 | * defined: |
| 1193 | * -ENOSPC - Not enough space in the mft record @m to perform the resize. |
| 1194 | * |
| 1195 | * Note: On error, no modifications have been performed whatsoever. |
| 1196 | * |
| 1197 | * Warning: If you make a record smaller without having copied all the data you |
| 1198 | * are interested in the data may be overwritten. |
| 1199 | */ |
| 1200 | int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size) |
| 1201 | { |
| 1202 | ntfs_debug("Entering for new_size %u.", new_size); |
| 1203 | /* Align to 8 bytes if it is not already done. */ |
| 1204 | if (new_size & 7) |
| 1205 | new_size = (new_size + 7) & ~7; |
| 1206 | /* If the actual attribute length has changed, move things around. */ |
| 1207 | if (new_size != le32_to_cpu(a->length)) { |
| 1208 | u32 new_muse = le32_to_cpu(m->bytes_in_use) - |
| 1209 | le32_to_cpu(a->length) + new_size; |
| 1210 | /* Not enough space in this mft record. */ |
| 1211 | if (new_muse > le32_to_cpu(m->bytes_allocated)) |
| 1212 | return -ENOSPC; |
| 1213 | /* Move attributes following @a to their new location. */ |
| 1214 | memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length), |
| 1215 | le32_to_cpu(m->bytes_in_use) - ((u8*)a - |
| 1216 | (u8*)m) - le32_to_cpu(a->length)); |
| 1217 | /* Adjust @m to reflect the change in used space. */ |
| 1218 | m->bytes_in_use = cpu_to_le32(new_muse); |
| 1219 | /* Adjust @a to reflect the new size. */ |
| 1220 | if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length)) |
| 1221 | a->length = cpu_to_le32(new_size); |
| 1222 | } |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | /** |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1227 | * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute |
| 1228 | * @ni: ntfs inode describing the attribute to convert |
| 1229 | * |
| 1230 | * Convert the resident ntfs attribute described by the ntfs inode @ni to a |
| 1231 | * non-resident one. |
| 1232 | * |
| 1233 | * Return 0 on success and -errno on error. The following error return codes |
| 1234 | * are defined: |
| 1235 | * -EPERM - The attribute is not allowed to be non-resident. |
| 1236 | * -ENOMEM - Not enough memory. |
| 1237 | * -ENOSPC - Not enough disk space. |
| 1238 | * -EINVAL - Attribute not defined on the volume. |
| 1239 | * -EIO - I/o error or other error. |
Anton Altaparmakov | 53d59aa | 2005-03-17 10:51:33 +0000 | [diff] [blame] | 1240 | * Note that -ENOSPC is also returned in the case that there is not enough |
| 1241 | * space in the mft record to do the conversion. This can happen when the mft |
| 1242 | * record is already very full. The caller is responsible for trying to make |
| 1243 | * space in the mft record and trying again. FIXME: Do we need a separate |
| 1244 | * error return code for this kind of -ENOSPC or is it always worth trying |
| 1245 | * again in case the attribute may then fit in a resident state so no need to |
| 1246 | * make it non-resident at all? Ho-hum... (AIA) |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1247 | * |
| 1248 | * NOTE to self: No changes in the attribute list are required to move from |
| 1249 | * a resident to a non-resident attribute. |
| 1250 | * |
| 1251 | * Locking: - The caller must hold i_sem on the inode. |
| 1252 | */ |
| 1253 | int ntfs_attr_make_non_resident(ntfs_inode *ni) |
| 1254 | { |
| 1255 | s64 new_size; |
| 1256 | struct inode *vi = VFS_I(ni); |
| 1257 | ntfs_volume *vol = ni->vol; |
| 1258 | ntfs_inode *base_ni; |
| 1259 | MFT_RECORD *m; |
| 1260 | ATTR_RECORD *a; |
| 1261 | ntfs_attr_search_ctx *ctx; |
| 1262 | struct page *page; |
| 1263 | runlist_element *rl; |
| 1264 | u8 *kaddr; |
| 1265 | unsigned long flags; |
| 1266 | int mp_size, mp_ofs, name_ofs, arec_size, err, err2; |
| 1267 | u32 attr_size; |
| 1268 | u8 old_res_attr_flags; |
| 1269 | |
| 1270 | /* Check that the attribute is allowed to be non-resident. */ |
| 1271 | err = ntfs_attr_can_be_non_resident(vol, ni->type); |
| 1272 | if (unlikely(err)) { |
| 1273 | if (err == -EPERM) |
| 1274 | ntfs_debug("Attribute is not allowed to be " |
| 1275 | "non-resident."); |
| 1276 | else |
| 1277 | ntfs_debug("Attribute not defined on the NTFS " |
| 1278 | "volume!"); |
| 1279 | return err; |
| 1280 | } |
| 1281 | /* |
| 1282 | * The size needs to be aligned to a cluster boundary for allocation |
| 1283 | * purposes. |
| 1284 | */ |
| 1285 | new_size = (i_size_read(vi) + vol->cluster_size - 1) & |
| 1286 | ~(vol->cluster_size - 1); |
| 1287 | if (new_size > 0) { |
Anton Altaparmakov | 1d58b27 | 2005-06-25 17:04:55 +0100 | [diff] [blame] | 1288 | runlist_element *rl2; |
| 1289 | |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1290 | /* |
| 1291 | * Will need the page later and since the page lock nests |
| 1292 | * outside all ntfs locks, we need to get the page now. |
| 1293 | */ |
| 1294 | page = find_or_create_page(vi->i_mapping, 0, |
| 1295 | mapping_gfp_mask(vi->i_mapping)); |
| 1296 | if (unlikely(!page)) |
| 1297 | return -ENOMEM; |
| 1298 | /* Start by allocating clusters to hold the attribute value. */ |
| 1299 | rl = ntfs_cluster_alloc(vol, 0, new_size >> |
| 1300 | vol->cluster_size_bits, -1, DATA_ZONE); |
| 1301 | if (IS_ERR(rl)) { |
| 1302 | err = PTR_ERR(rl); |
| 1303 | ntfs_debug("Failed to allocate cluster%s, error code " |
| 1304 | "%i.\n", (new_size >> |
| 1305 | vol->cluster_size_bits) > 1 ? "s" : "", |
| 1306 | err); |
| 1307 | goto page_err_out; |
| 1308 | } |
Anton Altaparmakov | 1d58b27 | 2005-06-25 17:04:55 +0100 | [diff] [blame] | 1309 | /* Change the runlist terminator to LCN_ENOENT. */ |
| 1310 | rl2 = rl; |
| 1311 | while (rl2->length) |
| 1312 | rl2++; |
| 1313 | BUG_ON(rl2->lcn != LCN_RL_NOT_MAPPED); |
| 1314 | rl2->lcn = LCN_ENOENT; |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1315 | } else { |
| 1316 | rl = NULL; |
| 1317 | page = NULL; |
| 1318 | } |
| 1319 | /* Determine the size of the mapping pairs array. */ |
Anton Altaparmakov | fa3be923 | 2005-06-25 17:15:36 +0100 | [diff] [blame^] | 1320 | mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1); |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1321 | if (unlikely(mp_size < 0)) { |
| 1322 | err = mp_size; |
| 1323 | ntfs_debug("Failed to get size for mapping pairs array, error " |
| 1324 | "code %i.", err); |
| 1325 | goto rl_err_out; |
| 1326 | } |
| 1327 | down_write(&ni->runlist.lock); |
| 1328 | if (!NInoAttr(ni)) |
| 1329 | base_ni = ni; |
| 1330 | else |
| 1331 | base_ni = ni->ext.base_ntfs_ino; |
| 1332 | m = map_mft_record(base_ni); |
| 1333 | if (IS_ERR(m)) { |
| 1334 | err = PTR_ERR(m); |
| 1335 | m = NULL; |
| 1336 | ctx = NULL; |
| 1337 | goto err_out; |
| 1338 | } |
| 1339 | ctx = ntfs_attr_get_search_ctx(base_ni, m); |
| 1340 | if (unlikely(!ctx)) { |
| 1341 | err = -ENOMEM; |
| 1342 | goto err_out; |
| 1343 | } |
| 1344 | err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, |
| 1345 | CASE_SENSITIVE, 0, NULL, 0, ctx); |
| 1346 | if (unlikely(err)) { |
| 1347 | if (err == -ENOENT) |
| 1348 | err = -EIO; |
| 1349 | goto err_out; |
| 1350 | } |
| 1351 | m = ctx->mrec; |
| 1352 | a = ctx->attr; |
| 1353 | BUG_ON(NInoNonResident(ni)); |
| 1354 | BUG_ON(a->non_resident); |
| 1355 | /* |
| 1356 | * Calculate new offsets for the name and the mapping pairs array. |
| 1357 | * We assume the attribute is not compressed or sparse. |
| 1358 | */ |
| 1359 | name_ofs = (offsetof(ATTR_REC, |
| 1360 | data.non_resident.compressed_size) + 7) & ~7; |
| 1361 | mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7; |
| 1362 | /* |
| 1363 | * Determine the size of the resident part of the now non-resident |
| 1364 | * attribute record. |
| 1365 | */ |
| 1366 | arec_size = (mp_ofs + mp_size + 7) & ~7; |
| 1367 | /* |
| 1368 | * If the page is not uptodate bring it uptodate by copying from the |
| 1369 | * attribute value. |
| 1370 | */ |
| 1371 | attr_size = le32_to_cpu(a->data.resident.value_length); |
| 1372 | BUG_ON(attr_size != i_size_read(vi)); |
| 1373 | if (page && !PageUptodate(page)) { |
| 1374 | kaddr = kmap_atomic(page, KM_USER0); |
| 1375 | memcpy(kaddr, (u8*)a + |
| 1376 | le16_to_cpu(a->data.resident.value_offset), |
| 1377 | attr_size); |
| 1378 | memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size); |
| 1379 | kunmap_atomic(kaddr, KM_USER0); |
| 1380 | flush_dcache_page(page); |
| 1381 | SetPageUptodate(page); |
| 1382 | } |
| 1383 | /* Backup the attribute flag. */ |
| 1384 | old_res_attr_flags = a->data.resident.flags; |
| 1385 | /* Resize the resident part of the attribute record. */ |
| 1386 | err = ntfs_attr_record_resize(m, a, arec_size); |
| 1387 | if (unlikely(err)) |
| 1388 | goto err_out; |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * Convert the resident part of the attribute record to describe a |
| 1391 | * non-resident attribute. |
| 1392 | */ |
| 1393 | a->non_resident = 1; |
| 1394 | /* Move the attribute name if it exists and update the offset. */ |
| 1395 | if (a->name_length) |
| 1396 | memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset), |
| 1397 | a->name_length * sizeof(ntfschar)); |
| 1398 | a->name_offset = cpu_to_le16(name_ofs); |
Anton Altaparmakov | 905685f | 2005-03-10 11:06:19 +0000 | [diff] [blame] | 1399 | /* |
| 1400 | * FIXME: For now just clear all of these as we do not support them |
| 1401 | * when writing. |
| 1402 | */ |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1403 | a->flags &= cpu_to_le16(0xffff & ~le16_to_cpu(ATTR_IS_SPARSE | |
| 1404 | ATTR_IS_ENCRYPTED | ATTR_COMPRESSION_MASK)); |
| 1405 | /* Setup the fields specific to non-resident attributes. */ |
| 1406 | a->data.non_resident.lowest_vcn = 0; |
| 1407 | a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >> |
| 1408 | vol->cluster_size_bits); |
| 1409 | a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs); |
| 1410 | a->data.non_resident.compression_unit = 0; |
| 1411 | memset(&a->data.non_resident.reserved, 0, |
| 1412 | sizeof(a->data.non_resident.reserved)); |
| 1413 | a->data.non_resident.allocated_size = cpu_to_sle64(new_size); |
| 1414 | a->data.non_resident.data_size = |
| 1415 | a->data.non_resident.initialized_size = |
| 1416 | cpu_to_sle64(attr_size); |
| 1417 | /* Generate the mapping pairs array into the attribute record. */ |
| 1418 | err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs, |
Anton Altaparmakov | fa3be923 | 2005-06-25 17:15:36 +0100 | [diff] [blame^] | 1419 | arec_size - mp_ofs, rl, 0, -1, NULL); |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1420 | if (unlikely(err)) { |
| 1421 | ntfs_debug("Failed to build mapping pairs, error code %i.", |
| 1422 | err); |
| 1423 | goto undo_err_out; |
| 1424 | } |
Anton Altaparmakov | 905685f | 2005-03-10 11:06:19 +0000 | [diff] [blame] | 1425 | /* Setup the in-memory attribute structure to be non-resident. */ |
| 1426 | /* |
| 1427 | * FIXME: For now just clear all of these as we do not support them |
| 1428 | * when writing. |
| 1429 | */ |
| 1430 | NInoClearSparse(ni); |
| 1431 | NInoClearEncrypted(ni); |
| 1432 | NInoClearCompressed(ni); |
| 1433 | ni->runlist.rl = rl; |
| 1434 | write_lock_irqsave(&ni->size_lock, flags); |
| 1435 | ni->allocated_size = new_size; |
| 1436 | write_unlock_irqrestore(&ni->size_lock, flags); |
| 1437 | /* |
| 1438 | * This needs to be last since the address space operations ->readpage |
| 1439 | * and ->writepage can run concurrently with us as they are not |
| 1440 | * serialized on i_sem. Note, we are not allowed to fail once we flip |
| 1441 | * this switch, which is another reason to do this last. |
| 1442 | */ |
| 1443 | NInoSetNonResident(ni); |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1444 | /* Mark the mft record dirty, so it gets written back. */ |
| 1445 | flush_dcache_mft_record_page(ctx->ntfs_ino); |
| 1446 | mark_mft_record_dirty(ctx->ntfs_ino); |
| 1447 | ntfs_attr_put_search_ctx(ctx); |
| 1448 | unmap_mft_record(base_ni); |
| 1449 | up_write(&ni->runlist.lock); |
| 1450 | if (page) { |
| 1451 | set_page_dirty(page); |
| 1452 | unlock_page(page); |
Anton Altaparmakov | 905685f | 2005-03-10 11:06:19 +0000 | [diff] [blame] | 1453 | mark_page_accessed(page); |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1454 | page_cache_release(page); |
| 1455 | } |
| 1456 | ntfs_debug("Done."); |
| 1457 | return 0; |
| 1458 | undo_err_out: |
| 1459 | /* Convert the attribute back into a resident attribute. */ |
| 1460 | a->non_resident = 0; |
| 1461 | /* Move the attribute name if it exists and update the offset. */ |
| 1462 | name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) + |
| 1463 | sizeof(a->data.resident.reserved) + 7) & ~7; |
| 1464 | if (a->name_length) |
| 1465 | memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset), |
| 1466 | a->name_length * sizeof(ntfschar)); |
| 1467 | mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7; |
| 1468 | a->name_offset = cpu_to_le16(name_ofs); |
| 1469 | arec_size = (mp_ofs + attr_size + 7) & ~7; |
| 1470 | /* Resize the resident part of the attribute record. */ |
| 1471 | err2 = ntfs_attr_record_resize(m, a, arec_size); |
| 1472 | if (unlikely(err2)) { |
| 1473 | /* |
| 1474 | * This cannot happen (well if memory corruption is at work it |
| 1475 | * could happen in theory), but deal with it as well as we can. |
| 1476 | * If the old size is too small, truncate the attribute, |
| 1477 | * otherwise simply give it a larger allocated size. |
| 1478 | * FIXME: Should check whether chkdsk complains when the |
| 1479 | * allocated size is much bigger than the resident value size. |
| 1480 | */ |
| 1481 | arec_size = le32_to_cpu(a->length); |
| 1482 | if ((mp_ofs + attr_size) > arec_size) { |
| 1483 | err2 = attr_size; |
| 1484 | attr_size = arec_size - mp_ofs; |
| 1485 | ntfs_error(vol->sb, "Failed to undo partial resident " |
| 1486 | "to non-resident attribute " |
| 1487 | "conversion. Truncating inode 0x%lx, " |
| 1488 | "attribute type 0x%x from %i bytes to " |
| 1489 | "%i bytes to maintain metadata " |
| 1490 | "consistency. THIS MEANS YOU ARE " |
| 1491 | "LOSING %i BYTES DATA FROM THIS %s.", |
| 1492 | vi->i_ino, |
| 1493 | (unsigned)le32_to_cpu(ni->type), |
| 1494 | err2, attr_size, err2 - attr_size, |
| 1495 | ((ni->type == AT_DATA) && |
| 1496 | !ni->name_len) ? "FILE": "ATTRIBUTE"); |
| 1497 | write_lock_irqsave(&ni->size_lock, flags); |
| 1498 | ni->initialized_size = attr_size; |
| 1499 | i_size_write(vi, attr_size); |
| 1500 | write_unlock_irqrestore(&ni->size_lock, flags); |
| 1501 | } |
| 1502 | } |
| 1503 | /* Setup the fields specific to resident attributes. */ |
| 1504 | a->data.resident.value_length = cpu_to_le32(attr_size); |
| 1505 | a->data.resident.value_offset = cpu_to_le16(mp_ofs); |
| 1506 | a->data.resident.flags = old_res_attr_flags; |
| 1507 | memset(&a->data.resident.reserved, 0, |
| 1508 | sizeof(a->data.resident.reserved)); |
| 1509 | /* Copy the data from the page back to the attribute value. */ |
| 1510 | if (page) { |
| 1511 | kaddr = kmap_atomic(page, KM_USER0); |
| 1512 | memcpy((u8*)a + mp_ofs, kaddr, attr_size); |
| 1513 | kunmap_atomic(kaddr, KM_USER0); |
| 1514 | } |
Anton Altaparmakov | 905685f | 2005-03-10 11:06:19 +0000 | [diff] [blame] | 1515 | /* Setup the allocated size in the ntfs inode in case it changed. */ |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1516 | write_lock_irqsave(&ni->size_lock, flags); |
| 1517 | ni->allocated_size = arec_size - mp_ofs; |
| 1518 | write_unlock_irqrestore(&ni->size_lock, flags); |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1519 | /* Mark the mft record dirty, so it gets written back. */ |
| 1520 | flush_dcache_mft_record_page(ctx->ntfs_ino); |
| 1521 | mark_mft_record_dirty(ctx->ntfs_ino); |
| 1522 | err_out: |
| 1523 | if (ctx) |
| 1524 | ntfs_attr_put_search_ctx(ctx); |
| 1525 | if (m) |
| 1526 | unmap_mft_record(base_ni); |
| 1527 | ni->runlist.rl = NULL; |
| 1528 | up_write(&ni->runlist.lock); |
| 1529 | rl_err_out: |
| 1530 | if (rl) { |
| 1531 | if (ntfs_cluster_free_from_rl(vol, rl) < 0) { |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1532 | ntfs_error(vol->sb, "Failed to release allocated " |
| 1533 | "cluster(s) in error code path. Run " |
| 1534 | "chkdsk to recover the lost " |
| 1535 | "cluster(s)."); |
| 1536 | NVolSetErrors(vol); |
| 1537 | } |
Anton Altaparmakov | 53d59aa | 2005-03-17 10:51:33 +0000 | [diff] [blame] | 1538 | ntfs_free(rl); |
Anton Altaparmakov | 2bfb4ff | 2005-03-09 15:15:06 +0000 | [diff] [blame] | 1539 | page_err_out: |
| 1540 | unlock_page(page); |
| 1541 | page_cache_release(page); |
| 1542 | } |
| 1543 | if (err == -EINVAL) |
| 1544 | err = -EIO; |
| 1545 | return err; |
| 1546 | } |
| 1547 | |
| 1548 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | * ntfs_attr_set - fill (a part of) an attribute with a byte |
| 1550 | * @ni: ntfs inode describing the attribute to fill |
| 1551 | * @ofs: offset inside the attribute at which to start to fill |
| 1552 | * @cnt: number of bytes to fill |
| 1553 | * @val: the unsigned 8-bit value with which to fill the attribute |
| 1554 | * |
| 1555 | * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at |
| 1556 | * byte offset @ofs inside the attribute with the constant byte @val. |
| 1557 | * |
| 1558 | * This function is effectively like memset() applied to an ntfs attribute. |
Anton Altaparmakov | da28438 | 2004-11-11 11:18:10 +0000 | [diff] [blame] | 1559 | * Note thie function actually only operates on the page cache pages belonging |
| 1560 | * to the ntfs attribute and it marks them dirty after doing the memset(). |
| 1561 | * Thus it relies on the vm dirty page write code paths to cause the modified |
| 1562 | * pages to be written to the mft record/disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | * |
| 1564 | * Return 0 on success and -errno on error. An error code of -ESPIPE means |
| 1565 | * that @ofs + @cnt were outside the end of the attribute and no write was |
| 1566 | * performed. |
| 1567 | */ |
| 1568 | int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val) |
| 1569 | { |
| 1570 | ntfs_volume *vol = ni->vol; |
| 1571 | struct address_space *mapping; |
| 1572 | struct page *page; |
| 1573 | u8 *kaddr; |
| 1574 | pgoff_t idx, end; |
| 1575 | unsigned int start_ofs, end_ofs, size; |
| 1576 | |
| 1577 | ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.", |
| 1578 | (long long)ofs, (long long)cnt, val); |
| 1579 | BUG_ON(ofs < 0); |
| 1580 | BUG_ON(cnt < 0); |
| 1581 | if (!cnt) |
| 1582 | goto done; |
| 1583 | mapping = VFS_I(ni)->i_mapping; |
| 1584 | /* Work out the starting index and page offset. */ |
| 1585 | idx = ofs >> PAGE_CACHE_SHIFT; |
| 1586 | start_ofs = ofs & ~PAGE_CACHE_MASK; |
| 1587 | /* Work out the ending index and page offset. */ |
| 1588 | end = ofs + cnt; |
| 1589 | end_ofs = end & ~PAGE_CACHE_MASK; |
| 1590 | /* If the end is outside the inode size return -ESPIPE. */ |
Anton Altaparmakov | da28438 | 2004-11-11 11:18:10 +0000 | [diff] [blame] | 1591 | if (unlikely(end > i_size_read(VFS_I(ni)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | ntfs_error(vol->sb, "Request exceeds end of attribute."); |
| 1593 | return -ESPIPE; |
| 1594 | } |
| 1595 | end >>= PAGE_CACHE_SHIFT; |
| 1596 | /* If there is a first partial page, need to do it the slow way. */ |
| 1597 | if (start_ofs) { |
| 1598 | page = read_cache_page(mapping, idx, |
| 1599 | (filler_t*)mapping->a_ops->readpage, NULL); |
| 1600 | if (IS_ERR(page)) { |
| 1601 | ntfs_error(vol->sb, "Failed to read first partial " |
| 1602 | "page (sync error, index 0x%lx).", idx); |
| 1603 | return PTR_ERR(page); |
| 1604 | } |
| 1605 | wait_on_page_locked(page); |
| 1606 | if (unlikely(!PageUptodate(page))) { |
| 1607 | ntfs_error(vol->sb, "Failed to read first partial page " |
| 1608 | "(async error, index 0x%lx).", idx); |
| 1609 | page_cache_release(page); |
| 1610 | return PTR_ERR(page); |
| 1611 | } |
| 1612 | /* |
| 1613 | * If the last page is the same as the first page, need to |
| 1614 | * limit the write to the end offset. |
| 1615 | */ |
| 1616 | size = PAGE_CACHE_SIZE; |
| 1617 | if (idx == end) |
| 1618 | size = end_ofs; |
| 1619 | kaddr = kmap_atomic(page, KM_USER0); |
| 1620 | memset(kaddr + start_ofs, val, size - start_ofs); |
| 1621 | flush_dcache_page(page); |
| 1622 | kunmap_atomic(kaddr, KM_USER0); |
| 1623 | set_page_dirty(page); |
| 1624 | page_cache_release(page); |
| 1625 | if (idx == end) |
| 1626 | goto done; |
| 1627 | idx++; |
| 1628 | } |
| 1629 | /* Do the whole pages the fast way. */ |
| 1630 | for (; idx < end; idx++) { |
| 1631 | /* Find or create the current page. (The page is locked.) */ |
| 1632 | page = grab_cache_page(mapping, idx); |
| 1633 | if (unlikely(!page)) { |
| 1634 | ntfs_error(vol->sb, "Insufficient memory to grab " |
| 1635 | "page (index 0x%lx).", idx); |
| 1636 | return -ENOMEM; |
| 1637 | } |
| 1638 | kaddr = kmap_atomic(page, KM_USER0); |
| 1639 | memset(kaddr, val, PAGE_CACHE_SIZE); |
| 1640 | flush_dcache_page(page); |
| 1641 | kunmap_atomic(kaddr, KM_USER0); |
| 1642 | /* |
| 1643 | * If the page has buffers, mark them uptodate since buffer |
| 1644 | * state and not page state is definitive in 2.6 kernels. |
| 1645 | */ |
| 1646 | if (page_has_buffers(page)) { |
| 1647 | struct buffer_head *bh, *head; |
| 1648 | |
| 1649 | bh = head = page_buffers(page); |
| 1650 | do { |
| 1651 | set_buffer_uptodate(bh); |
| 1652 | } while ((bh = bh->b_this_page) != head); |
| 1653 | } |
| 1654 | /* Now that buffers are uptodate, set the page uptodate, too. */ |
| 1655 | SetPageUptodate(page); |
| 1656 | /* |
| 1657 | * Set the page and all its buffers dirty and mark the inode |
| 1658 | * dirty, too. The VM will write the page later on. |
| 1659 | */ |
| 1660 | set_page_dirty(page); |
| 1661 | /* Finally unlock and release the page. */ |
| 1662 | unlock_page(page); |
| 1663 | page_cache_release(page); |
| 1664 | } |
| 1665 | /* If there is a last partial page, need to do it the slow way. */ |
| 1666 | if (end_ofs) { |
| 1667 | page = read_cache_page(mapping, idx, |
| 1668 | (filler_t*)mapping->a_ops->readpage, NULL); |
| 1669 | if (IS_ERR(page)) { |
| 1670 | ntfs_error(vol->sb, "Failed to read last partial page " |
| 1671 | "(sync error, index 0x%lx).", idx); |
| 1672 | return PTR_ERR(page); |
| 1673 | } |
| 1674 | wait_on_page_locked(page); |
| 1675 | if (unlikely(!PageUptodate(page))) { |
| 1676 | ntfs_error(vol->sb, "Failed to read last partial page " |
| 1677 | "(async error, index 0x%lx).", idx); |
| 1678 | page_cache_release(page); |
| 1679 | return PTR_ERR(page); |
| 1680 | } |
| 1681 | kaddr = kmap_atomic(page, KM_USER0); |
| 1682 | memset(kaddr, val, end_ofs); |
| 1683 | flush_dcache_page(page); |
| 1684 | kunmap_atomic(kaddr, KM_USER0); |
| 1685 | set_page_dirty(page); |
| 1686 | page_cache_release(page); |
| 1687 | } |
| 1688 | done: |
| 1689 | ntfs_debug("Done."); |
| 1690 | return 0; |
| 1691 | } |
Anton Altaparmakov | 53d59aa | 2005-03-17 10:51:33 +0000 | [diff] [blame] | 1692 | |
| 1693 | #endif /* NTFS_RW */ |