4 * Copyright (C) 1996-1999 Martin von Löwis
5 * Copyright (C) 1996-1997 Régis Duchesne
6 * Copyright (C) 1998 Joseph Malicki
7 * Copyright (C) 1999 Steve Dodd
8 * Copyright (C) 2001 Anton Altaparmakov (AIA)
11 #include "ntfstypes.h"
15 #include <linux/errno.h>
16 #include <linux/ntfs_fs.h>
25 * ntfs_find_attr_in_mft_rec - find attribute in mft record
26 * @vol: volume on which attr resides
27 * @m: mft record to search
28 * @type: attribute type to find
29 * @name: attribute name to find (optional, i.e. NULL means don't care)
30 * @name_len: attribute name length (only needed if @name present)
31 * @ic: ignore case if 1 or case sensitive if 0 (ignored if @name NULL)
32 * @instance: instance number to find
34 * Only search the specified mft record and it ignores the presence of an
35 * attribute list attribute (unless it is the one being searched for,
36 * obviously, in which case it is returned).
38 ntfs_u8* ntfs_find_attr_in_mft_rec(ntfs_volume *vol, ntfs_u8 *m, __u32 type,
39 wchar_t *name, __u32 name_len, int ic, __u16 instance)
43 /* Iterate over attributes in mft record @m. */
44 a = m + NTFS_GETU16(m + 20); /* attrs_offset */
45 for (; a >= m && a <= m + vol->mft_record_size;
46 a += NTFS_GETU32(a + 4 /* length */)) {
47 /* We catch $END with this more general check, too... */
48 if (NTFS_GETU32(a + 0 /* type */) > type)
50 if (!NTFS_GETU32(a + 4 /* length */))
52 if (NTFS_GETU32(a + 0 /* type */) != type)
54 /* If @name is present, compare the two names. */
55 if (name && !ntfs_are_names_equal(name, name_len, (wchar_t*)
56 (a + NTFS_GETU16(a + 10 /* name_offset */)),
57 a[9] /* name_length */, ic, vol->upcase,
58 vol->upcase_length)) {
61 rc = ntfs_collate_names(vol->upcase, vol->upcase_length,
62 name, name_len, (wchar_t*)(a +
63 NTFS_GETU16(a + 10 /* name_offset */)),
64 a[9] /* name_length */, 1, 1);
66 * If @name collates before a->name, there is no
71 /* If the strings are not equal, continue search. */
74 rc = ntfs_collate_names(vol->upcase, vol->upcase_length,
75 name, name_len, (wchar_t*)(a +
76 NTFS_GETU16(a + 10 /* name_offset */)),
77 a[9] /* name_length */, 0, 1);
84 * The names match or @name not present. Check instance number.
85 * and if it matches we have found the attribute and are done.
87 if (instance != NTFS_GETU16(a + 14 /* instance */))
89 ntfs_debug(DEBUG_FILE3, "ntfs_find_attr_in_mft_record: found: "
90 "attr type 0x%x, instance number = 0x%x.\n",
91 NTFS_GETU32(a + 0), instance);
94 ntfs_error("ntfs_find_attr_in_mft_record: mft record 0x%x is corrupt"
95 ". Run chkdsk.\n", m);
99 /* Look if an attribute already exists in the inode, and if not, create it. */
100 int ntfs_new_attr(ntfs_inode *ino, int type, void *name, int namelen,
101 void *value, int value_len, int *pos, int *found)
107 for (i = 0; i < ino->attr_count; i++)
112 if (a->type > type) {
116 /* If @name is present, compare the two names. */
117 if (namelen && !ntfs_are_names_equal((wchar_t*)name, namelen,
118 a->name, a->namelen /* name_length */,
119 1 /* ignore case*/, ino->vol->upcase,
120 ino->vol->upcase_length)) {
123 rc = ntfs_collate_names(ino->vol->upcase,
124 ino->vol->upcase_length, a->name,
125 a->namelen, (wchar_t*)name, namelen,
126 1 /* ignore case */, 1);
133 rc = ntfs_collate_names(ino->vol->upcase,
134 ino->vol->upcase_length, a->name,
135 a->namelen, (wchar_t*)name, namelen,
136 0 /* case sensitive */, 1);
144 /* Names are equal or no name was asked for. */
145 /* If a value was specified compare the values. */
146 if (value_len && a->resident) {
148 ntfs_error("ntfs_new_attr: Value specified but "
149 "attribute non-resident. Bug!\n");
155 m = memcmp(value, a->d.data, m);
162 /* Values match until min of value lengths. */
163 if (value_len > a->size)
165 if (value_len < a->size) {
175 /* Re-allocate space. */
176 if (ino->attr_count % 8 == 0)
179 new = (ntfs_attribute*)ntfs_malloc((ino->attr_count + 8) *
180 sizeof(ntfs_attribute));
184 ntfs_memcpy(new, ino->attrs, ino->attr_count *
185 sizeof(ntfs_attribute));
186 ntfs_free(ino->attrs);
191 ntfs_memmove(ino->attrs + i + 1, ino->attrs + i,
192 (ino->attr_count - i) * sizeof(ntfs_attribute));
194 ino->attrs[i].type = type;
195 ino->attrs[i].namelen = namelen;
196 ino->attrs[i].name = name;
202 int ntfs_make_attr_resident(ntfs_inode *ino, ntfs_attribute *attr)
204 __s64 size = attr->size;
206 /* FIXME: read data, free clusters */
213 /* Store in the inode readable information about a run. */
214 int ntfs_insert_run(ntfs_attribute *attr, int cnum, ntfs_cluster_t cluster,
217 /* (re-)allocate space if necessary. */
218 if ((attr->d.r.len * sizeof(ntfs_runlist)) % PAGE_SIZE == 0) {
220 unsigned long new_size;
222 ntfs_debug(DEBUG_MALLOC, "ntfs_insert_run: re-allocating "
223 "space: old attr->d.r.len = 0x%x\n",
225 new_size = attr->d.r.len * sizeof(ntfs_runlist) + PAGE_SIZE;
226 if ((new_size >> PAGE_SHIFT) > num_physpages) {
227 ntfs_error("ntfs_insert_run: attempted to allocate "
228 "more pages than num_physpages."
229 "This might be a bug or a corrupt"
233 new = ntfs_vmalloc(new_size);
235 ntfs_error("ntfs_insert_run: ntfs_vmalloc(new_size = "
236 "0x%x) failed\n", new_size);
239 if (attr->d.r.runlist) {
240 ntfs_memcpy(new, attr->d.r.runlist, attr->d.r.len
241 * sizeof(ntfs_runlist));
242 ntfs_vfree(attr->d.r.runlist);
244 attr->d.r.runlist = new;
246 if (attr->d.r.len > cnum)
247 ntfs_memmove(attr->d.r.runlist + cnum + 1,
248 attr->d.r.runlist + cnum,
249 (attr->d.r.len - cnum) * sizeof(ntfs_runlist));
250 attr->d.r.runlist[cnum].lcn = cluster;
251 attr->d.r.runlist[cnum].len = len;
257 * ntfs_extend_attr - extend allocated size of an attribute
258 * @ino: ntfs inode containing the attribute to extend
259 * @attr: attribute which to extend
260 * @len: desired new length for @attr (_not_ the amount to extend by)
262 * Extends an attribute. Allocate clusters on the volume which @ino belongs to.
263 * Extends the run list accordingly, preferably by extending the last run of
264 * the existing run list, first.
266 * Only modifies attr->allocated, i.e. doesn't touch attr->size, nor
269 int ntfs_extend_attr(ntfs_inode *ino, ntfs_attribute *attr, const __s64 len)
271 int rlen, rl2_len, err = 0;
272 ntfs_cluster_t cluster, clen;
273 ntfs_runlist *rl, *rl2;
275 if ((attr->flags & (ATTR_IS_COMPRESSED | ATTR_IS_ENCRYPTED)) ||
276 ino->record_count > 1)
279 * FIXME: Don't make non-resident if the attribute type is not right.
280 * For example cannot make index attribute non-resident! (AIA)
282 if (attr->resident) {
283 err = ntfs_make_attr_nonresident(ino, attr);
287 if (len <= attr->allocated)
288 return 0; /* Truly stupid things do sometimes happen. */
289 rl = attr->d.r.runlist;
290 rlen = attr->d.r.len;
292 cluster = rl[rlen - 1].lcn + rl[rlen - 1].len;
294 /* No preference for allocation space. */
295 cluster = (ntfs_cluster_t)-1;
297 * Calculate the extra space we need, and round up to multiple of
298 * cluster size to get number of new clusters needed.
300 clen = (len - attr->allocated + ino->vol->cluster_size - 1) >>
301 ino->vol->cluster_size_bits;
304 err = ntfs_allocate_clusters(ino->vol, &cluster, &clen, &rl2,
305 &rl2_len, DATA_ZONE);
308 attr->allocated += (__s64)clen << ino->vol->cluster_size_bits;
310 err = splice_runlists(&rl, &rlen, rl2, rl2_len);
320 attr->d.r.runlist = rl;
321 attr->d.r.len = rlen;
325 int ntfs_make_attr_nonresident(ntfs_inode *ino, ntfs_attribute *attr)
329 void *data = attr->d.data;
330 __s64 len = attr->size;
333 attr->d.r.runlist = NULL;
336 * ->allocated is updated by ntfs_extend_attr(), while ->initialized
337 * and ->size are updated by ntfs_readwrite_attr(). (AIA)
339 attr->allocated = attr->initialized = 0;
340 error = ntfs_extend_attr(ino, attr, len);
342 return error; /* FIXME: On error, restore old values. */
343 io.fn_put = ntfs_put;
344 io.fn_get = ntfs_get;
348 return ntfs_readwrite_attr(ino, attr, 0, &io);
351 int ntfs_attr_allnonresident(ntfs_inode *ino)
354 ntfs_volume *vol = ino->vol;
356 for (i = 0; !error && i < ino->attr_count; i++)
358 if (ino->attrs[i].type != vol->at_security_descriptor &&
359 ino->attrs[i].type != vol->at_data)
361 error = ntfs_make_attr_nonresident(ino, ino->attrs + i);
367 * Resize the attribute to a newsize. attr->allocated and attr->size are
368 * updated, but attr->initialized is not changed unless it becomes bigger than
369 * attr->size, in which case it is set to attr->size.
371 int ntfs_resize_attr(ntfs_inode *ino, ntfs_attribute *attr, __s64 newsize)
374 __s64 oldsize = attr->size;
375 int clustersizebits = ino->vol->cluster_size_bits;
376 int i, count, newcount;
377 ntfs_runlist *rl, *rlt;
379 if (newsize == oldsize)
381 if (attr->flags & (ATTR_IS_COMPRESSED | ATTR_IS_ENCRYPTED))
383 if (attr->resident) {
385 if (newsize > ino->vol->mft_record_size) {
386 error = ntfs_make_attr_nonresident(ino, attr);
389 return ntfs_resize_attr(ino, attr, newsize);
393 __s64 minsize = newsize;
394 attr->d.data = ntfs_malloc(newsize);
399 if (newsize > oldsize) {
401 ntfs_bzero((char*)attr->d.data + oldsize,
404 ntfs_memcpy((char*)attr->d.data, v, minsize);
408 attr->size = newsize;
411 /* Non-resident attribute. */
412 rl = attr->d.r.runlist;
413 if (newsize < oldsize) {
416 * FIXME: We might be going awfully wrong for newsize = 0,
417 * possibly even leaking memory really badly. But considering
418 * in that case there is more breakage due to -EOPNOTSUPP stuff
419 * further down the code path, who cares for the moment... (AIA)
421 for (i = 0, count = 0; i < attr->d.r.len; i++) {
422 if ((__s64)(count + rl[i].len) << clustersizebits >
427 count += (int)rl[i].len;
430 /* Free unused clusters in current run, unless sparse. */
431 if (rl[--i].lcn != (ntfs_cluster_t)-1) {
432 ntfs_cluster_t rounded = newsize - ((__s64)count <<
434 rounded = (rounded + ino->vol->cluster_size - 1) >>
436 error = ntfs_deallocate_cluster_run(ino->vol,
438 rl[i].len - rounded);
440 return error; /* FIXME: Incomplete operation. */
442 newcount = count + rounded;
444 /* Free all other runs. */
446 error = ntfs_deallocate_clusters(ino->vol, rl + i,
449 return error; /* FIXME: Incomplete operation. */
451 * Free space for extra runs in memory if enough memory left
452 * to do so. FIXME: Only do it if it would free memory. (AIA)
454 rl_size = ((i + 1) * sizeof(ntfs_runlist) + PAGE_SIZE - 1) &
456 if (rl_size < ((attr->d.r.len * sizeof(ntfs_runlist) +
457 PAGE_SIZE - 1) & PAGE_MASK)) {
458 rlt = ntfs_vmalloc(rl_size);
460 ntfs_memcpy(rlt, rl, i * sizeof(ntfs_runlist));
462 attr->d.r.runlist = rl = rlt;
465 rl[i].lcn = (ntfs_cluster_t)-1;
466 rl[i].len = (ntfs_cluster_t)0;
469 error = ntfs_extend_attr(ino, attr, newsize);
471 return error; /* FIXME: Incomplete operation. */
472 newcount = (newsize + ino->vol->cluster_size - 1) >>
475 /* Fill in new sizes. */
476 attr->allocated = (__s64)newcount << clustersizebits;
477 attr->size = newsize;
478 if (attr->initialized > newsize)
479 attr->initialized = newsize;
481 error = ntfs_make_attr_resident(ino, attr);
485 int ntfs_create_attr(ntfs_inode *ino, int anum, char *aname, void *data,
486 int dsize, ntfs_attribute **rattr)
492 ntfs_attribute *attr;
494 if (dsize > ino->vol->mft_record_size)
495 /* FIXME: Non-resident attributes. */
498 namelen = strlen(aname);
499 name = ntfs_malloc(2 * namelen);
502 ntfs_ascii2uni(name, aname, namelen);
507 error = ntfs_new_attr(ino, anum, name, namelen, data, dsize, &i,
509 if (error || found) {
511 return error ? error : -EEXIST;
513 *rattr = attr = ino->attrs + i;
514 /* Allocate a new number.
515 * FIXME: Should this happen on inode writeback?
516 * FIXME: Extension records not supported. */
517 error = ntfs_allocate_attr_number(ino, &i);
521 if (attr->attrno + 1 != NTFS_GETU16(ino->attr + 0x28))
522 ntfs_error("UH OH! attr->attrno (%i) != NTFS_GETU16(ino->attr "
523 "+ 0x28) (%i)\n", attr->attrno,
524 NTFS_GETU16(ino->attr + 0x28));
528 attr->size = attr->allocated = attr->initialized = dsize;
530 /* FIXME: INDEXED information should come from $AttrDef
531 * Currently, only file names are indexed. As of NTFS v3.0 (Win2k),
532 * this is no longer true. Different attributes can be indexed now. */
533 if (anum == ino->vol->at_file_name)
537 attr->d.data = ntfs_malloc(dsize);
540 ntfs_memcpy(attr->d.data, data, dsize);
545 * Non-resident attributes are stored in runs (intervals of clusters).
547 * This function stores in the inode readable information about a non-resident
550 static int ntfs_process_runs(ntfs_inode *ino, ntfs_attribute* attr,
553 int startvcn, endvcn;
555 ntfs_cluster_t cluster;
558 startvcn = NTFS_GETS64(data + 0x10);
559 endvcn = NTFS_GETS64(data + 0x18);
561 /* Check whether this chunk really belongs to the end. Problem with
562 * this: this functions can get called on the last extent first, before
563 * it is called on the other extents in sequence. This happens when the
564 * base mft record contains the last extent instead of the first one
565 * and the first extent is stored, like any intermediate extents in
566 * extension mft records. This would be difficult to allow the way the
567 * runlist is stored in memory. Thus we fix elsewhere by causing the
568 * attribute list attribute to be processed immediately when found. The
569 * extents will then be processed starting with the first one. */
570 for (cnum = 0, vcn = 0; cnum < attr->d.r.len; cnum++)
571 vcn += attr->d.r.runlist[cnum].len;
572 if (vcn != startvcn) {
573 ntfs_debug(DEBUG_FILE3, "ntfs_process_runs: ino = 0x%x, "
574 "attr->type = 0x%x, startvcn = 0x%x, endvcn = 0x%x, "
575 "vcn = 0x%x, cnum = 0x%x\n", ino->i_number, attr->type,
576 startvcn, endvcn, vcn, cnum);
577 if (vcn < startvcn) {
578 ntfs_error("Problem with runlist in extended record\n");
581 /* Tried to insert an already inserted runlist. */
586 /* Allocated length. */
587 endvcn = NTFS_GETS64(data + 0x28) - 1;
588 endvcn >>= ino->vol->cluster_size_bits;
590 /* This is an extent. Allocated length is not defined!
591 * Extents must have an endvcn though so this is an
593 ntfs_error("Corrupt attribute extent. (endvcn is "
598 data = data + NTFS_GETU16(data + 0x20);
599 cnum = attr->d.r.len;
601 for (vcn = startvcn; vcn <= endvcn; vcn += len) {
602 if (ntfs_decompress_run(&data, &len, &cluster, &ctype)) {
603 ntfs_debug(DEBUG_FILE3, "ntfs_process_runs: "
604 "ntfs_decompress_run failed. i_number = 0x%x\n",
609 er = ntfs_insert_run(attr, cnum, -1, len);
611 er = ntfs_insert_run(attr, cnum, cluster, len);
617 ntfs_error("ntfs_process_runs: ntfs_insert_run failed\n");
618 ntfs_debug(DEBUG_FILE3, "ntfs_process_runs: startvcn = 0x%x, vcn = 0x%x"
619 ", endvcn = 0x%x, cnum = %i\n", startvcn, vcn,
624 /* Insert the attribute starting at attr in the inode ino. */
625 int ntfs_insert_attribute(ntfs_inode *ino, unsigned char *attrdata)
632 ntfs_attribute *attr;
635 type = NTFS_GETU32(attrdata);
636 namelen = NTFS_GETU8(attrdata + 9);
637 ntfs_debug(DEBUG_FILE3, "ntfs_insert_attribute: ino->i_number 0x%x, "
638 "attr type 0x%x\n", ino->i_number, type);
639 /* Read the attribute's name if it has one. */
643 /* 1 Unicode character fits in 2 bytes. */
644 name = ntfs_malloc(2 * namelen);
647 ntfs_memcpy(name, attrdata + NTFS_GETU16(attrdata + 10),
650 /* If resident look for value, too. */
651 if (NTFS_GETU8(attrdata + 8) == 0)
652 error = ntfs_new_attr(ino, type, name, namelen,
653 attrdata + NTFS_GETU16(attrdata + 0x14),
654 NTFS_GETU16(attrdata + 0x10), &i, &found);
656 error = ntfs_new_attr(ino, type, name, namelen, NULL, 0, &i,
659 ntfs_debug(DEBUG_FILE3, "ntfs_insert_attribute: ntfs_new_attr "
666 /* It's already there, if not resident just process the runs. */
667 if (!ino->attrs[i].resident) {
668 ntfs_debug(DEBUG_FILE3, "ntfs_insert_attribute:"
669 " processing runs 1.\n");
670 /* FIXME: Check error code! (AIA) */
671 ntfs_process_runs(ino, ino->attrs + i, attrdata);
675 attr = ino->attrs + i;
676 attr->resident = NTFS_GETU8(attrdata + 8) == 0;
677 attr->flags = *(__u16*)(attrdata + 0xC);
678 attr->attrno = NTFS_GETU16(attrdata + 0xE);
680 if (attr->resident) {
681 attr->size = NTFS_GETU16(attrdata + 0x10);
682 data = attrdata + NTFS_GETU16(attrdata + 0x14);
683 attr->d.data = (void*)ntfs_malloc(attr->size);
686 ntfs_memcpy(attr->d.data, data, attr->size);
687 attr->indexed = NTFS_GETU8(attrdata + 0x16);
689 attr->allocated = NTFS_GETS64(attrdata + 0x28);
690 attr->size = NTFS_GETS64(attrdata + 0x30);
691 attr->initialized = NTFS_GETS64(attrdata + 0x38);
692 attr->cengine = NTFS_GETU16(attrdata + 0x22);
693 if (attr->flags & ATTR_IS_COMPRESSED)
694 attr->compsize = NTFS_GETS64(attrdata + 0x40);
695 ntfs_debug(DEBUG_FILE3, "ntfs_insert_attribute: "
696 "attr->allocated = 0x%Lx, attr->size = 0x%Lx, "
697 "attr->initialized = 0x%Lx\n", attr->allocated,
698 attr->size, attr->initialized);
699 ino->attrs[i].d.r.runlist = 0;
700 ino->attrs[i].d.r.len = 0;
701 ntfs_debug(DEBUG_FILE3, "ntfs_insert_attribute: processing "
703 /* FIXME: Check error code! (AIA) */
704 ntfs_process_runs(ino, attr, attrdata);
709 int ntfs_read_zero(ntfs_io *dest, int size)
712 char *sparse = ntfs_calloc(512);
719 dest->fn_put(dest, sparse, i);
726 /* Process compressed attributes. */
727 int ntfs_read_compressed(ntfs_inode *ino, ntfs_attribute *attr, __s64 offset,
732 int s_vcn, rnum, vcn, got, l1;
733 __s64 copied, len, chunk, offs1, l, chunk2;
734 ntfs_cluster_t cluster, cl1;
735 char *comp = 0, *comp1;
741 clustersizebits = ino->vol->cluster_size_bits;
742 /* Starting cluster of potential chunk. There are three situations:
743 a) In a large uncompressible or sparse chunk, s_vcn is in the middle
745 b) s_vcn is right on a run border.
746 c) When several runs make a chunk, s_vcn is before the chunks. */
747 s_vcn = offset >> clustersizebits;
748 /* Round down to multiple of 16. */
750 rl = attr->d.r.runlist;
751 for (rnum = vcn = 0; rnum < attr->d.r.len && vcn + rl->len <= s_vcn;
754 if (rnum == attr->d.r.len) {
755 /* Beyond end of file. */
756 /* FIXME: Check allocated / initialized. */
761 io.fn_put = ntfs_put;
768 if (cluster == (ntfs_cluster_t)-1) {
769 /* Sparse cluster. */
772 if ((len - (s_vcn - vcn)) & 15)
773 ntfs_error("Unexpected sparse chunk size.");
774 ll = ((__s64)(vcn + len) << clustersizebits) - offset;
778 error = ntfs_read_zero(dest, ll);
781 } else if (dest->do_read) {
783 comp = ntfs_malloc(16 << clustersizebits);
790 /* We might need to start in the middle of a run. */
791 cl1 = cluster + s_vcn - vcn;
803 io.size = (__s64)l1 << clustersizebits;
804 error = ntfs_getput_clusters(ino->vol, cl1, 0,
808 if (l1 + delta == len) {
812 cluster = cl1 = rl->lcn;
816 comp1 += (__s64)l1 << clustersizebits;
817 } while (cluster != (ntfs_cluster_t)-1 && got < 16);
818 /* Until empty run. */
819 chunk = 16 << clustersizebits;
820 if (cluster != (ntfs_cluster_t)-1 || got == 16)
825 decomp = ntfs_malloc(16 <<
832 /* Make sure there are null bytes after the
834 *(ntfs_u32*)comp1 = 0;
835 ntfs_decompress(decomp, comp, chunk);
838 offs1 = offset - ((__s64)s_vcn << clustersizebits);
839 chunk2 = (16 << clustersizebits) - offs1;
844 dest->fn_put(dest, comp1 + offs1, chunk);
849 s_vcn = (offset >> clustersizebits) & ~15;
850 if (l && offset >= ((__s64)(vcn + len) << clustersizebits)) {
867 int ntfs_write_compressed(ntfs_inode *ino, ntfs_attribute *attr, __s64 offset,