blob: eda056bac2567a51c99ef521347e066ac04f86a5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
3 *
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +00004 * Copyright (c) 2001-2005 Anton Altaparmakov
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 Altaparmakov29b89902005-10-11 14:54:42 +010024#include <linux/sched.h>
Anton Altaparmakov1ef334d2005-04-04 14:59:42 +010025#include <linux/swap.h>
Anton Altaparmakov29b89902005-10-11 14:54:42 +010026#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include "attrib.h"
29#include "debug.h"
30#include "layout.h"
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +000031#include "lcnalloc.h"
32#include "malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "mft.h"
34#include "ntfs.h"
35#include "types.h"
36
37/**
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +000038 * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * @ni: ntfs inode for which to map (part of) a runlist
40 * @vcn: map runlist part containing this vcn
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +010041 * @ctx: active attribute search context if present or NULL if not
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
43 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
44 *
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +010045 * If @ctx is specified, it is an active search context of @ni and its base mft
46 * record. This is needed when ntfs_map_runlist_nolock() encounters unmapped
47 * runlist fragments and allows their mapping. If you do not have the mft
48 * record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock()
49 * will perform the necessary mapping and unmapping.
50 *
51 * Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and
52 * restores it before returning. Thus, @ctx will be left pointing to the same
53 * attribute on return as on entry. However, the actual pointers in @ctx may
54 * point to different memory locations on return, so you must remember to reset
55 * any cached pointers from the @ctx, i.e. after the call to
56 * ntfs_map_runlist_nolock(), you will probably want to do:
57 * m = ctx->mrec;
58 * a = ctx->attr;
59 * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
60 * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
61 *
Anton Altaparmakov4757d7df2005-06-25 17:24:08 +010062 * Return 0 on success and -errno on error. There is one special error code
63 * which is not an error as such. This is -ENOENT. It means that @vcn is out
64 * of bounds of the runlist.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
Anton Altaparmakov2983d1b2005-09-08 20:56:09 +010066 * Note the runlist can be NULL after this function returns if @vcn is zero and
67 * the attribute has zero allocated size, i.e. there simply is no runlist.
68 *
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +010069 * WARNING: If @ctx is supplied, regardless of whether success or failure is
70 * returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx
71 * is no longer valid, i.e. you need to either call
72 * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
73 * In that case PTR_ERR(@ctx->mrec) will give you the error code for
74 * why the mapping of the old inode failed.
75 *
76 * Locking: - The runlist described by @ni must be locked for writing on entry
77 * and is locked on return. Note the runlist will be modified.
78 * - If @ctx is NULL, the base mft record of @ni must not be mapped on
79 * entry and it will be left unmapped on return.
80 * - If @ctx is not NULL, the base mft record must be mapped on entry
81 * and it will be left mapped on return.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +010083int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Anton Altaparmakov4757d7df2005-06-25 17:24:08 +010085 VCN end_vcn;
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +010086 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ntfs_inode *base_ni;
Anton Altaparmakov4757d7df2005-06-25 17:24:08 +010088 MFT_RECORD *m;
89 ATTR_RECORD *a;
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +000090 runlist_element *rl;
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +010091 struct page *put_this_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int err = 0;
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +010093 BOOL ctx_is_temporary, ctx_needs_reset;
Anton Altaparmakovdda65b92005-10-24 08:57:59 +010094 ntfs_attr_search_ctx old_ctx = { NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
97 (unsigned long long)vcn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (!NInoAttr(ni))
99 base_ni = ni;
100 else
101 base_ni = ni->ext.base_ntfs_ino;
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +0100102 if (!ctx) {
103 ctx_is_temporary = ctx_needs_reset = TRUE;
104 m = map_mft_record(base_ni);
105 if (IS_ERR(m))
106 return PTR_ERR(m);
107 ctx = ntfs_attr_get_search_ctx(base_ni, m);
108 if (unlikely(!ctx)) {
109 err = -ENOMEM;
110 goto err_out;
111 }
112 } else {
113 VCN allocated_size_vcn;
114
115 BUG_ON(IS_ERR(ctx->mrec));
116 a = ctx->attr;
117 BUG_ON(!a->non_resident);
118 ctx_is_temporary = FALSE;
119 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
120 read_lock_irqsave(&ni->size_lock, flags);
121 allocated_size_vcn = ni->allocated_size >>
122 ni->vol->cluster_size_bits;
123 read_unlock_irqrestore(&ni->size_lock, flags);
124 if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
125 end_vcn = allocated_size_vcn - 1;
126 /*
127 * If we already have the attribute extent containing @vcn in
128 * @ctx, no need to look it up again. We slightly cheat in
129 * that if vcn exceeds the allocated size, we will refuse to
130 * map the runlist below, so there is definitely no need to get
131 * the right attribute extent.
132 */
133 if (vcn >= allocated_size_vcn || (a->type == ni->type &&
134 a->name_length == ni->name_len &&
135 !memcmp((u8*)a + le16_to_cpu(a->name_offset),
136 ni->name, ni->name_len) &&
137 sle64_to_cpu(a->data.non_resident.lowest_vcn)
138 <= vcn && end_vcn >= vcn))
139 ctx_needs_reset = FALSE;
140 else {
141 /* Save the old search context. */
142 old_ctx = *ctx;
143 /*
144 * If the currently mapped (extent) inode is not the
145 * base inode we will unmap it when we reinitialize the
146 * search context which means we need to get a
147 * reference to the page containing the mapped mft
148 * record so we do not accidentally drop changes to the
149 * mft record when it has not been marked dirty yet.
150 */
151 if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
152 old_ctx.base_ntfs_ino) {
153 put_this_page = old_ctx.ntfs_ino->page;
154 page_cache_get(put_this_page);
155 }
156 /*
157 * Reinitialize the search context so we can lookup the
158 * needed attribute extent.
159 */
160 ntfs_attr_reinit_search_ctx(ctx);
161 ctx_needs_reset = TRUE;
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +0100164 if (ctx_needs_reset) {
165 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
166 CASE_SENSITIVE, vcn, NULL, 0, ctx);
167 if (unlikely(err)) {
168 if (err == -ENOENT)
169 err = -EIO;
170 goto err_out;
171 }
172 BUG_ON(!ctx->attr->non_resident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Anton Altaparmakov4757d7df2005-06-25 17:24:08 +0100174 a = ctx->attr;
175 /*
176 * Only decompress the mapping pairs if @vcn is inside it. Otherwise
177 * we get into problems when we try to map an out of bounds vcn because
178 * we then try to map the already mapped runlist fragment and
179 * ntfs_mapping_pairs_decompress() fails.
180 */
181 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +0100182 if (!a->data.non_resident.lowest_vcn && end_vcn == 1)
183 end_vcn = sle64_to_cpu(a->data.non_resident.allocated_size) >>
184 ni->vol->cluster_size_bits;
Anton Altaparmakov4757d7df2005-06-25 17:24:08 +0100185 if (unlikely(vcn >= end_vcn)) {
186 err = -ENOENT;
187 goto err_out;
188 }
189 rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
190 if (IS_ERR(rl))
191 err = PTR_ERR(rl);
192 else
193 ni->runlist.rl = rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194err_out:
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +0100195 if (ctx_is_temporary) {
196 if (likely(ctx))
197 ntfs_attr_put_search_ctx(ctx);
198 unmap_mft_record(base_ni);
199 } else if (ctx_needs_reset) {
200 /*
201 * If there is no attribute list, restoring the search context
202 * is acomplished simply by copying the saved context back over
203 * the caller supplied context. If there is an attribute list,
204 * things are more complicated as we need to deal with mapping
205 * of mft records and resulting potential changes in pointers.
206 */
207 if (NInoAttrList(base_ni)) {
208 /*
209 * If the currently mapped (extent) inode is not the
210 * one we had before, we need to unmap it and map the
211 * old one.
212 */
213 if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
214 /*
215 * If the currently mapped inode is not the
216 * base inode, unmap it.
217 */
218 if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
219 ctx->base_ntfs_ino) {
220 unmap_extent_mft_record(ctx->ntfs_ino);
221 ctx->mrec = ctx->base_mrec;
222 BUG_ON(!ctx->mrec);
223 }
224 /*
225 * If the old mapped inode is not the base
226 * inode, map it.
227 */
228 if (old_ctx.base_ntfs_ino &&
229 old_ctx.ntfs_ino !=
230 old_ctx.base_ntfs_ino) {
231retry_map:
232 ctx->mrec = map_mft_record(
233 old_ctx.ntfs_ino);
234 /*
235 * Something bad has happened. If out
236 * of memory retry till it succeeds.
237 * Any other errors are fatal and we
238 * return the error code in ctx->mrec.
239 * Let the caller deal with it... We
240 * just need to fudge things so the
241 * caller can reinit and/or put the
242 * search context safely.
243 */
244 if (IS_ERR(ctx->mrec)) {
245 if (PTR_ERR(ctx->mrec) ==
246 -ENOMEM) {
247 schedule();
248 goto retry_map;
249 } else
250 old_ctx.ntfs_ino =
251 old_ctx.
252 base_ntfs_ino;
253 }
254 }
255 }
256 /* Update the changed pointers in the saved context. */
257 if (ctx->mrec != old_ctx.mrec) {
258 if (!IS_ERR(ctx->mrec))
259 old_ctx.attr = (ATTR_RECORD*)(
260 (u8*)ctx->mrec +
261 ((u8*)old_ctx.attr -
262 (u8*)old_ctx.mrec));
263 old_ctx.mrec = ctx->mrec;
264 }
265 }
266 /* Restore the search context to the saved one. */
267 *ctx = old_ctx;
268 /*
269 * We drop the reference on the page we took earlier. In the
270 * case that IS_ERR(ctx->mrec) is true this means we might lose
271 * some changes to the mft record that had been made between
272 * the last time it was marked dirty/written out and now. This
273 * at this stage is not a problem as the mapping error is fatal
274 * enough that the mft record cannot be written out anyway and
275 * the caller is very likely to shutdown the whole inode
276 * immediately and mark the volume dirty for chkdsk to pick up
277 * the pieces anyway.
278 */
279 if (put_this_page)
280 page_cache_release(put_this_page);
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return err;
283}
284
285/**
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000286 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
287 * @ni: ntfs inode for which to map (part of) a runlist
288 * @vcn: map runlist part containing this vcn
289 *
290 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
291 *
Anton Altaparmakov4757d7df2005-06-25 17:24:08 +0100292 * Return 0 on success and -errno on error. There is one special error code
293 * which is not an error as such. This is -ENOENT. It means that @vcn is out
294 * of bounds of the runlist.
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000295 *
296 * Locking: - The runlist must be unlocked on entry and is unlocked on return.
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +0100297 * - This function takes the runlist lock for writing and may modify
298 * the runlist.
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000299 */
300int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
301{
302 int err = 0;
303
304 down_write(&ni->runlist.lock);
305 /* Make sure someone else didn't do the work while we were sleeping. */
306 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
307 LCN_RL_NOT_MAPPED))
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +0100308 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000309 up_write(&ni->runlist.lock);
310 return err;
311}
312
313/**
Anton Altaparmakov271849a2005-03-07 21:36:18 +0000314 * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
315 * @ni: ntfs inode of the attribute whose runlist to search
316 * @vcn: vcn to convert
317 * @write_locked: true if the runlist is locked for writing
318 *
319 * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
320 * described by the ntfs inode @ni and return the corresponding logical cluster
321 * number (lcn).
322 *
323 * If the @vcn is not mapped yet, the attempt is made to map the attribute
324 * extent containing the @vcn and the vcn to lcn conversion is retried.
325 *
326 * If @write_locked is true the caller has locked the runlist for writing and
327 * if false for reading.
328 *
329 * Since lcns must be >= 0, we use negative return codes with special meaning:
330 *
331 * Return code Meaning / Description
332 * ==========================================
333 * LCN_HOLE Hole / not allocated on disk.
334 * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds.
335 * LCN_ENOMEM Not enough memory to map runlist.
336 * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc).
337 *
338 * Locking: - The runlist must be locked on entry and is left locked on return.
339 * - If @write_locked is FALSE, i.e. the runlist is locked for reading,
340 * the lock may be dropped inside the function so you cannot rely on
341 * the runlist still being the same when this function returns.
342 */
343LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
344 const BOOL write_locked)
345{
346 LCN lcn;
Anton Altaparmakov2983d1b2005-09-08 20:56:09 +0100347 unsigned long flags;
Anton Altaparmakov271849a2005-03-07 21:36:18 +0000348 BOOL is_retry = FALSE;
349
350 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
351 ni->mft_no, (unsigned long long)vcn,
352 write_locked ? "write" : "read");
353 BUG_ON(!ni);
354 BUG_ON(!NInoNonResident(ni));
355 BUG_ON(vcn < 0);
Anton Altaparmakov2983d1b2005-09-08 20:56:09 +0100356 if (!ni->runlist.rl) {
357 read_lock_irqsave(&ni->size_lock, flags);
358 if (!ni->allocated_size) {
359 read_unlock_irqrestore(&ni->size_lock, flags);
360 return LCN_ENOENT;
361 }
362 read_unlock_irqrestore(&ni->size_lock, flags);
363 }
Anton Altaparmakov271849a2005-03-07 21:36:18 +0000364retry_remap:
365 /* Convert vcn to lcn. If that fails map the runlist and retry once. */
366 lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
367 if (likely(lcn >= LCN_HOLE)) {
368 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
369 return lcn;
370 }
371 if (lcn != LCN_RL_NOT_MAPPED) {
372 if (lcn != LCN_ENOENT)
373 lcn = LCN_EIO;
374 } else if (!is_retry) {
375 int err;
376
377 if (!write_locked) {
378 up_read(&ni->runlist.lock);
379 down_write(&ni->runlist.lock);
380 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
381 LCN_RL_NOT_MAPPED)) {
382 up_write(&ni->runlist.lock);
383 down_read(&ni->runlist.lock);
384 goto retry_remap;
385 }
386 }
Anton Altaparmakovfd9d6362005-10-04 13:44:48 +0100387 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
Anton Altaparmakov271849a2005-03-07 21:36:18 +0000388 if (!write_locked) {
389 up_write(&ni->runlist.lock);
390 down_read(&ni->runlist.lock);
391 }
392 if (likely(!err)) {
393 is_retry = TRUE;
394 goto retry_remap;
395 }
396 if (err == -ENOENT)
397 lcn = LCN_ENOENT;
398 else if (err == -ENOMEM)
399 lcn = LCN_ENOMEM;
400 else
401 lcn = LCN_EIO;
402 }
403 if (lcn != LCN_ENOENT)
404 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
405 (long long)lcn);
406 return lcn;
407}
408
409/**
Anton Altaparmakovc0c1cc02005-03-07 21:43:38 +0000410 * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
Anton Altaparmakov69b41e32005-10-04 14:01:14 +0100411 * @ni: ntfs inode describing the runlist to search
412 * @vcn: vcn to find
413 * @ctx: active attribute search context if present or NULL if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 *
415 * Find the virtual cluster number @vcn in the runlist described by the ntfs
416 * inode @ni and return the address of the runlist element containing the @vcn.
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000417 *
Anton Altaparmakovc0c1cc02005-03-07 21:43:38 +0000418 * If the @vcn is not mapped yet, the attempt is made to map the attribute
419 * extent containing the @vcn and the vcn to lcn conversion is retried.
420 *
Anton Altaparmakov69b41e32005-10-04 14:01:14 +0100421 * If @ctx is specified, it is an active search context of @ni and its base mft
422 * record. This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped
423 * runlist fragments and allows their mapping. If you do not have the mft
424 * record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock()
425 * will perform the necessary mapping and unmapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Anton Altaparmakov69b41e32005-10-04 14:01:14 +0100427 * Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and
428 * restores it before returning. Thus, @ctx will be left pointing to the same
429 * attribute on return as on entry. However, the actual pointers in @ctx may
430 * point to different memory locations on return, so you must remember to reset
431 * any cached pointers from the @ctx, i.e. after the call to
432 * ntfs_attr_find_vcn_nolock(), you will probably want to do:
433 * m = ctx->mrec;
434 * a = ctx->attr;
435 * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
436 * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * Note you need to distinguish between the lcn of the returned runlist element
438 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
439 * read and allocate clusters on write.
440 *
441 * Return the runlist element containing the @vcn on success and
442 * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
443 * to decide if the return is success or failure and PTR_ERR() to get to the
444 * error code if IS_ERR() is true.
445 *
446 * The possible error return codes are:
447 * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
448 * -ENOMEM - Not enough memory to map runlist.
449 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
450 *
Anton Altaparmakov69b41e32005-10-04 14:01:14 +0100451 * WARNING: If @ctx is supplied, regardless of whether success or failure is
452 * returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx
453 * is no longer valid, i.e. you need to either call
454 * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
455 * In that case PTR_ERR(@ctx->mrec) will give you the error code for
456 * why the mapping of the old inode failed.
457 *
458 * Locking: - The runlist described by @ni must be locked for writing on entry
459 * and is locked on return. Note the runlist may be modified when
460 * needed runlist fragments need to be mapped.
461 * - If @ctx is NULL, the base mft record of @ni must not be mapped on
462 * entry and it will be left unmapped on return.
463 * - If @ctx is not NULL, the base mft record must be mapped on entry
464 * and it will be left mapped on return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Anton Altaparmakovc0c1cc02005-03-07 21:43:38 +0000466runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
Anton Altaparmakov69b41e32005-10-04 14:01:14 +0100467 ntfs_attr_search_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Anton Altaparmakov2983d1b2005-09-08 20:56:09 +0100469 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 runlist_element *rl;
471 int err = 0;
472 BOOL is_retry = FALSE;
473
Anton Altaparmakov69b41e32005-10-04 14:01:14 +0100474 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
475 ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 BUG_ON(!ni);
477 BUG_ON(!NInoNonResident(ni));
478 BUG_ON(vcn < 0);
Anton Altaparmakov2983d1b2005-09-08 20:56:09 +0100479 if (!ni->runlist.rl) {
480 read_lock_irqsave(&ni->size_lock, flags);
481 if (!ni->allocated_size) {
482 read_unlock_irqrestore(&ni->size_lock, flags);
483 return ERR_PTR(-ENOENT);
484 }
485 read_unlock_irqrestore(&ni->size_lock, flags);
486 }
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000487retry_remap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 rl = ni->runlist.rl;
489 if (likely(rl && vcn >= rl[0].vcn)) {
490 while (likely(rl->length)) {
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000491 if (unlikely(vcn < rl[1].vcn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (likely(rl->lcn >= LCN_HOLE)) {
493 ntfs_debug("Done.");
494 return rl;
495 }
496 break;
497 }
498 rl++;
499 }
500 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
501 if (likely(rl->lcn == LCN_ENOENT))
502 err = -ENOENT;
503 else
504 err = -EIO;
505 }
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!err && !is_retry) {
508 /*
Anton Altaparmakov69b41e32005-10-04 14:01:14 +0100509 * If the search context is invalid we cannot map the unmapped
510 * region.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Anton Altaparmakov69b41e32005-10-04 14:01:14 +0100512 if (IS_ERR(ctx->mrec))
513 err = PTR_ERR(ctx->mrec);
514 else {
515 /*
516 * The @vcn is in an unmapped region, map the runlist
517 * and retry.
518 */
519 err = ntfs_map_runlist_nolock(ni, vcn, ctx);
520 if (likely(!err)) {
521 is_retry = TRUE;
Anton Altaparmakovc0c1cc02005-03-07 21:43:38 +0000522 goto retry_remap;
523 }
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000524 }
Anton Altaparmakov4757d7df2005-06-25 17:24:08 +0100525 if (err == -EINVAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 err = -EIO;
527 } else if (!err)
528 err = -EIO;
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000529 if (err != -ENOENT)
530 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return ERR_PTR(err);
532}
533
534/**
535 * ntfs_attr_find - find (next) attribute in mft record
536 * @type: attribute type to find
537 * @name: attribute name to find (optional, i.e. NULL means don't care)
538 * @name_len: attribute name length (only needed if @name present)
539 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
540 * @val: attribute value to find (optional, resident attributes only)
541 * @val_len: attribute value length
542 * @ctx: search context with mft record and attribute to search from
543 *
544 * You should not need to call this function directly. Use ntfs_attr_lookup()
545 * instead.
546 *
547 * ntfs_attr_find() takes a search context @ctx as parameter and searches the
548 * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
549 * attribute of @type, optionally @name and @val.
550 *
551 * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
552 * point to the found attribute.
553 *
554 * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
555 * @ctx->attr will point to the attribute before which the attribute being
556 * searched for would need to be inserted if such an action were to be desired.
557 *
558 * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
559 * undefined and in particular do not rely on it not changing.
560 *
561 * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it
562 * is FALSE, the search begins after @ctx->attr.
563 *
564 * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
565 * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
566 * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
567 * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
568 * sensitive. When @name is present, @name_len is the @name length in Unicode
569 * characters.
570 *
571 * If @name is not present (NULL), we assume that the unnamed attribute is
572 * being searched for.
573 *
574 * Finally, the resident attribute value @val is looked for, if present. If
575 * @val is not present (NULL), @val_len is ignored.
576 *
577 * ntfs_attr_find() only searches the specified mft record and it ignores the
578 * presence of an attribute list attribute (unless it is the one being searched
579 * for, obviously). If you need to take attribute lists into consideration,
580 * use ntfs_attr_lookup() instead (see below). This also means that you cannot
581 * use ntfs_attr_find() to search for extent records of non-resident
582 * attributes, as extents with lowest_vcn != 0 are usually described by the
583 * attribute list attribute only. - Note that it is possible that the first
584 * extent is only in the attribute list while the last extent is in the base
585 * mft record, so do not rely on being able to find the first extent in the
586 * base mft record.
587 *
588 * Warning: Never use @val when looking for attribute types which can be
589 * non-resident as this most likely will result in a crash!
590 */
591static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
592 const u32 name_len, const IGNORE_CASE_BOOL ic,
593 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
594{
595 ATTR_RECORD *a;
596 ntfs_volume *vol = ctx->ntfs_ino->vol;
597 ntfschar *upcase = vol->upcase;
598 u32 upcase_len = vol->upcase_len;
599
600 /*
601 * Iterate over attributes in mft record starting at @ctx->attr, or the
602 * attribute following that, if @ctx->is_first is TRUE.
603 */
604 if (ctx->is_first) {
605 a = ctx->attr;
606 ctx->is_first = FALSE;
607 } else
608 a = (ATTR_RECORD*)((u8*)ctx->attr +
609 le32_to_cpu(ctx->attr->length));
610 for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
611 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
612 le32_to_cpu(ctx->mrec->bytes_allocated))
613 break;
614 ctx->attr = a;
615 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
616 a->type == AT_END))
617 return -ENOENT;
618 if (unlikely(!a->length))
619 break;
620 if (a->type != type)
621 continue;
622 /*
623 * If @name is present, compare the two names. If @name is
624 * missing, assume we want an unnamed attribute.
625 */
626 if (!name) {
627 /* The search failed if the found attribute is named. */
628 if (a->name_length)
629 return -ENOENT;
630 } else if (!ntfs_are_names_equal(name, name_len,
631 (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
632 a->name_length, ic, upcase, upcase_len)) {
633 register int rc;
634
635 rc = ntfs_collate_names(name, name_len,
636 (ntfschar*)((u8*)a +
637 le16_to_cpu(a->name_offset)),
638 a->name_length, 1, IGNORE_CASE,
639 upcase, upcase_len);
640 /*
641 * If @name collates before a->name, there is no
642 * matching attribute.
643 */
644 if (rc == -1)
645 return -ENOENT;
646 /* If the strings are not equal, continue search. */
647 if (rc)
648 continue;
649 rc = ntfs_collate_names(name, name_len,
650 (ntfschar*)((u8*)a +
651 le16_to_cpu(a->name_offset)),
652 a->name_length, 1, CASE_SENSITIVE,
653 upcase, upcase_len);
654 if (rc == -1)
655 return -ENOENT;
656 if (rc)
657 continue;
658 }
659 /*
660 * The names match or @name not present and attribute is
661 * unnamed. If no @val specified, we have found the attribute
662 * and are done.
663 */
664 if (!val)
665 return 0;
666 /* @val is present; compare values. */
667 else {
668 register int rc;
669
670 rc = memcmp(val, (u8*)a + le16_to_cpu(
671 a->data.resident.value_offset),
672 min_t(u32, val_len, le32_to_cpu(
673 a->data.resident.value_length)));
674 /*
675 * If @val collates before the current attribute's
676 * value, there is no matching attribute.
677 */
678 if (!rc) {
679 register u32 avl;
680
681 avl = le32_to_cpu(
682 a->data.resident.value_length);
683 if (val_len == avl)
684 return 0;
685 if (val_len < avl)
686 return -ENOENT;
687 } else if (rc < 0)
688 return -ENOENT;
689 }
690 }
691 ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
692 NVolSetErrors(vol);
693 return -EIO;
694}
695
696/**
697 * load_attribute_list - load an attribute list into memory
698 * @vol: ntfs volume from which to read
699 * @runlist: runlist of the attribute list
700 * @al_start: destination buffer
701 * @size: size of the destination buffer in bytes
702 * @initialized_size: initialized size of the attribute list
703 *
704 * Walk the runlist @runlist and load all clusters from it copying them into
705 * the linear buffer @al. The maximum number of bytes copied to @al is @size
706 * bytes. Note, @size does not need to be a multiple of the cluster size. If
707 * @initialized_size is less than @size, the region in @al between
708 * @initialized_size and @size will be zeroed and not read from disk.
709 *
710 * Return 0 on success or -errno on error.
711 */
712int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
713 const s64 size, const s64 initialized_size)
714{
715 LCN lcn;
716 u8 *al = al_start;
717 u8 *al_end = al + initialized_size;
718 runlist_element *rl;
719 struct buffer_head *bh;
720 struct super_block *sb;
721 unsigned long block_size;
722 unsigned long block, max_block;
723 int err = 0;
724 unsigned char block_size_bits;
725
726 ntfs_debug("Entering.");
727 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
728 initialized_size > size)
729 return -EINVAL;
730 if (!initialized_size) {
731 memset(al, 0, size);
732 return 0;
733 }
734 sb = vol->sb;
735 block_size = sb->s_blocksize;
736 block_size_bits = sb->s_blocksize_bits;
737 down_read(&runlist->lock);
738 rl = runlist->rl;
Anton Altaparmakov2983d1b2005-09-08 20:56:09 +0100739 if (!rl) {
740 ntfs_error(sb, "Cannot read attribute list since runlist is "
741 "missing.");
742 goto err_out;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* Read all clusters specified by the runlist one run at a time. */
745 while (rl->length) {
746 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
747 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
748 (unsigned long long)rl->vcn,
749 (unsigned long long)lcn);
750 /* The attribute list cannot be sparse. */
751 if (lcn < 0) {
752 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
753 "read attribute list.");
754 goto err_out;
755 }
756 block = lcn << vol->cluster_size_bits >> block_size_bits;
757 /* Read the run from device in chunks of block_size bytes. */
758 max_block = block + (rl->length << vol->cluster_size_bits >>
759 block_size_bits);
760 ntfs_debug("max_block = 0x%lx.", max_block);
761 do {
762 ntfs_debug("Reading block = 0x%lx.", block);
763 bh = sb_bread(sb, block);
764 if (!bh) {
765 ntfs_error(sb, "sb_bread() failed. Cannot "
766 "read attribute list.");
767 goto err_out;
768 }
769 if (al + block_size >= al_end)
770 goto do_final;
771 memcpy(al, bh->b_data, block_size);
772 brelse(bh);
773 al += block_size;
774 } while (++block < max_block);
775 rl++;
776 }
777 if (initialized_size < size) {
778initialize:
779 memset(al_start + initialized_size, 0, size - initialized_size);
780 }
781done:
782 up_read(&runlist->lock);
783 return err;
784do_final:
785 if (al < al_end) {
786 /*
787 * Partial block.
788 *
789 * Note: The attribute list can be smaller than its allocation
790 * by multiple clusters. This has been encountered by at least
791 * two people running Windows XP, thus we cannot do any
792 * truncation sanity checking here. (AIA)
793 */
794 memcpy(al, bh->b_data, al_end - al);
795 brelse(bh);
796 if (initialized_size < size)
797 goto initialize;
798 goto done;
799 }
800 brelse(bh);
801 /* Real overflow! */
802 ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
803 "is truncated.");
804err_out:
805 err = -EIO;
806 goto done;
807}
808
809/**
810 * ntfs_external_attr_find - find an attribute in the attribute list of an inode
811 * @type: attribute type to find
812 * @name: attribute name to find (optional, i.e. NULL means don't care)
813 * @name_len: attribute name length (only needed if @name present)
814 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
815 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
816 * @val: attribute value to find (optional, resident attributes only)
817 * @val_len: attribute value length
818 * @ctx: search context with mft record and attribute to search from
819 *
820 * You should not need to call this function directly. Use ntfs_attr_lookup()
821 * instead.
822 *
823 * Find an attribute by searching the attribute list for the corresponding
824 * attribute list entry. Having found the entry, map the mft record if the
825 * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
826 * in there and return it.
827 *
828 * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
829 * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
830 * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
831 * then the base inode).
832 *
833 * After finishing with the attribute/mft record you need to call
834 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
835 * mapped inodes, etc).
836 *
837 * If the attribute is found, ntfs_external_attr_find() returns 0 and
838 * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
839 * mft record in which @ctx->attr is located and @ctx->al_entry will point to
840 * the attribute list entry for the attribute.
841 *
842 * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
843 * @ctx->attr will point to the attribute in the base mft record before which
844 * the attribute being searched for would need to be inserted if such an action
845 * were to be desired. @ctx->mrec will point to the mft record in which
846 * @ctx->attr is located and @ctx->al_entry will point to the attribute list
847 * entry of the attribute before which the attribute being searched for would
848 * need to be inserted if such an action were to be desired.
849 *
850 * Thus to insert the not found attribute, one wants to add the attribute to
851 * @ctx->mrec (the base mft record) and if there is not enough space, the
852 * attribute should be placed in a newly allocated extent mft record. The
853 * attribute list entry for the inserted attribute should be inserted in the
854 * attribute list attribute at @ctx->al_entry.
855 *
856 * On actual error, ntfs_external_attr_find() returns -EIO. In this case
857 * @ctx->attr is undefined and in particular do not rely on it not changing.
858 */
859static int ntfs_external_attr_find(const ATTR_TYPE type,
860 const ntfschar *name, const u32 name_len,
861 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
862 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
863{
864 ntfs_inode *base_ni, *ni;
865 ntfs_volume *vol;
866 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
867 u8 *al_start, *al_end;
868 ATTR_RECORD *a;
869 ntfschar *al_name;
870 u32 al_name_len;
871 int err = 0;
872 static const char *es = " Unmount and run chkdsk.";
873
874 ni = ctx->ntfs_ino;
875 base_ni = ctx->base_ntfs_ino;
876 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
877 if (!base_ni) {
878 /* First call happens with the base mft record. */
879 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
880 ctx->base_mrec = ctx->mrec;
881 }
882 if (ni == base_ni)
883 ctx->base_attr = ctx->attr;
884 if (type == AT_END)
885 goto not_found;
886 vol = base_ni->vol;
887 al_start = base_ni->attr_list;
888 al_end = al_start + base_ni->attr_list_size;
889 if (!ctx->al_entry)
890 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
891 /*
892 * Iterate over entries in attribute list starting at @ctx->al_entry,
893 * or the entry following that, if @ctx->is_first is TRUE.
894 */
895 if (ctx->is_first) {
896 al_entry = ctx->al_entry;
897 ctx->is_first = FALSE;
898 } else
899 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
900 le16_to_cpu(ctx->al_entry->length));
901 for (;; al_entry = next_al_entry) {
902 /* Out of bounds check. */
903 if ((u8*)al_entry < base_ni->attr_list ||
904 (u8*)al_entry > al_end)
905 break; /* Inode is corrupt. */
906 ctx->al_entry = al_entry;
907 /* Catch the end of the attribute list. */
908 if ((u8*)al_entry == al_end)
909 goto not_found;
910 if (!al_entry->length)
911 break;
912 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
913 le16_to_cpu(al_entry->length) > al_end)
914 break;
915 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
916 le16_to_cpu(al_entry->length));
917 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
918 goto not_found;
919 if (type != al_entry->type)
920 continue;
921 /*
922 * If @name is present, compare the two names. If @name is
923 * missing, assume we want an unnamed attribute.
924 */
925 al_name_len = al_entry->name_length;
926 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
927 if (!name) {
928 if (al_name_len)
929 goto not_found;
930 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
931 name_len, ic, vol->upcase, vol->upcase_len)) {
932 register int rc;
933
934 rc = ntfs_collate_names(name, name_len, al_name,
935 al_name_len, 1, IGNORE_CASE,
936 vol->upcase, vol->upcase_len);
937 /*
938 * If @name collates before al_name, there is no
939 * matching attribute.
940 */
941 if (rc == -1)
942 goto not_found;
943 /* If the strings are not equal, continue search. */
944 if (rc)
945 continue;
946 /*
947 * FIXME: Reverse engineering showed 0, IGNORE_CASE but
948 * that is inconsistent with ntfs_attr_find(). The
949 * subsequent rc checks were also different. Perhaps I
950 * made a mistake in one of the two. Need to recheck
951 * which is correct or at least see what is going on...
952 * (AIA)
953 */
954 rc = ntfs_collate_names(name, name_len, al_name,
955 al_name_len, 1, CASE_SENSITIVE,
956 vol->upcase, vol->upcase_len);
957 if (rc == -1)
958 goto not_found;
959 if (rc)
960 continue;
961 }
962 /*
963 * The names match or @name not present and attribute is
964 * unnamed. Now check @lowest_vcn. Continue search if the
965 * next attribute list entry still fits @lowest_vcn. Otherwise
966 * we have reached the right one or the search has failed.
967 */
968 if (lowest_vcn && (u8*)next_al_entry >= al_start &&
969 (u8*)next_al_entry + 6 < al_end &&
970 (u8*)next_al_entry + le16_to_cpu(
971 next_al_entry->length) <= al_end &&
972 sle64_to_cpu(next_al_entry->lowest_vcn) <=
973 lowest_vcn &&
974 next_al_entry->type == al_entry->type &&
975 next_al_entry->name_length == al_name_len &&
976 ntfs_are_names_equal((ntfschar*)((u8*)
977 next_al_entry +
978 next_al_entry->name_offset),
979 next_al_entry->name_length,
980 al_name, al_name_len, CASE_SENSITIVE,
981 vol->upcase, vol->upcase_len))
982 continue;
983 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
984 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
985 ntfs_error(vol->sb, "Found stale mft "
986 "reference in attribute list "
987 "of base inode 0x%lx.%s",
988 base_ni->mft_no, es);
989 err = -EIO;
990 break;
991 }
992 } else { /* Mft references do not match. */
993 /* If there is a mapped record unmap it first. */
994 if (ni != base_ni)
995 unmap_extent_mft_record(ni);
996 /* Do we want the base record back? */
997 if (MREF_LE(al_entry->mft_reference) ==
998 base_ni->mft_no) {
999 ni = ctx->ntfs_ino = base_ni;
1000 ctx->mrec = ctx->base_mrec;
1001 } else {
1002 /* We want an extent record. */
1003 ctx->mrec = map_extent_mft_record(base_ni,
1004 le64_to_cpu(
1005 al_entry->mft_reference), &ni);
1006 if (IS_ERR(ctx->mrec)) {
1007 ntfs_error(vol->sb, "Failed to map "
1008 "extent mft record "
1009 "0x%lx of base inode "
1010 "0x%lx.%s",
1011 MREF_LE(al_entry->
1012 mft_reference),
1013 base_ni->mft_no, es);
1014 err = PTR_ERR(ctx->mrec);
1015 if (err == -ENOENT)
1016 err = -EIO;
1017 /* Cause @ctx to be sanitized below. */
1018 ni = NULL;
1019 break;
1020 }
1021 ctx->ntfs_ino = ni;
1022 }
1023 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1024 le16_to_cpu(ctx->mrec->attrs_offset));
1025 }
1026 /*
1027 * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
1028 * mft record containing the attribute represented by the
1029 * current al_entry.
1030 */
1031 /*
1032 * We could call into ntfs_attr_find() to find the right
1033 * attribute in this mft record but this would be less
1034 * efficient and not quite accurate as ntfs_attr_find() ignores
1035 * the attribute instance numbers for example which become
1036 * important when one plays with attribute lists. Also,
1037 * because a proper match has been found in the attribute list
1038 * entry above, the comparison can now be optimized. So it is
1039 * worth re-implementing a simplified ntfs_attr_find() here.
1040 */
1041 a = ctx->attr;
1042 /*
1043 * Use a manual loop so we can still use break and continue
1044 * with the same meanings as above.
1045 */
1046do_next_attr_loop:
1047 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1048 le32_to_cpu(ctx->mrec->bytes_allocated))
1049 break;
1050 if (a->type == AT_END)
1051 continue;
1052 if (!a->length)
1053 break;
1054 if (al_entry->instance != a->instance)
1055 goto do_next_attr;
1056 /*
1057 * If the type and/or the name are mismatched between the
1058 * attribute list entry and the attribute record, there is
1059 * corruption so we break and return error EIO.
1060 */
1061 if (al_entry->type != a->type)
1062 break;
1063 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1064 le16_to_cpu(a->name_offset)), a->name_length,
1065 al_name, al_name_len, CASE_SENSITIVE,
1066 vol->upcase, vol->upcase_len))
1067 break;
1068 ctx->attr = a;
1069 /*
1070 * If no @val specified or @val specified and it matches, we
1071 * have found it!
1072 */
1073 if (!val || (!a->non_resident && le32_to_cpu(
1074 a->data.resident.value_length) == val_len &&
1075 !memcmp((u8*)a +
1076 le16_to_cpu(a->data.resident.value_offset),
1077 val, val_len))) {
1078 ntfs_debug("Done, found.");
1079 return 0;
1080 }
1081do_next_attr:
1082 /* Proceed to the next attribute in the current mft record. */
1083 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1084 goto do_next_attr_loop;
1085 }
1086 if (!err) {
1087 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1088 "attribute list attribute.%s", base_ni->mft_no,
1089 es);
1090 err = -EIO;
1091 }
1092 if (ni != base_ni) {
1093 if (ni)
1094 unmap_extent_mft_record(ni);
1095 ctx->ntfs_ino = base_ni;
1096 ctx->mrec = ctx->base_mrec;
1097 ctx->attr = ctx->base_attr;
1098 }
1099 if (err != -ENOMEM)
1100 NVolSetErrors(vol);
1101 return err;
1102not_found:
1103 /*
1104 * If we were looking for AT_END, we reset the search context @ctx and
1105 * use ntfs_attr_find() to seek to the end of the base mft record.
1106 */
1107 if (type == AT_END) {
1108 ntfs_attr_reinit_search_ctx(ctx);
1109 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1110 ctx);
1111 }
1112 /*
1113 * The attribute was not found. Before we return, we want to ensure
1114 * @ctx->mrec and @ctx->attr indicate the position at which the
1115 * attribute should be inserted in the base mft record. Since we also
1116 * want to preserve @ctx->al_entry we cannot reinitialize the search
1117 * context using ntfs_attr_reinit_search_ctx() as this would set
1118 * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
1119 * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
1120 * @ctx->al_entry as the remaining fields (base_*) are identical to
1121 * their non base_ counterparts and we cannot set @ctx->base_attr
1122 * correctly yet as we do not know what @ctx->attr will be set to by
1123 * the call to ntfs_attr_find() below.
1124 */
1125 if (ni != base_ni)
1126 unmap_extent_mft_record(ni);
1127 ctx->mrec = ctx->base_mrec;
1128 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1129 le16_to_cpu(ctx->mrec->attrs_offset));
1130 ctx->is_first = TRUE;
1131 ctx->ntfs_ino = base_ni;
1132 ctx->base_ntfs_ino = NULL;
1133 ctx->base_mrec = NULL;
1134 ctx->base_attr = NULL;
1135 /*
1136 * In case there are multiple matches in the base mft record, need to
1137 * keep enumerating until we get an attribute not found response (or
1138 * another error), otherwise we would keep returning the same attribute
1139 * over and over again and all programs using us for enumeration would
1140 * lock up in a tight loop.
1141 */
1142 do {
1143 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1144 ctx);
1145 } while (!err);
1146 ntfs_debug("Done, not found.");
1147 return err;
1148}
1149
1150/**
1151 * ntfs_attr_lookup - find an attribute in an ntfs inode
1152 * @type: attribute type to find
1153 * @name: attribute name to find (optional, i.e. NULL means don't care)
1154 * @name_len: attribute name length (only needed if @name present)
1155 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1156 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
1157 * @val: attribute value to find (optional, resident attributes only)
1158 * @val_len: attribute value length
1159 * @ctx: search context with mft record and attribute to search from
1160 *
1161 * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
1162 * be the base mft record and @ctx must have been obtained from a call to
1163 * ntfs_attr_get_search_ctx().
1164 *
1165 * This function transparently handles attribute lists and @ctx is used to
1166 * continue searches where they were left off at.
1167 *
1168 * After finishing with the attribute/mft record you need to call
1169 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1170 * mapped inodes, etc).
1171 *
1172 * Return 0 if the search was successful and -errno if not.
1173 *
1174 * When 0, @ctx->attr is the found attribute and it is in mft record
1175 * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
1176 * the attribute list entry of the found attribute.
1177 *
1178 * When -ENOENT, @ctx->attr is the attribute which collates just after the
1179 * attribute being searched for, i.e. if one wants to add the attribute to the
1180 * mft record this is the correct place to insert it into. If an attribute
1181 * list attribute is present, @ctx->al_entry is the attribute list entry which
1182 * collates just after the attribute list entry of the attribute being searched
1183 * for, i.e. if one wants to add the attribute to the mft record this is the
1184 * correct place to insert its attribute list entry into.
1185 *
1186 * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is
1187 * then undefined and in particular you should not rely on it not changing.
1188 */
1189int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1190 const u32 name_len, const IGNORE_CASE_BOOL ic,
1191 const VCN lowest_vcn, const u8 *val, const u32 val_len,
1192 ntfs_attr_search_ctx *ctx)
1193{
1194 ntfs_inode *base_ni;
1195
1196 ntfs_debug("Entering.");
Anton Altaparmakov69b41e32005-10-04 14:01:14 +01001197 BUG_ON(IS_ERR(ctx->mrec));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (ctx->base_ntfs_ino)
1199 base_ni = ctx->base_ntfs_ino;
1200 else
1201 base_ni = ctx->ntfs_ino;
1202 /* Sanity check, just for debugging really. */
1203 BUG_ON(!base_ni);
1204 if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1205 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1206 ctx);
1207 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1208 val, val_len, ctx);
1209}
1210
1211/**
1212 * ntfs_attr_init_search_ctx - initialize an attribute search context
1213 * @ctx: attribute search context to initialize
1214 * @ni: ntfs inode with which to initialize the search context
1215 * @mrec: mft record with which to initialize the search context
1216 *
1217 * Initialize the attribute search context @ctx with @ni and @mrec.
1218 */
1219static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1220 ntfs_inode *ni, MFT_RECORD *mrec)
1221{
Anton Altaparmakov442d2072005-05-27 16:42:56 +01001222 *ctx = (ntfs_attr_search_ctx) {
1223 .mrec = mrec,
1224 /* Sanity checks are performed elsewhere. */
1225 .attr = (ATTR_RECORD*)((u8*)mrec +
1226 le16_to_cpu(mrec->attrs_offset)),
1227 .is_first = TRUE,
1228 .ntfs_ino = ni,
1229 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232/**
1233 * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1234 * @ctx: attribute search context to reinitialize
1235 *
1236 * Reinitialize the attribute search context @ctx, unmapping an associated
1237 * extent mft record if present, and initialize the search context again.
1238 *
1239 * This is used when a search for a new attribute is being started to reset
1240 * the search context to the beginning.
1241 */
1242void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1243{
1244 if (likely(!ctx->base_ntfs_ino)) {
1245 /* No attribute list. */
1246 ctx->is_first = TRUE;
1247 /* Sanity checks are performed elsewhere. */
1248 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1249 le16_to_cpu(ctx->mrec->attrs_offset));
1250 /*
1251 * This needs resetting due to ntfs_external_attr_find() which
1252 * can leave it set despite having zeroed ctx->base_ntfs_ino.
1253 */
1254 ctx->al_entry = NULL;
1255 return;
1256 } /* Attribute list. */
1257 if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1258 unmap_extent_mft_record(ctx->ntfs_ino);
1259 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1260 return;
1261}
1262
1263/**
1264 * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1265 * @ni: ntfs inode with which to initialize the search context
1266 * @mrec: mft record with which to initialize the search context
1267 *
1268 * Allocate a new attribute search context, initialize it with @ni and @mrec,
1269 * and return it. Return NULL if allocation failed.
1270 */
1271ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1272{
1273 ntfs_attr_search_ctx *ctx;
1274
1275 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
1276 if (ctx)
1277 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1278 return ctx;
1279}
1280
1281/**
1282 * ntfs_attr_put_search_ctx - release an attribute search context
1283 * @ctx: attribute search context to free
1284 *
1285 * Release the attribute search context @ctx, unmapping an associated extent
1286 * mft record if present.
1287 */
1288void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1289{
1290 if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1291 unmap_extent_mft_record(ctx->ntfs_ino);
1292 kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1293 return;
1294}
1295
Anton Altaparmakov53d59aa2005-03-17 10:51:33 +00001296#ifdef NTFS_RW
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298/**
1299 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
1300 * @vol: ntfs volume to which the attribute belongs
1301 * @type: attribute type which to find
1302 *
1303 * Search for the attribute definition record corresponding to the attribute
1304 * @type in the $AttrDef system file.
1305 *
1306 * Return the attribute type definition record if found and NULL if not found.
1307 */
1308static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1309 const ATTR_TYPE type)
1310{
1311 ATTR_DEF *ad;
1312
1313 BUG_ON(!vol->attrdef);
1314 BUG_ON(!type);
1315 for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1316 vol->attrdef_size && ad->type; ++ad) {
1317 /* We have not found it yet, carry on searching. */
1318 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1319 continue;
1320 /* We found the attribute; return it. */
1321 if (likely(ad->type == type))
1322 return ad;
1323 /* We have gone too far already. No point in continuing. */
1324 break;
1325 }
1326 /* Attribute not found. */
1327 ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1328 le32_to_cpu(type));
1329 return NULL;
1330}
1331
1332/**
1333 * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
1334 * @vol: ntfs volume to which the attribute belongs
1335 * @type: attribute type which to check
1336 * @size: size which to check
1337 *
1338 * Check whether the @size in bytes is valid for an attribute of @type on the
1339 * ntfs volume @vol. This information is obtained from $AttrDef system file.
1340 *
1341 * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
1342 * listed in $AttrDef.
1343 */
1344int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1345 const s64 size)
1346{
1347 ATTR_DEF *ad;
1348
1349 BUG_ON(size < 0);
1350 /*
1351 * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
1352 * listed in $AttrDef.
1353 */
1354 if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1355 return -ERANGE;
1356 /* Get the $AttrDef entry for the attribute @type. */
1357 ad = ntfs_attr_find_in_attrdef(vol, type);
1358 if (unlikely(!ad))
1359 return -ENOENT;
1360 /* Do the bounds check. */
1361 if (((sle64_to_cpu(ad->min_size) > 0) &&
1362 size < sle64_to_cpu(ad->min_size)) ||
1363 ((sle64_to_cpu(ad->max_size) > 0) && size >
1364 sle64_to_cpu(ad->max_size)))
1365 return -ERANGE;
1366 return 0;
1367}
1368
1369/**
1370 * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
1371 * @vol: ntfs volume to which the attribute belongs
1372 * @type: attribute type which to check
1373 *
1374 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1375 * be non-resident. This information is obtained from $AttrDef system file.
1376 *
Anton Altaparmakovbb3cf332005-04-06 13:34:31 +01001377 * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * -ENOENT if the attribute is not listed in $AttrDef.
1379 */
1380int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1381{
1382 ATTR_DEF *ad;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* Find the attribute definition record in $AttrDef. */
1385 ad = ntfs_attr_find_in_attrdef(vol, type);
1386 if (unlikely(!ad))
1387 return -ENOENT;
1388 /* Check the flags and return the result. */
Anton Altaparmakovbb3cf332005-04-06 13:34:31 +01001389 if (ad->flags & ATTR_DEF_RESIDENT)
1390 return -EPERM;
1391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
1394/**
1395 * ntfs_attr_can_be_resident - check if an attribute can be resident
1396 * @vol: ntfs volume to which the attribute belongs
1397 * @type: attribute type which to check
1398 *
1399 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1400 * be resident. This information is derived from our ntfs knowledge and may
1401 * not be completely accurate, especially when user defined attributes are
1402 * present. Basically we allow everything to be resident except for index
1403 * allocation and $EA attributes.
1404 *
1405 * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
1406 *
1407 * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
1408 * otherwise windows will not boot (blue screen of death)! We cannot
1409 * check for this here as we do not know which inode's $Bitmap is
1410 * being asked about so the caller needs to special case this.
1411 */
1412int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1413{
Anton Altaparmakov7d0ffdb2005-10-19 12:21:19 +01001414 if (type == AT_INDEX_ALLOCATION)
Anton Altaparmakovbb3cf332005-04-06 13:34:31 +01001415 return -EPERM;
1416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/**
1420 * ntfs_attr_record_resize - resize an attribute record
1421 * @m: mft record containing attribute record
1422 * @a: attribute record to resize
1423 * @new_size: new size in bytes to which to resize the attribute record @a
1424 *
1425 * Resize the attribute record @a, i.e. the resident part of the attribute, in
1426 * the mft record @m to @new_size bytes.
1427 *
1428 * Return 0 on success and -errno on error. The following error codes are
1429 * defined:
1430 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1431 *
1432 * Note: On error, no modifications have been performed whatsoever.
1433 *
1434 * Warning: If you make a record smaller without having copied all the data you
1435 * are interested in the data may be overwritten.
1436 */
1437int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1438{
1439 ntfs_debug("Entering for new_size %u.", new_size);
1440 /* Align to 8 bytes if it is not already done. */
1441 if (new_size & 7)
1442 new_size = (new_size + 7) & ~7;
1443 /* If the actual attribute length has changed, move things around. */
1444 if (new_size != le32_to_cpu(a->length)) {
1445 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1446 le32_to_cpu(a->length) + new_size;
1447 /* Not enough space in this mft record. */
1448 if (new_muse > le32_to_cpu(m->bytes_allocated))
1449 return -ENOSPC;
1450 /* Move attributes following @a to their new location. */
1451 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1452 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1453 (u8*)m) - le32_to_cpu(a->length));
1454 /* Adjust @m to reflect the change in used space. */
1455 m->bytes_in_use = cpu_to_le32(new_muse);
1456 /* Adjust @a to reflect the new size. */
1457 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1458 a->length = cpu_to_le32(new_size);
1459 }
1460 return 0;
1461}
1462
1463/**
Anton Altaparmakov0aaccea2005-09-08 20:40:32 +01001464 * ntfs_resident_attr_value_resize - resize the value of a resident attribute
1465 * @m: mft record containing attribute record
1466 * @a: attribute record whose value to resize
1467 * @new_size: new size in bytes to which to resize the attribute value of @a
1468 *
1469 * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
1470 * If the value is made bigger, the newly allocated space is cleared.
1471 *
1472 * Return 0 on success and -errno on error. The following error codes are
1473 * defined:
1474 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1475 *
1476 * Note: On error, no modifications have been performed whatsoever.
1477 *
1478 * Warning: If you make a record smaller without having copied all the data you
1479 * are interested in the data may be overwritten.
1480 */
1481int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1482 const u32 new_size)
1483{
1484 u32 old_size;
1485
1486 /* Resize the resident part of the attribute record. */
1487 if (ntfs_attr_record_resize(m, a,
1488 le16_to_cpu(a->data.resident.value_offset) + new_size))
1489 return -ENOSPC;
1490 /*
1491 * The resize succeeded! If we made the attribute value bigger, clear
1492 * the area between the old size and @new_size.
1493 */
1494 old_size = le32_to_cpu(a->data.resident.value_length);
1495 if (new_size > old_size)
1496 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1497 old_size, 0, new_size - old_size);
1498 /* Finally update the length of the attribute value. */
1499 a->data.resident.value_length = cpu_to_le32(new_size);
1500 return 0;
1501}
1502
1503/**
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001504 * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1505 * @ni: ntfs inode describing the attribute to convert
Anton Altaparmakov8925d4f2005-10-04 14:48:20 +01001506 * @data_size: size of the resident data to copy to the non-resident attribute
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001507 *
1508 * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1509 * non-resident one.
1510 *
Anton Altaparmakov8925d4f2005-10-04 14:48:20 +01001511 * @data_size must be equal to the attribute value size. This is needed since
1512 * we need to know the size before we can map the mft record and our callers
1513 * always know it. The reason we cannot simply read the size from the vfs
1514 * inode i_size is that this is not necessarily uptodate. This happens when
1515 * ntfs_attr_make_non_resident() is called in the ->truncate call path(s).
1516 *
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001517 * Return 0 on success and -errno on error. The following error return codes
1518 * are defined:
1519 * -EPERM - The attribute is not allowed to be non-resident.
1520 * -ENOMEM - Not enough memory.
1521 * -ENOSPC - Not enough disk space.
1522 * -EINVAL - Attribute not defined on the volume.
1523 * -EIO - I/o error or other error.
Anton Altaparmakov53d59aa2005-03-17 10:51:33 +00001524 * Note that -ENOSPC is also returned in the case that there is not enough
1525 * space in the mft record to do the conversion. This can happen when the mft
1526 * record is already very full. The caller is responsible for trying to make
1527 * space in the mft record and trying again. FIXME: Do we need a separate
1528 * error return code for this kind of -ENOSPC or is it always worth trying
1529 * again in case the attribute may then fit in a resident state so no need to
1530 * make it non-resident at all? Ho-hum... (AIA)
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001531 *
1532 * NOTE to self: No changes in the attribute list are required to move from
1533 * a resident to a non-resident attribute.
1534 *
1535 * Locking: - The caller must hold i_sem on the inode.
1536 */
Anton Altaparmakov8925d4f2005-10-04 14:48:20 +01001537int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001538{
1539 s64 new_size;
1540 struct inode *vi = VFS_I(ni);
1541 ntfs_volume *vol = ni->vol;
1542 ntfs_inode *base_ni;
1543 MFT_RECORD *m;
1544 ATTR_RECORD *a;
1545 ntfs_attr_search_ctx *ctx;
1546 struct page *page;
1547 runlist_element *rl;
1548 u8 *kaddr;
1549 unsigned long flags;
1550 int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1551 u32 attr_size;
1552 u8 old_res_attr_flags;
1553
1554 /* Check that the attribute is allowed to be non-resident. */
1555 err = ntfs_attr_can_be_non_resident(vol, ni->type);
1556 if (unlikely(err)) {
1557 if (err == -EPERM)
1558 ntfs_debug("Attribute is not allowed to be "
1559 "non-resident.");
1560 else
1561 ntfs_debug("Attribute not defined on the NTFS "
1562 "volume!");
1563 return err;
1564 }
1565 /*
Anton Altaparmakov807c4532005-09-08 21:01:17 +01001566 * FIXME: Compressed and encrypted attributes are not supported when
1567 * writing and we should never have gotten here for them.
1568 */
1569 BUG_ON(NInoCompressed(ni));
1570 BUG_ON(NInoEncrypted(ni));
1571 /*
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001572 * The size needs to be aligned to a cluster boundary for allocation
1573 * purposes.
1574 */
Anton Altaparmakov8925d4f2005-10-04 14:48:20 +01001575 new_size = (data_size + vol->cluster_size - 1) &
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001576 ~(vol->cluster_size - 1);
1577 if (new_size > 0) {
1578 /*
1579 * Will need the page later and since the page lock nests
1580 * outside all ntfs locks, we need to get the page now.
1581 */
1582 page = find_or_create_page(vi->i_mapping, 0,
1583 mapping_gfp_mask(vi->i_mapping));
1584 if (unlikely(!page))
1585 return -ENOMEM;
1586 /* Start by allocating clusters to hold the attribute value. */
1587 rl = ntfs_cluster_alloc(vol, 0, new_size >>
Anton Altaparmakovfc0fa7d2005-10-04 14:36:56 +01001588 vol->cluster_size_bits, -1, DATA_ZONE, TRUE);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001589 if (IS_ERR(rl)) {
1590 err = PTR_ERR(rl);
1591 ntfs_debug("Failed to allocate cluster%s, error code "
Anton Altaparmakovaf859a42005-06-25 21:07:27 +01001592 "%i.", (new_size >>
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001593 vol->cluster_size_bits) > 1 ? "s" : "",
1594 err);
1595 goto page_err_out;
1596 }
1597 } else {
1598 rl = NULL;
1599 page = NULL;
1600 }
1601 /* Determine the size of the mapping pairs array. */
Anton Altaparmakovfa3be922005-06-25 17:15:36 +01001602 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001603 if (unlikely(mp_size < 0)) {
1604 err = mp_size;
1605 ntfs_debug("Failed to get size for mapping pairs array, error "
1606 "code %i.", err);
1607 goto rl_err_out;
1608 }
1609 down_write(&ni->runlist.lock);
1610 if (!NInoAttr(ni))
1611 base_ni = ni;
1612 else
1613 base_ni = ni->ext.base_ntfs_ino;
1614 m = map_mft_record(base_ni);
1615 if (IS_ERR(m)) {
1616 err = PTR_ERR(m);
1617 m = NULL;
1618 ctx = NULL;
1619 goto err_out;
1620 }
1621 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1622 if (unlikely(!ctx)) {
1623 err = -ENOMEM;
1624 goto err_out;
1625 }
1626 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1627 CASE_SENSITIVE, 0, NULL, 0, ctx);
1628 if (unlikely(err)) {
1629 if (err == -ENOENT)
1630 err = -EIO;
1631 goto err_out;
1632 }
1633 m = ctx->mrec;
1634 a = ctx->attr;
1635 BUG_ON(NInoNonResident(ni));
1636 BUG_ON(a->non_resident);
1637 /*
1638 * Calculate new offsets for the name and the mapping pairs array.
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001639 */
Anton Altaparmakov807c4532005-09-08 21:01:17 +01001640 if (NInoSparse(ni) || NInoCompressed(ni))
1641 name_ofs = (offsetof(ATTR_REC,
1642 data.non_resident.compressed_size) +
1643 sizeof(a->data.non_resident.compressed_size) +
1644 7) & ~7;
1645 else
1646 name_ofs = (offsetof(ATTR_REC,
1647 data.non_resident.compressed_size) + 7) & ~7;
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001648 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1649 /*
1650 * Determine the size of the resident part of the now non-resident
1651 * attribute record.
1652 */
1653 arec_size = (mp_ofs + mp_size + 7) & ~7;
1654 /*
1655 * If the page is not uptodate bring it uptodate by copying from the
1656 * attribute value.
1657 */
1658 attr_size = le32_to_cpu(a->data.resident.value_length);
Anton Altaparmakov8925d4f2005-10-04 14:48:20 +01001659 BUG_ON(attr_size != data_size);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001660 if (page && !PageUptodate(page)) {
1661 kaddr = kmap_atomic(page, KM_USER0);
1662 memcpy(kaddr, (u8*)a +
1663 le16_to_cpu(a->data.resident.value_offset),
1664 attr_size);
1665 memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1666 kunmap_atomic(kaddr, KM_USER0);
1667 flush_dcache_page(page);
1668 SetPageUptodate(page);
1669 }
1670 /* Backup the attribute flag. */
1671 old_res_attr_flags = a->data.resident.flags;
1672 /* Resize the resident part of the attribute record. */
1673 err = ntfs_attr_record_resize(m, a, arec_size);
1674 if (unlikely(err))
1675 goto err_out;
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001676 /*
1677 * Convert the resident part of the attribute record to describe a
1678 * non-resident attribute.
1679 */
1680 a->non_resident = 1;
1681 /* Move the attribute name if it exists and update the offset. */
1682 if (a->name_length)
1683 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1684 a->name_length * sizeof(ntfschar));
1685 a->name_offset = cpu_to_le16(name_ofs);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001686 /* Setup the fields specific to non-resident attributes. */
1687 a->data.non_resident.lowest_vcn = 0;
1688 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1689 vol->cluster_size_bits);
1690 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001691 memset(&a->data.non_resident.reserved, 0,
1692 sizeof(a->data.non_resident.reserved));
1693 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1694 a->data.non_resident.data_size =
1695 a->data.non_resident.initialized_size =
1696 cpu_to_sle64(attr_size);
Anton Altaparmakov807c4532005-09-08 21:01:17 +01001697 if (NInoSparse(ni) || NInoCompressed(ni)) {
1698 a->data.non_resident.compression_unit = 4;
1699 a->data.non_resident.compressed_size =
1700 a->data.non_resident.allocated_size;
1701 } else
1702 a->data.non_resident.compression_unit = 0;
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001703 /* Generate the mapping pairs array into the attribute record. */
1704 err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
Anton Altaparmakovfa3be922005-06-25 17:15:36 +01001705 arec_size - mp_ofs, rl, 0, -1, NULL);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001706 if (unlikely(err)) {
1707 ntfs_debug("Failed to build mapping pairs, error code %i.",
1708 err);
1709 goto undo_err_out;
1710 }
Anton Altaparmakov905685f2005-03-10 11:06:19 +00001711 /* Setup the in-memory attribute structure to be non-resident. */
Anton Altaparmakov905685f2005-03-10 11:06:19 +00001712 ni->runlist.rl = rl;
1713 write_lock_irqsave(&ni->size_lock, flags);
1714 ni->allocated_size = new_size;
Anton Altaparmakov807c4532005-09-08 21:01:17 +01001715 if (NInoSparse(ni) || NInoCompressed(ni)) {
1716 ni->itype.compressed.size = ni->allocated_size;
1717 ni->itype.compressed.block_size = 1U <<
1718 (a->data.non_resident.compression_unit +
1719 vol->cluster_size_bits);
1720 ni->itype.compressed.block_size_bits =
1721 ffs(ni->itype.compressed.block_size) - 1;
1722 ni->itype.compressed.block_clusters = 1U <<
1723 a->data.non_resident.compression_unit;
Anton Altaparmakov2a6fc4e12005-10-04 14:57:15 +01001724 vi->i_blocks = ni->itype.compressed.size >> 9;
1725 } else
1726 vi->i_blocks = ni->allocated_size >> 9;
Anton Altaparmakov905685f2005-03-10 11:06:19 +00001727 write_unlock_irqrestore(&ni->size_lock, flags);
1728 /*
1729 * This needs to be last since the address space operations ->readpage
1730 * and ->writepage can run concurrently with us as they are not
1731 * serialized on i_sem. Note, we are not allowed to fail once we flip
1732 * this switch, which is another reason to do this last.
1733 */
1734 NInoSetNonResident(ni);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001735 /* Mark the mft record dirty, so it gets written back. */
1736 flush_dcache_mft_record_page(ctx->ntfs_ino);
1737 mark_mft_record_dirty(ctx->ntfs_ino);
1738 ntfs_attr_put_search_ctx(ctx);
1739 unmap_mft_record(base_ni);
1740 up_write(&ni->runlist.lock);
1741 if (page) {
1742 set_page_dirty(page);
1743 unlock_page(page);
Anton Altaparmakov905685f2005-03-10 11:06:19 +00001744 mark_page_accessed(page);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001745 page_cache_release(page);
1746 }
1747 ntfs_debug("Done.");
1748 return 0;
1749undo_err_out:
1750 /* Convert the attribute back into a resident attribute. */
1751 a->non_resident = 0;
1752 /* Move the attribute name if it exists and update the offset. */
1753 name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1754 sizeof(a->data.resident.reserved) + 7) & ~7;
1755 if (a->name_length)
1756 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1757 a->name_length * sizeof(ntfschar));
1758 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1759 a->name_offset = cpu_to_le16(name_ofs);
1760 arec_size = (mp_ofs + attr_size + 7) & ~7;
1761 /* Resize the resident part of the attribute record. */
1762 err2 = ntfs_attr_record_resize(m, a, arec_size);
1763 if (unlikely(err2)) {
1764 /*
1765 * This cannot happen (well if memory corruption is at work it
1766 * could happen in theory), but deal with it as well as we can.
1767 * If the old size is too small, truncate the attribute,
1768 * otherwise simply give it a larger allocated size.
1769 * FIXME: Should check whether chkdsk complains when the
1770 * allocated size is much bigger than the resident value size.
1771 */
1772 arec_size = le32_to_cpu(a->length);
1773 if ((mp_ofs + attr_size) > arec_size) {
1774 err2 = attr_size;
1775 attr_size = arec_size - mp_ofs;
1776 ntfs_error(vol->sb, "Failed to undo partial resident "
1777 "to non-resident attribute "
1778 "conversion. Truncating inode 0x%lx, "
1779 "attribute type 0x%x from %i bytes to "
1780 "%i bytes to maintain metadata "
1781 "consistency. THIS MEANS YOU ARE "
1782 "LOSING %i BYTES DATA FROM THIS %s.",
1783 vi->i_ino,
1784 (unsigned)le32_to_cpu(ni->type),
1785 err2, attr_size, err2 - attr_size,
1786 ((ni->type == AT_DATA) &&
1787 !ni->name_len) ? "FILE": "ATTRIBUTE");
1788 write_lock_irqsave(&ni->size_lock, flags);
1789 ni->initialized_size = attr_size;
1790 i_size_write(vi, attr_size);
1791 write_unlock_irqrestore(&ni->size_lock, flags);
1792 }
1793 }
1794 /* Setup the fields specific to resident attributes. */
1795 a->data.resident.value_length = cpu_to_le32(attr_size);
1796 a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1797 a->data.resident.flags = old_res_attr_flags;
1798 memset(&a->data.resident.reserved, 0,
1799 sizeof(a->data.resident.reserved));
1800 /* Copy the data from the page back to the attribute value. */
1801 if (page) {
1802 kaddr = kmap_atomic(page, KM_USER0);
1803 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1804 kunmap_atomic(kaddr, KM_USER0);
1805 }
Anton Altaparmakov905685f2005-03-10 11:06:19 +00001806 /* Setup the allocated size in the ntfs inode in case it changed. */
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001807 write_lock_irqsave(&ni->size_lock, flags);
1808 ni->allocated_size = arec_size - mp_ofs;
1809 write_unlock_irqrestore(&ni->size_lock, flags);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001810 /* Mark the mft record dirty, so it gets written back. */
1811 flush_dcache_mft_record_page(ctx->ntfs_ino);
1812 mark_mft_record_dirty(ctx->ntfs_ino);
1813err_out:
1814 if (ctx)
1815 ntfs_attr_put_search_ctx(ctx);
1816 if (m)
1817 unmap_mft_record(base_ni);
1818 ni->runlist.rl = NULL;
1819 up_write(&ni->runlist.lock);
1820rl_err_out:
1821 if (rl) {
1822 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001823 ntfs_error(vol->sb, "Failed to release allocated "
1824 "cluster(s) in error code path. Run "
1825 "chkdsk to recover the lost "
1826 "cluster(s).");
1827 NVolSetErrors(vol);
1828 }
Anton Altaparmakov53d59aa2005-03-17 10:51:33 +00001829 ntfs_free(rl);
Anton Altaparmakov2bfb4ff2005-03-09 15:15:06 +00001830page_err_out:
1831 unlock_page(page);
1832 page_cache_release(page);
1833 }
1834 if (err == -EINVAL)
1835 err = -EIO;
1836 return err;
1837}
1838
1839/**
Anton Altaparmakov2d868292005-10-04 15:18:56 +01001840 * ntfs_attr_extend_allocation - extend the allocated space of an attribute
1841 * @ni: ntfs inode of the attribute whose allocation to extend
1842 * @new_alloc_size: new size in bytes to which to extend the allocation to
1843 * @new_data_size: new size in bytes to which to extend the data to
1844 * @data_start: beginning of region which is required to be non-sparse
1845 *
1846 * Extend the allocated space of an attribute described by the ntfs inode @ni
1847 * to @new_alloc_size bytes. If @data_start is -1, the whole extension may be
1848 * implemented as a hole in the file (as long as both the volume and the ntfs
1849 * inode @ni have sparse support enabled). If @data_start is >= 0, then the
1850 * region between the old allocated size and @data_start - 1 may be made sparse
1851 * but the regions between @data_start and @new_alloc_size must be backed by
1852 * actual clusters.
1853 *
1854 * If @new_data_size is -1, it is ignored. If it is >= 0, then the data size
1855 * of the attribute is extended to @new_data_size. Note that the i_size of the
1856 * vfs inode is not updated. Only the data size in the base attribute record
1857 * is updated. The caller has to update i_size separately if this is required.
1858 * WARNING: It is a BUG() for @new_data_size to be smaller than the old data
1859 * size as well as for @new_data_size to be greater than @new_alloc_size.
1860 *
1861 * For resident attributes this involves resizing the attribute record and if
1862 * necessary moving it and/or other attributes into extent mft records and/or
1863 * converting the attribute to a non-resident attribute which in turn involves
1864 * extending the allocation of a non-resident attribute as described below.
1865 *
1866 * For non-resident attributes this involves allocating clusters in the data
1867 * zone on the volume (except for regions that are being made sparse) and
1868 * extending the run list to describe the allocated clusters as well as
1869 * updating the mapping pairs array of the attribute. This in turn involves
1870 * resizing the attribute record and if necessary moving it and/or other
1871 * attributes into extent mft records and/or splitting the attribute record
1872 * into multiple extent attribute records.
1873 *
1874 * Also, the attribute list attribute is updated if present and in some of the
1875 * above cases (the ones where extent mft records/attributes come into play),
1876 * an attribute list attribute is created if not already present.
1877 *
1878 * Return the new allocated size on success and -errno on error. In the case
1879 * that an error is encountered but a partial extension at least up to
1880 * @data_start (if present) is possible, the allocation is partially extended
1881 * and this is returned. This means the caller must check the returned size to
1882 * determine if the extension was partial. If @data_start is -1 then partial
1883 * allocations are not performed.
1884 *
1885 * WARNING: Do not call ntfs_attr_extend_allocation() for $MFT/$DATA.
1886 *
1887 * Locking: This function takes the runlist lock of @ni for writing as well as
1888 * locking the mft record of the base ntfs inode. These locks are maintained
1889 * throughout execution of the function. These locks are required so that the
1890 * attribute can be resized safely and so that it can for example be converted
1891 * from resident to non-resident safely.
1892 *
1893 * TODO: At present attribute list attribute handling is not implemented.
1894 *
1895 * TODO: At present it is not safe to call this function for anything other
1896 * than the $DATA attribute(s) of an uncompressed and unencrypted file.
1897 */
1898s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size,
1899 const s64 new_data_size, const s64 data_start)
1900{
1901 VCN vcn;
1902 s64 ll, allocated_size, start = data_start;
1903 struct inode *vi = VFS_I(ni);
1904 ntfs_volume *vol = ni->vol;
1905 ntfs_inode *base_ni;
1906 MFT_RECORD *m;
1907 ATTR_RECORD *a;
1908 ntfs_attr_search_ctx *ctx;
1909 runlist_element *rl, *rl2;
1910 unsigned long flags;
1911 int err, mp_size;
1912 u32 attr_len = 0; /* Silence stupid gcc warning. */
1913 BOOL mp_rebuilt;
1914
1915#ifdef NTFS_DEBUG
1916 read_lock_irqsave(&ni->size_lock, flags);
1917 allocated_size = ni->allocated_size;
1918 read_unlock_irqrestore(&ni->size_lock, flags);
1919 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
1920 "old_allocated_size 0x%llx, "
1921 "new_allocated_size 0x%llx, new_data_size 0x%llx, "
1922 "data_start 0x%llx.", vi->i_ino,
1923 (unsigned)le32_to_cpu(ni->type),
1924 (unsigned long long)allocated_size,
1925 (unsigned long long)new_alloc_size,
1926 (unsigned long long)new_data_size,
1927 (unsigned long long)start);
1928#endif
1929retry_extend:
1930 /*
1931 * For non-resident attributes, @start and @new_size need to be aligned
1932 * to cluster boundaries for allocation purposes.
1933 */
1934 if (NInoNonResident(ni)) {
1935 if (start > 0)
1936 start &= ~(s64)vol->cluster_size_mask;
1937 new_alloc_size = (new_alloc_size + vol->cluster_size - 1) &
1938 ~(s64)vol->cluster_size_mask;
1939 }
1940 BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
1941 /* Check if new size is allowed in $AttrDef. */
1942 err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size);
1943 if (unlikely(err)) {
1944 /* Only emit errors when the write will fail completely. */
1945 read_lock_irqsave(&ni->size_lock, flags);
1946 allocated_size = ni->allocated_size;
1947 read_unlock_irqrestore(&ni->size_lock, flags);
1948 if (start < 0 || start >= allocated_size) {
1949 if (err == -ERANGE) {
1950 ntfs_error(vol->sb, "Cannot extend allocation "
1951 "of inode 0x%lx, attribute "
1952 "type 0x%x, because the new "
1953 "allocation would exceed the "
1954 "maximum allowed size for "
1955 "this attribute type.",
1956 vi->i_ino, (unsigned)
1957 le32_to_cpu(ni->type));
1958 } else {
1959 ntfs_error(vol->sb, "Cannot extend allocation "
1960 "of inode 0x%lx, attribute "
1961 "type 0x%x, because this "
1962 "attribute type is not "
1963 "defined on the NTFS volume. "
1964 "Possible corruption! You "
1965 "should run chkdsk!",
1966 vi->i_ino, (unsigned)
1967 le32_to_cpu(ni->type));
1968 }
1969 }
1970 /* Translate error code to be POSIX conformant for write(2). */
1971 if (err == -ERANGE)
1972 err = -EFBIG;
1973 else
1974 err = -EIO;
1975 return err;
1976 }
1977 if (!NInoAttr(ni))
1978 base_ni = ni;
1979 else
1980 base_ni = ni->ext.base_ntfs_ino;
1981 /*
1982 * We will be modifying both the runlist (if non-resident) and the mft
1983 * record so lock them both down.
1984 */
1985 down_write(&ni->runlist.lock);
1986 m = map_mft_record(base_ni);
1987 if (IS_ERR(m)) {
1988 err = PTR_ERR(m);
1989 m = NULL;
1990 ctx = NULL;
1991 goto err_out;
1992 }
1993 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1994 if (unlikely(!ctx)) {
1995 err = -ENOMEM;
1996 goto err_out;
1997 }
1998 read_lock_irqsave(&ni->size_lock, flags);
1999 allocated_size = ni->allocated_size;
2000 read_unlock_irqrestore(&ni->size_lock, flags);
2001 /*
2002 * If non-resident, seek to the last extent. If resident, there is
2003 * only one extent, so seek to that.
2004 */
2005 vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits :
2006 0;
2007 /*
2008 * Abort if someone did the work whilst we waited for the locks. If we
2009 * just converted the attribute from resident to non-resident it is
2010 * likely that exactly this has happened already. We cannot quite
2011 * abort if we need to update the data size.
2012 */
2013 if (unlikely(new_alloc_size <= allocated_size)) {
2014 ntfs_debug("Allocated size already exceeds requested size.");
2015 new_alloc_size = allocated_size;
2016 if (new_data_size < 0)
2017 goto done;
2018 /*
2019 * We want the first attribute extent so that we can update the
2020 * data size.
2021 */
2022 vcn = 0;
2023 }
2024 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2025 CASE_SENSITIVE, vcn, NULL, 0, ctx);
2026 if (unlikely(err)) {
2027 if (err == -ENOENT)
2028 err = -EIO;
2029 goto err_out;
2030 }
2031 m = ctx->mrec;
2032 a = ctx->attr;
2033 /* Use goto to reduce indentation. */
2034 if (a->non_resident)
2035 goto do_non_resident_extend;
2036 BUG_ON(NInoNonResident(ni));
2037 /* The total length of the attribute value. */
2038 attr_len = le32_to_cpu(a->data.resident.value_length);
2039 /*
2040 * Extend the attribute record to be able to store the new attribute
2041 * size. ntfs_attr_record_resize() will not do anything if the size is
2042 * not changing.
2043 */
2044 if (new_alloc_size < vol->mft_record_size &&
2045 !ntfs_attr_record_resize(m, a,
2046 le16_to_cpu(a->data.resident.value_offset) +
2047 new_alloc_size)) {
2048 /* The resize succeeded! */
2049 write_lock_irqsave(&ni->size_lock, flags);
2050 ni->allocated_size = le32_to_cpu(a->length) -
2051 le16_to_cpu(a->data.resident.value_offset);
2052 write_unlock_irqrestore(&ni->size_lock, flags);
2053 if (new_data_size >= 0) {
2054 BUG_ON(new_data_size < attr_len);
2055 a->data.resident.value_length =
2056 cpu_to_le32((u32)new_data_size);
2057 }
2058 goto flush_done;
2059 }
2060 /*
2061 * We have to drop all the locks so we can call
2062 * ntfs_attr_make_non_resident(). This could be optimised by try-
2063 * locking the first page cache page and only if that fails dropping
2064 * the locks, locking the page, and redoing all the locking and
2065 * lookups. While this would be a huge optimisation, it is not worth
2066 * it as this is definitely a slow code path.
2067 */
2068 ntfs_attr_put_search_ctx(ctx);
2069 unmap_mft_record(base_ni);
2070 up_write(&ni->runlist.lock);
2071 /*
2072 * Not enough space in the mft record, try to make the attribute
2073 * non-resident and if successful restart the extension process.
2074 */
2075 err = ntfs_attr_make_non_resident(ni, attr_len);
2076 if (likely(!err))
2077 goto retry_extend;
2078 /*
2079 * Could not make non-resident. If this is due to this not being
2080 * permitted for this attribute type or there not being enough space,
2081 * try to make other attributes non-resident. Otherwise fail.
2082 */
2083 if (unlikely(err != -EPERM && err != -ENOSPC)) {
2084 /* Only emit errors when the write will fail completely. */
2085 read_lock_irqsave(&ni->size_lock, flags);
2086 allocated_size = ni->allocated_size;
2087 read_unlock_irqrestore(&ni->size_lock, flags);
2088 if (start < 0 || start >= allocated_size)
2089 ntfs_error(vol->sb, "Cannot extend allocation of "
2090 "inode 0x%lx, attribute type 0x%x, "
2091 "because the conversion from resident "
2092 "to non-resident attribute failed "
2093 "with error code %i.", vi->i_ino,
2094 (unsigned)le32_to_cpu(ni->type), err);
2095 if (err != -ENOMEM)
2096 err = -EIO;
2097 goto conv_err_out;
2098 }
2099 /* TODO: Not implemented from here, abort. */
2100 read_lock_irqsave(&ni->size_lock, flags);
2101 allocated_size = ni->allocated_size;
2102 read_unlock_irqrestore(&ni->size_lock, flags);
2103 if (start < 0 || start >= allocated_size) {
2104 if (err == -ENOSPC)
2105 ntfs_error(vol->sb, "Not enough space in the mft "
2106 "record/on disk for the non-resident "
2107 "attribute value. This case is not "
2108 "implemented yet.");
2109 else /* if (err == -EPERM) */
2110 ntfs_error(vol->sb, "This attribute type may not be "
2111 "non-resident. This case is not "
2112 "implemented yet.");
2113 }
2114 err = -EOPNOTSUPP;
2115 goto conv_err_out;
2116#if 0
2117 // TODO: Attempt to make other attributes non-resident.
2118 if (!err)
2119 goto do_resident_extend;
2120 /*
2121 * Both the attribute list attribute and the standard information
2122 * attribute must remain in the base inode. Thus, if this is one of
2123 * these attributes, we have to try to move other attributes out into
2124 * extent mft records instead.
2125 */
2126 if (ni->type == AT_ATTRIBUTE_LIST ||
2127 ni->type == AT_STANDARD_INFORMATION) {
2128 // TODO: Attempt to move other attributes into extent mft
2129 // records.
2130 err = -EOPNOTSUPP;
2131 if (!err)
2132 goto do_resident_extend;
2133 goto err_out;
2134 }
2135 // TODO: Attempt to move this attribute to an extent mft record, but
2136 // only if it is not already the only attribute in an mft record in
2137 // which case there would be nothing to gain.
2138 err = -EOPNOTSUPP;
2139 if (!err)
2140 goto do_resident_extend;
2141 /* There is nothing we can do to make enough space. )-: */
2142 goto err_out;
2143#endif
2144do_non_resident_extend:
2145 BUG_ON(!NInoNonResident(ni));
2146 if (new_alloc_size == allocated_size) {
2147 BUG_ON(vcn);
2148 goto alloc_done;
2149 }
2150 /*
2151 * If the data starts after the end of the old allocation, this is a
2152 * $DATA attribute and sparse attributes are enabled on the volume and
2153 * for this inode, then create a sparse region between the old
2154 * allocated size and the start of the data. Otherwise simply proceed
2155 * with filling the whole space between the old allocated size and the
2156 * new allocated size with clusters.
2157 */
2158 if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA ||
2159 !NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
2160 goto skip_sparse;
2161 // TODO: This is not implemented yet. We just fill in with real
2162 // clusters for now...
2163 ntfs_debug("Inserting holes is not-implemented yet. Falling back to "
2164 "allocating real clusters instead.");
2165skip_sparse:
2166 rl = ni->runlist.rl;
2167 if (likely(rl)) {
2168 /* Seek to the end of the runlist. */
2169 while (rl->length)
2170 rl++;
2171 }
2172 /* If this attribute extent is not mapped, map it now. */
2173 if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED ||
2174 (rl->lcn == LCN_ENOENT && rl > ni->runlist.rl &&
2175 (rl-1)->lcn == LCN_RL_NOT_MAPPED))) {
2176 if (!rl && !allocated_size)
2177 goto first_alloc;
2178 rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
2179 if (IS_ERR(rl)) {
2180 err = PTR_ERR(rl);
2181 if (start < 0 || start >= allocated_size)
2182 ntfs_error(vol->sb, "Cannot extend allocation "
2183 "of inode 0x%lx, attribute "
2184 "type 0x%x, because the "
2185 "mapping of a runlist "
2186 "fragment failed with error "
2187 "code %i.", vi->i_ino,
2188 (unsigned)le32_to_cpu(ni->type),
2189 err);
2190 if (err != -ENOMEM)
2191 err = -EIO;
2192 goto err_out;
2193 }
2194 ni->runlist.rl = rl;
2195 /* Seek to the end of the runlist. */
2196 while (rl->length)
2197 rl++;
2198 }
2199 /*
2200 * We now know the runlist of the last extent is mapped and @rl is at
2201 * the end of the runlist. We want to begin allocating clusters
2202 * starting at the last allocated cluster to reduce fragmentation. If
2203 * there are no valid LCNs in the attribute we let the cluster
2204 * allocator choose the starting cluster.
2205 */
2206 /* If the last LCN is a hole or simillar seek back to last real LCN. */
2207 while (rl->lcn < 0 && rl > ni->runlist.rl)
2208 rl--;
2209first_alloc:
2210 // FIXME: Need to implement partial allocations so at least part of the
2211 // write can be performed when start >= 0. (Needed for POSIX write(2)
2212 // conformance.)
2213 rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits,
2214 (new_alloc_size - allocated_size) >>
2215 vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ?
2216 rl->lcn + rl->length : -1, DATA_ZONE, TRUE);
2217 if (IS_ERR(rl2)) {
2218 err = PTR_ERR(rl2);
2219 if (start < 0 || start >= allocated_size)
2220 ntfs_error(vol->sb, "Cannot extend allocation of "
2221 "inode 0x%lx, attribute type 0x%x, "
2222 "because the allocation of clusters "
2223 "failed with error code %i.", vi->i_ino,
2224 (unsigned)le32_to_cpu(ni->type), err);
2225 if (err != -ENOMEM && err != -ENOSPC)
2226 err = -EIO;
2227 goto err_out;
2228 }
2229 rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
2230 if (IS_ERR(rl)) {
2231 err = PTR_ERR(rl);
2232 if (start < 0 || start >= allocated_size)
2233 ntfs_error(vol->sb, "Cannot extend allocation of "
2234 "inode 0x%lx, attribute type 0x%x, "
2235 "because the runlist merge failed "
2236 "with error code %i.", vi->i_ino,
2237 (unsigned)le32_to_cpu(ni->type), err);
2238 if (err != -ENOMEM)
2239 err = -EIO;
2240 if (ntfs_cluster_free_from_rl(vol, rl2)) {
2241 ntfs_error(vol->sb, "Failed to release allocated "
2242 "cluster(s) in error code path. Run "
2243 "chkdsk to recover the lost "
2244 "cluster(s).");
2245 NVolSetErrors(vol);
2246 }
2247 ntfs_free(rl2);
2248 goto err_out;
2249 }
2250 ni->runlist.rl = rl;
2251 ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size -
2252 allocated_size) >> vol->cluster_size_bits);
2253 /* Find the runlist element with which the attribute extent starts. */
2254 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
2255 rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
2256 BUG_ON(!rl2);
2257 BUG_ON(!rl2->length);
2258 BUG_ON(rl2->lcn < LCN_HOLE);
2259 mp_rebuilt = FALSE;
2260 /* Get the size for the new mapping pairs array for this extent. */
2261 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
2262 if (unlikely(mp_size <= 0)) {
2263 err = mp_size;
2264 if (start < 0 || start >= allocated_size)
2265 ntfs_error(vol->sb, "Cannot extend allocation of "
2266 "inode 0x%lx, attribute type 0x%x, "
2267 "because determining the size for the "
2268 "mapping pairs failed with error code "
2269 "%i.", vi->i_ino,
2270 (unsigned)le32_to_cpu(ni->type), err);
2271 err = -EIO;
2272 goto undo_alloc;
2273 }
2274 /* Extend the attribute record to fit the bigger mapping pairs array. */
2275 attr_len = le32_to_cpu(a->length);
2276 err = ntfs_attr_record_resize(m, a, mp_size +
2277 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
2278 if (unlikely(err)) {
2279 BUG_ON(err != -ENOSPC);
2280 // TODO: Deal with this by moving this extent to a new mft
2281 // record or by starting a new extent in a new mft record,
2282 // possibly by extending this extent partially and filling it
2283 // and creating a new extent for the remainder, or by making
2284 // other attributes non-resident and/or by moving other
2285 // attributes out of this mft record.
2286 if (start < 0 || start >= allocated_size)
2287 ntfs_error(vol->sb, "Not enough space in the mft "
2288 "record for the extended attribute "
2289 "record. This case is not "
2290 "implemented yet.");
2291 err = -EOPNOTSUPP;
2292 goto undo_alloc;
2293 }
2294 mp_rebuilt = TRUE;
2295 /* Generate the mapping pairs array directly into the attr record. */
2296 err = ntfs_mapping_pairs_build(vol, (u8*)a +
2297 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
2298 mp_size, rl2, ll, -1, NULL);
2299 if (unlikely(err)) {
2300 if (start < 0 || start >= allocated_size)
2301 ntfs_error(vol->sb, "Cannot extend allocation of "
2302 "inode 0x%lx, attribute type 0x%x, "
2303 "because building the mapping pairs "
2304 "failed with error code %i.", vi->i_ino,
2305 (unsigned)le32_to_cpu(ni->type), err);
2306 err = -EIO;
2307 goto undo_alloc;
2308 }
2309 /* Update the highest_vcn. */
2310 a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
2311 vol->cluster_size_bits) - 1);
2312 /*
2313 * We now have extended the allocated size of the attribute. Reflect
2314 * this in the ntfs_inode structure and the attribute record.
2315 */
2316 if (a->data.non_resident.lowest_vcn) {
2317 /*
2318 * We are not in the first attribute extent, switch to it, but
2319 * first ensure the changes will make it to disk later.
2320 */
2321 flush_dcache_mft_record_page(ctx->ntfs_ino);
2322 mark_mft_record_dirty(ctx->ntfs_ino);
2323 ntfs_attr_reinit_search_ctx(ctx);
2324 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2325 CASE_SENSITIVE, 0, NULL, 0, ctx);
2326 if (unlikely(err))
2327 goto restore_undo_alloc;
2328 /* @m is not used any more so no need to set it. */
2329 a = ctx->attr;
2330 }
2331 write_lock_irqsave(&ni->size_lock, flags);
2332 ni->allocated_size = new_alloc_size;
2333 a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
2334 /*
2335 * FIXME: This would fail if @ni is a directory, $MFT, or an index,
2336 * since those can have sparse/compressed set. For example can be
2337 * set compressed even though it is not compressed itself and in that
2338 * case the bit means that files are to be created compressed in the
2339 * directory... At present this is ok as this code is only called for
2340 * regular files, and only for their $DATA attribute(s).
2341 * FIXME: The calculation is wrong if we created a hole above. For now
2342 * it does not matter as we never create holes.
2343 */
2344 if (NInoSparse(ni) || NInoCompressed(ni)) {
2345 ni->itype.compressed.size += new_alloc_size - allocated_size;
2346 a->data.non_resident.compressed_size =
2347 cpu_to_sle64(ni->itype.compressed.size);
2348 vi->i_blocks = ni->itype.compressed.size >> 9;
2349 } else
2350 vi->i_blocks = new_alloc_size >> 9;
2351 write_unlock_irqrestore(&ni->size_lock, flags);
2352alloc_done:
2353 if (new_data_size >= 0) {
2354 BUG_ON(new_data_size <
2355 sle64_to_cpu(a->data.non_resident.data_size));
2356 a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
2357 }
2358flush_done:
2359 /* Ensure the changes make it to disk. */
2360 flush_dcache_mft_record_page(ctx->ntfs_ino);
2361 mark_mft_record_dirty(ctx->ntfs_ino);
2362done:
2363 ntfs_attr_put_search_ctx(ctx);
2364 unmap_mft_record(base_ni);
2365 up_write(&ni->runlist.lock);
2366 ntfs_debug("Done, new_allocated_size 0x%llx.",
2367 (unsigned long long)new_alloc_size);
2368 return new_alloc_size;
2369restore_undo_alloc:
2370 if (start < 0 || start >= allocated_size)
2371 ntfs_error(vol->sb, "Cannot complete extension of allocation "
2372 "of inode 0x%lx, attribute type 0x%x, because "
2373 "lookup of first attribute extent failed with "
2374 "error code %i.", vi->i_ino,
2375 (unsigned)le32_to_cpu(ni->type), err);
2376 if (err == -ENOENT)
2377 err = -EIO;
2378 ntfs_attr_reinit_search_ctx(ctx);
2379 if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
2380 allocated_size >> vol->cluster_size_bits, NULL, 0,
2381 ctx)) {
2382 ntfs_error(vol->sb, "Failed to find last attribute extent of "
2383 "attribute in error code path. Run chkdsk to "
2384 "recover.");
2385 write_lock_irqsave(&ni->size_lock, flags);
2386 ni->allocated_size = new_alloc_size;
2387 /*
2388 * FIXME: This would fail if @ni is a directory... See above.
2389 * FIXME: The calculation is wrong if we created a hole above.
2390 * For now it does not matter as we never create holes.
2391 */
2392 if (NInoSparse(ni) || NInoCompressed(ni)) {
2393 ni->itype.compressed.size += new_alloc_size -
2394 allocated_size;
2395 vi->i_blocks = ni->itype.compressed.size >> 9;
2396 } else
2397 vi->i_blocks = new_alloc_size >> 9;
2398 write_unlock_irqrestore(&ni->size_lock, flags);
2399 ntfs_attr_put_search_ctx(ctx);
2400 unmap_mft_record(base_ni);
2401 up_write(&ni->runlist.lock);
2402 /*
2403 * The only thing that is now wrong is the allocated size of the
2404 * base attribute extent which chkdsk should be able to fix.
2405 */
2406 NVolSetErrors(vol);
2407 return err;
2408 }
2409 ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64(
2410 (allocated_size >> vol->cluster_size_bits) - 1);
2411undo_alloc:
2412 ll = allocated_size >> vol->cluster_size_bits;
2413 if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
2414 ntfs_error(vol->sb, "Failed to release allocated cluster(s) "
2415 "in error code path. Run chkdsk to recover "
2416 "the lost cluster(s).");
2417 NVolSetErrors(vol);
2418 }
2419 m = ctx->mrec;
2420 a = ctx->attr;
2421 /*
2422 * If the runlist truncation fails and/or the search context is no
2423 * longer valid, we cannot resize the attribute record or build the
2424 * mapping pairs array thus we mark the inode bad so that no access to
2425 * the freed clusters can happen.
2426 */
2427 if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) {
2428 ntfs_error(vol->sb, "Failed to %s in error code path. Run "
2429 "chkdsk to recover.", IS_ERR(m) ?
2430 "restore attribute search context" :
2431 "truncate attribute runlist");
2432 make_bad_inode(vi);
2433 make_bad_inode(VFS_I(base_ni));
2434 NVolSetErrors(vol);
2435 } else if (mp_rebuilt) {
2436 if (ntfs_attr_record_resize(m, a, attr_len)) {
2437 ntfs_error(vol->sb, "Failed to restore attribute "
2438 "record in error code path. Run "
2439 "chkdsk to recover.");
2440 make_bad_inode(vi);
2441 make_bad_inode(VFS_I(base_ni));
2442 NVolSetErrors(vol);
2443 } else /* if (success) */ {
2444 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
2445 a->data.non_resident.
2446 mapping_pairs_offset), attr_len -
2447 le16_to_cpu(a->data.non_resident.
2448 mapping_pairs_offset), rl2, ll, -1,
2449 NULL)) {
2450 ntfs_error(vol->sb, "Failed to restore "
2451 "mapping pairs array in error "
2452 "code path. Run chkdsk to "
2453 "recover.");
2454 make_bad_inode(vi);
2455 make_bad_inode(VFS_I(base_ni));
2456 NVolSetErrors(vol);
2457 }
2458 flush_dcache_mft_record_page(ctx->ntfs_ino);
2459 mark_mft_record_dirty(ctx->ntfs_ino);
2460 }
2461 }
2462err_out:
2463 if (ctx)
2464 ntfs_attr_put_search_ctx(ctx);
2465 if (m)
2466 unmap_mft_record(base_ni);
2467 up_write(&ni->runlist.lock);
2468conv_err_out:
2469 ntfs_debug("Failed. Returning error code %i.", err);
2470 return err;
2471}
2472
2473/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 * ntfs_attr_set - fill (a part of) an attribute with a byte
2475 * @ni: ntfs inode describing the attribute to fill
2476 * @ofs: offset inside the attribute at which to start to fill
2477 * @cnt: number of bytes to fill
2478 * @val: the unsigned 8-bit value with which to fill the attribute
2479 *
2480 * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
2481 * byte offset @ofs inside the attribute with the constant byte @val.
2482 *
2483 * This function is effectively like memset() applied to an ntfs attribute.
Anton Altaparmakovda284382004-11-11 11:18:10 +00002484 * Note thie function actually only operates on the page cache pages belonging
2485 * to the ntfs attribute and it marks them dirty after doing the memset().
2486 * Thus it relies on the vm dirty page write code paths to cause the modified
2487 * pages to be written to the mft record/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 *
2489 * Return 0 on success and -errno on error. An error code of -ESPIPE means
2490 * that @ofs + @cnt were outside the end of the attribute and no write was
2491 * performed.
2492 */
2493int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
2494{
2495 ntfs_volume *vol = ni->vol;
2496 struct address_space *mapping;
2497 struct page *page;
2498 u8 *kaddr;
2499 pgoff_t idx, end;
2500 unsigned int start_ofs, end_ofs, size;
2501
2502 ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
2503 (long long)ofs, (long long)cnt, val);
2504 BUG_ON(ofs < 0);
2505 BUG_ON(cnt < 0);
2506 if (!cnt)
2507 goto done;
Anton Altaparmakov807c4532005-09-08 21:01:17 +01002508 /*
2509 * FIXME: Compressed and encrypted attributes are not supported when
2510 * writing and we should never have gotten here for them.
2511 */
2512 BUG_ON(NInoCompressed(ni));
2513 BUG_ON(NInoEncrypted(ni));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 mapping = VFS_I(ni)->i_mapping;
2515 /* Work out the starting index and page offset. */
2516 idx = ofs >> PAGE_CACHE_SHIFT;
2517 start_ofs = ofs & ~PAGE_CACHE_MASK;
2518 /* Work out the ending index and page offset. */
2519 end = ofs + cnt;
2520 end_ofs = end & ~PAGE_CACHE_MASK;
2521 /* If the end is outside the inode size return -ESPIPE. */
Anton Altaparmakovda284382004-11-11 11:18:10 +00002522 if (unlikely(end > i_size_read(VFS_I(ni)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 ntfs_error(vol->sb, "Request exceeds end of attribute.");
2524 return -ESPIPE;
2525 }
2526 end >>= PAGE_CACHE_SHIFT;
2527 /* If there is a first partial page, need to do it the slow way. */
2528 if (start_ofs) {
2529 page = read_cache_page(mapping, idx,
2530 (filler_t*)mapping->a_ops->readpage, NULL);
2531 if (IS_ERR(page)) {
2532 ntfs_error(vol->sb, "Failed to read first partial "
2533 "page (sync error, index 0x%lx).", idx);
2534 return PTR_ERR(page);
2535 }
2536 wait_on_page_locked(page);
2537 if (unlikely(!PageUptodate(page))) {
2538 ntfs_error(vol->sb, "Failed to read first partial page "
2539 "(async error, index 0x%lx).", idx);
2540 page_cache_release(page);
2541 return PTR_ERR(page);
2542 }
2543 /*
2544 * If the last page is the same as the first page, need to
2545 * limit the write to the end offset.
2546 */
2547 size = PAGE_CACHE_SIZE;
2548 if (idx == end)
2549 size = end_ofs;
2550 kaddr = kmap_atomic(page, KM_USER0);
2551 memset(kaddr + start_ofs, val, size - start_ofs);
2552 flush_dcache_page(page);
2553 kunmap_atomic(kaddr, KM_USER0);
2554 set_page_dirty(page);
2555 page_cache_release(page);
2556 if (idx == end)
2557 goto done;
2558 idx++;
2559 }
2560 /* Do the whole pages the fast way. */
2561 for (; idx < end; idx++) {
2562 /* Find or create the current page. (The page is locked.) */
2563 page = grab_cache_page(mapping, idx);
2564 if (unlikely(!page)) {
2565 ntfs_error(vol->sb, "Insufficient memory to grab "
2566 "page (index 0x%lx).", idx);
2567 return -ENOMEM;
2568 }
2569 kaddr = kmap_atomic(page, KM_USER0);
2570 memset(kaddr, val, PAGE_CACHE_SIZE);
2571 flush_dcache_page(page);
2572 kunmap_atomic(kaddr, KM_USER0);
2573 /*
2574 * If the page has buffers, mark them uptodate since buffer
2575 * state and not page state is definitive in 2.6 kernels.
2576 */
2577 if (page_has_buffers(page)) {
2578 struct buffer_head *bh, *head;
2579
2580 bh = head = page_buffers(page);
2581 do {
2582 set_buffer_uptodate(bh);
2583 } while ((bh = bh->b_this_page) != head);
2584 }
2585 /* Now that buffers are uptodate, set the page uptodate, too. */
2586 SetPageUptodate(page);
2587 /*
2588 * Set the page and all its buffers dirty and mark the inode
2589 * dirty, too. The VM will write the page later on.
2590 */
2591 set_page_dirty(page);
2592 /* Finally unlock and release the page. */
2593 unlock_page(page);
2594 page_cache_release(page);
Anton Altaparmakov29b89902005-10-11 14:54:42 +01002595 balance_dirty_pages_ratelimited(mapping);
2596 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598 /* If there is a last partial page, need to do it the slow way. */
2599 if (end_ofs) {
2600 page = read_cache_page(mapping, idx,
2601 (filler_t*)mapping->a_ops->readpage, NULL);
2602 if (IS_ERR(page)) {
2603 ntfs_error(vol->sb, "Failed to read last partial page "
2604 "(sync error, index 0x%lx).", idx);
2605 return PTR_ERR(page);
2606 }
2607 wait_on_page_locked(page);
2608 if (unlikely(!PageUptodate(page))) {
2609 ntfs_error(vol->sb, "Failed to read last partial page "
2610 "(async error, index 0x%lx).", idx);
2611 page_cache_release(page);
2612 return PTR_ERR(page);
2613 }
2614 kaddr = kmap_atomic(page, KM_USER0);
2615 memset(kaddr, val, end_ofs);
2616 flush_dcache_page(page);
2617 kunmap_atomic(kaddr, KM_USER0);
2618 set_page_dirty(page);
2619 page_cache_release(page);
2620 }
2621done:
2622 ntfs_debug("Done.");
2623 return 0;
2624}
Anton Altaparmakov53d59aa2005-03-17 10:51:33 +00002625
2626#endif /* NTFS_RW */