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