1 /**
2 * inode.c - Inode handling code. Originated from the Linux-NTFS project.
3 *
4 * Copyright (c) 2002-2005 Anton Altaparmakov
5 * Copyright (c) 2002-2008 Szabolcs Szakacsits
6 * Copyright (c) 2004-2007 Yura Pakhuchiy
7 * Copyright (c) 2004-2005 Richard Russon
8 * Copyright (c) 2009-2010 Jean-Pierre Andre
9 *
10 * This program/include file is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program/include file is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program (in the main directory of the NTFS-3G
22 * distribution in the file COPYING); if not, write to the Free Software
23 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36 #ifdef HAVE_ERRNO_H
37 #include <errno.h>
38 #endif
39
40 #ifdef __HAIKU__
41 #include <KernelExport.h>
42 #endif
43
44 #include "param.h"
45 #include "compat.h"
46 #include "types.h"
47 #include "volume.h"
48 #include "cache.h"
49 #include "inode.h"
50 #include "attrib.h"
51 #include "debug.h"
52 #include "mft.h"
53 #include "attrlist.h"
54 #include "runlist.h"
55 #include "lcnalloc.h"
56 #include "index.h"
57 #include "dir.h"
58 #include "ntfstime.h"
59 #include "logging.h"
60 #include "misc.h"
61 #include "xattrs.h"
62
ntfs_inode_base(ntfs_inode * ni)63 ntfs_inode *ntfs_inode_base(ntfs_inode *ni)
64 {
65 if (ni->nr_extents == -1)
66 return ni->base_ni;
67 return ni;
68 }
69
70 /**
71 * ntfs_inode_mark_dirty - set the inode (and its base inode if it exists) dirty
72 * @ni: ntfs inode to set dirty
73 *
74 * Set the inode @ni dirty so it is written out later (at the latest at
75 * ntfs_inode_close() time). If @ni is an extent inode, set the base inode
76 * dirty, too.
77 *
78 * This function cannot fail.
79 */
ntfs_inode_mark_dirty(ntfs_inode * ni)80 void ntfs_inode_mark_dirty(ntfs_inode *ni)
81 {
82 NInoSetDirty(ni);
83 if (ni->nr_extents == -1)
84 NInoSetDirty(ni->base_ni);
85 }
86
87 /**
88 * __ntfs_inode_allocate - Create and initialise an NTFS inode object
89 * @vol:
90 *
91 * Description...
92 *
93 * Returns:
94 */
__ntfs_inode_allocate(ntfs_volume * vol)95 static ntfs_inode *__ntfs_inode_allocate(ntfs_volume *vol)
96 {
97 ntfs_inode *ni;
98
99 ni = (ntfs_inode*)ntfs_calloc(sizeof(ntfs_inode));
100 if (ni)
101 ni->vol = vol;
102 return ni;
103 }
104
105 /**
106 * ntfs_inode_allocate - Create an NTFS inode object
107 * @vol:
108 *
109 * Description...
110 *
111 * Returns:
112 */
ntfs_inode_allocate(ntfs_volume * vol)113 ntfs_inode *ntfs_inode_allocate(ntfs_volume *vol)
114 {
115 return __ntfs_inode_allocate(vol);
116 }
117
118 /**
119 * __ntfs_inode_release - Destroy an NTFS inode object
120 * @ni:
121 *
122 * Description...
123 *
124 * Returns:
125 */
__ntfs_inode_release(ntfs_inode * ni)126 static void __ntfs_inode_release(ntfs_inode *ni)
127 {
128 if (NInoDirty(ni))
129 ntfs_log_error("Releasing dirty inode %lld!\n",
130 (long long)ni->mft_no);
131 if (NInoAttrList(ni) && ni->attr_list)
132 free(ni->attr_list);
133 free(ni->mrec);
134 free(ni);
135 return;
136 }
137
138 /**
139 * ntfs_inode_open - open an inode ready for access
140 * @vol: volume to get the inode from
141 * @mref: inode number / mft record number to open
142 *
143 * Allocate an ntfs_inode structure and initialize it for the given inode
144 * specified by @mref. @mref specifies the inode number / mft record to read,
145 * including the sequence number, which can be 0 if no sequence number checking
146 * is to be performed.
147 *
148 * Then, allocate a buffer for the mft record, read the mft record from the
149 * volume @vol, and attach it to the ntfs_inode structure (->mrec). The
150 * mft record is mst deprotected and sanity checked for validity and we abort
151 * if deprotection or checks fail.
152 *
153 * Finally, search for an attribute list attribute in the mft record and if one
154 * is found, load the attribute list attribute value and attach it to the
155 * ntfs_inode structure (->attr_list). Also set the NI_AttrList bit to indicate
156 * this.
157 *
158 * Return a pointer to the ntfs_inode structure on success or NULL on error,
159 * with errno set to the error code.
160 */
ntfs_inode_real_open(ntfs_volume * vol,const MFT_REF mref)161 static ntfs_inode *ntfs_inode_real_open(ntfs_volume *vol, const MFT_REF mref)
162 {
163 s64 l;
164 ntfs_inode *ni = NULL;
165 ntfs_attr_search_ctx *ctx;
166 STANDARD_INFORMATION *std_info;
167 le32 lthle;
168 int olderrno;
169
170 ntfs_log_enter("Entering for inode %lld\n", (long long)MREF(mref));
171 if (!vol) {
172 errno = EINVAL;
173 goto out;
174 }
175 ni = __ntfs_inode_allocate(vol);
176 if (!ni)
177 goto out;
178 if (ntfs_file_record_read(vol, mref, &ni->mrec, NULL))
179 goto err_out;
180 if (!(ni->mrec->flags & MFT_RECORD_IN_USE)) {
181 errno = ENOENT;
182 goto err_out;
183 }
184 ni->mft_no = MREF(mref);
185 ctx = ntfs_attr_get_search_ctx(ni, NULL);
186 if (!ctx)
187 goto err_out;
188 /* Receive some basic information about inode. */
189 if (ntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED,
190 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
191 if (!ni->mrec->base_mft_record)
192 ntfs_log_perror("No STANDARD_INFORMATION in base record"
193 " %lld", (long long)MREF(mref));
194 goto put_err_out;
195 }
196 lthle = ctx->attr->value_length;
197 if (le32_to_cpu(lthle) < offsetof(STANDARD_INFORMATION, owner_id)) {
198 ntfs_log_error("Corrupt STANDARD_INFORMATION in base"
199 " record %lld\n",
200 (long long)MREF(mref));
201 goto put_err_out;
202 }
203 std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr +
204 le16_to_cpu(ctx->attr->value_offset));
205 ni->flags = std_info->file_attributes;
206 ni->creation_time = std_info->creation_time;
207 ni->last_data_change_time = std_info->last_data_change_time;
208 ni->last_mft_change_time = std_info->last_mft_change_time;
209 ni->last_access_time = std_info->last_access_time;
210 /* Insert v3 extensions if present */
211 /* length may be seen as 48 (v1.x) or 72 (v3.x) */
212 if (le32_to_cpu(lthle) >= offsetof(STANDARD_INFORMATION, v3_end)) {
213 set_nino_flag(ni, v3_Extensions);
214 ni->owner_id = std_info->owner_id;
215 ni->security_id = std_info->security_id;
216 ni->quota_charged = std_info->quota_charged;
217 ni->usn = std_info->usn;
218 } else {
219 clear_nino_flag(ni, v3_Extensions);
220 ni->owner_id = const_cpu_to_le32(0);
221 ni->security_id = const_cpu_to_le32(0);
222 }
223 /* Set attribute list information. */
224 olderrno = errno;
225 if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, AT_UNNAMED, 0,
226 CASE_SENSITIVE, 0, NULL, 0, ctx)) {
227 if (errno != ENOENT)
228 goto put_err_out;
229 /* Attribute list attribute does not present. */
230 /* restore previous errno to avoid misinterpretation */
231 errno = olderrno;
232 goto get_size;
233 }
234 NInoSetAttrList(ni);
235 l = ntfs_get_attribute_value_length(ctx->attr);
236 if (!l)
237 goto put_err_out;
238 if ((u64)l > 0x40000) {
239 errno = EIO;
240 ntfs_log_perror("Too large attrlist attribute (%llu), inode "
241 "%lld", (long long)l, (long long)MREF(mref));
242 goto put_err_out;
243 }
244 ni->attr_list_size = l;
245 ni->attr_list = ntfs_malloc(ni->attr_list_size);
246 if (!ni->attr_list)
247 goto put_err_out;
248 l = ntfs_get_attribute_value(vol, ctx->attr, ni->attr_list);
249 if (!l)
250 goto put_err_out;
251 if (l != ni->attr_list_size) {
252 errno = EIO;
253 ntfs_log_perror("Unexpected attrlist size (%lld <> %u), inode "
254 "%lld", (long long)l, ni->attr_list_size,
255 (long long)MREF(mref));
256 goto put_err_out;
257 }
258 get_size:
259 olderrno = errno;
260 if (ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx)) {
261 if (errno != ENOENT)
262 goto put_err_out;
263 /* Directory or special file. */
264 /* restore previous errno to avoid misinterpretation */
265 errno = olderrno;
266 ni->data_size = ni->allocated_size = 0;
267 } else {
268 if (ctx->attr->non_resident) {
269 ni->data_size = sle64_to_cpu(ctx->attr->data_size);
270 if (ctx->attr->flags &
271 (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE))
272 ni->allocated_size = sle64_to_cpu(
273 ctx->attr->compressed_size);
274 else
275 ni->allocated_size = sle64_to_cpu(
276 ctx->attr->allocated_size);
277 } else {
278 ni->data_size = le32_to_cpu(ctx->attr->value_length);
279 ni->allocated_size = (ni->data_size + 7) & ~7;
280 }
281 set_nino_flag(ni,KnownSize);
282 }
283 ntfs_attr_put_search_ctx(ctx);
284 out:
285 ntfs_log_leave("\n");
286 return ni;
287
288 put_err_out:
289 ntfs_attr_put_search_ctx(ctx);
290 err_out:
291 __ntfs_inode_release(ni);
292 ni = NULL;
293 goto out;
294 }
295
296 /**
297 * ntfs_inode_close - close an ntfs inode and free all associated memory
298 * @ni: ntfs inode to close
299 *
300 * Make sure the ntfs inode @ni is clean.
301 *
302 * If the ntfs inode @ni is a base inode, close all associated extent inodes,
303 * then deallocate all memory attached to it, and finally free the ntfs inode
304 * structure itself.
305 *
306 * If it is an extent inode, we disconnect it from its base inode before we
307 * destroy it.
308 *
309 * It is OK to pass NULL to this function, it is just noop in this case.
310 *
311 * Return 0 on success or -1 on error with errno set to the error code. On
312 * error, @ni has not been freed. The user should attempt to handle the error
313 * and call ntfs_inode_close() again. The following error codes are defined:
314 *
315 * EBUSY @ni and/or its attribute list runlist is/are dirty and the
316 * attempt to write it/them to disk failed.
317 * EINVAL @ni is invalid (probably it is an extent inode).
318 * EIO I/O error while trying to write inode to disk.
319 */
320
ntfs_inode_real_close(ntfs_inode * ni)321 int ntfs_inode_real_close(ntfs_inode *ni)
322 {
323 int ret = -1;
324
325 if (!ni)
326 return 0;
327
328 ntfs_log_enter("Entering for inode %lld\n", (long long)ni->mft_no);
329
330 /* If we have dirty metadata, write it out. */
331 if (NInoDirty(ni) || NInoAttrListDirty(ni)) {
332 if (ntfs_inode_sync(ni)) {
333 if (errno != EIO)
334 errno = EBUSY;
335 goto err;
336 }
337 }
338 /* Is this a base inode with mapped extent inodes? */
339 if (ni->nr_extents > 0) {
340 while (ni->nr_extents > 0) {
341 if (ntfs_inode_real_close(ni->extent_nis[0])) {
342 if (errno != EIO)
343 errno = EBUSY;
344 goto err;
345 }
346 }
347 } else if (ni->nr_extents == -1) {
348 ntfs_inode **tmp_nis;
349 ntfs_inode *base_ni;
350 s32 i;
351
352 /*
353 * If the inode is an extent inode, disconnect it from the
354 * base inode before destroying it.
355 */
356 base_ni = ni->base_ni;
357 for (i = 0; i < base_ni->nr_extents; ++i) {
358 tmp_nis = base_ni->extent_nis;
359 if (tmp_nis[i] != ni)
360 continue;
361 /* Found it. Disconnect. */
362 memmove(tmp_nis + i, tmp_nis + i + 1,
363 (base_ni->nr_extents - i - 1) *
364 sizeof(ntfs_inode *));
365 /* Buffer should be for multiple of four extents. */
366 if ((--base_ni->nr_extents) & 3) {
367 i = -1;
368 break;
369 }
370 /*
371 * ElectricFence is unhappy with realloc(x,0) as free(x)
372 * thus we explicitly separate these two cases.
373 */
374 if (base_ni->nr_extents) {
375 /* Resize the memory buffer. */
376 tmp_nis = realloc(tmp_nis, base_ni->nr_extents *
377 sizeof(ntfs_inode *));
378 /* Ignore errors, they don't really matter. */
379 if (tmp_nis)
380 base_ni->extent_nis = tmp_nis;
381 } else if (tmp_nis) {
382 free(tmp_nis);
383 base_ni->extent_nis = (ntfs_inode**)NULL;
384 }
385 /* Allow for error checking. */
386 i = -1;
387 break;
388 }
389
390 /*
391 * We could successfully sync, so only log this error
392 * and try to sync other inode extents too.
393 */
394 if (i != -1)
395 ntfs_log_error("Extent inode %lld was not found\n",
396 (long long)ni->mft_no);
397 }
398
399 __ntfs_inode_release(ni);
400 ret = 0;
401 err:
402 ntfs_log_leave("\n");
403 return ret;
404 }
405
406 #if CACHE_NIDATA_SIZE
407
408 /*
409 * Free an inode structure when there is not more space
410 * in the cache
411 */
412
ntfs_inode_nidata_free(const struct CACHED_GENERIC * cached)413 void ntfs_inode_nidata_free(const struct CACHED_GENERIC *cached)
414 {
415 ntfs_inode_real_close(((const struct CACHED_NIDATA*)cached)->ni);
416 }
417
418 /*
419 * Compute a hash value for an inode entry
420 */
421
ntfs_inode_nidata_hash(const struct CACHED_GENERIC * item)422 int ntfs_inode_nidata_hash(const struct CACHED_GENERIC *item)
423 {
424 return (((const struct CACHED_NIDATA*)item)->inum
425 % (2*CACHE_NIDATA_SIZE));
426 }
427
428 /*
429 * inum comparing for entering/fetching from cache
430 */
431
idata_cache_compare(const struct CACHED_GENERIC * cached,const struct CACHED_GENERIC * wanted)432 static int idata_cache_compare(const struct CACHED_GENERIC *cached,
433 const struct CACHED_GENERIC *wanted)
434 {
435 return (((const struct CACHED_NIDATA*)cached)->inum
436 != ((const struct CACHED_NIDATA*)wanted)->inum);
437 }
438
439 /*
440 * Invalidate an inode entry when not needed anymore.
441 * The entry should have been synced, it may be reused later,
442 * if it is requested before it is dropped from cache.
443 */
444
ntfs_inode_invalidate(ntfs_volume * vol,const MFT_REF mref)445 void ntfs_inode_invalidate(ntfs_volume *vol, const MFT_REF mref)
446 {
447 struct CACHED_NIDATA item;
448
449 item.inum = MREF(mref);
450 item.ni = (ntfs_inode*)NULL;
451 item.pathname = (const char*)NULL;
452 item.varsize = 0;
453 ntfs_invalidate_cache(vol->nidata_cache,
454 GENERIC(&item),idata_cache_compare,CACHE_FREE);
455 }
456
457 #endif
458
459 /*
460 * Open an inode
461 *
462 * When possible, an entry recorded in the cache is reused
463 *
464 * **NEVER REOPEN** an inode, this can lead to a duplicated
465 * cache entry (hard to detect), and to an obsolete one being
466 * reused. System files are however protected from being cached.
467 */
468
ntfs_inode_open(ntfs_volume * vol,const MFT_REF mref)469 ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref)
470 {
471 ntfs_inode *ni;
472 #if CACHE_NIDATA_SIZE
473 struct CACHED_NIDATA item;
474 struct CACHED_NIDATA *cached;
475
476 /* fetch idata from cache */
477 item.inum = MREF(mref);
478 debug_double_inode(item.inum,1);
479 item.pathname = (const char*)NULL;
480 item.varsize = 0;
481 cached = (struct CACHED_NIDATA*)ntfs_fetch_cache(vol->nidata_cache,
482 GENERIC(&item),idata_cache_compare);
483 if (cached) {
484 ni = cached->ni;
485 /* do not keep open entries in cache */
486 ntfs_remove_cache(vol->nidata_cache,
487 (struct CACHED_GENERIC*)cached,0);
488 } else {
489 ni = ntfs_inode_real_open(vol, mref);
490 }
491 if (!ni) {
492 debug_double_inode(item.inum, 0);
493 }
494 #else
495 ni = ntfs_inode_real_open(vol, mref);
496 #endif
497 return (ni);
498 }
499
500 /*
501 * Close an inode entry
502 *
503 * If cacheing is in use, the entry is synced and kept available
504 * in cache for further use.
505 *
506 * System files (inode < 16 or having the IS_4 flag) are protected
507 * against being cached.
508 */
509
ntfs_inode_close(ntfs_inode * ni)510 int ntfs_inode_close(ntfs_inode *ni)
511 {
512 int res;
513 #if CACHE_NIDATA_SIZE
514 BOOL dirty;
515 struct CACHED_NIDATA item;
516
517 if (ni) {
518 debug_double_inode(ni->mft_no,0);
519 /* do not cache system files : could lead to double entries */
520 if (ni->vol && ni->vol->nidata_cache
521 && ((ni->mft_no == FILE_root)
522 || ((ni->mft_no >= FILE_first_user)
523 && !(ni->mrec->flags & MFT_RECORD_IS_4)))) {
524 /* If we have dirty metadata, write it out. */
525 dirty = NInoDirty(ni) || NInoAttrListDirty(ni);
526 if (dirty) {
527 res = ntfs_inode_sync(ni);
528 /* do a real close if sync failed */
529 if (res)
530 ntfs_inode_real_close(ni);
531 } else
532 res = 0;
533
534 if (!res) {
535 /* feed idata into cache */
536 item.inum = ni->mft_no;
537 item.ni = ni;
538 item.pathname = (const char*)NULL;
539 item.varsize = 0;
540 debug_cached_inode(ni);
541 #if defined(__HAIKU__)
542 if (ntfs_fetch_cache(ni->vol->nidata_cache,
543 GENERIC(&item), idata_cache_compare)) {
544 panic("ntfs_inode_close: %llu already in cache!", ni->mft_no);
545 }
546 #endif
547 ntfs_enter_cache(ni->vol->nidata_cache,
548 GENERIC(&item), idata_cache_compare);
549 }
550 } else {
551 /* cache not ready or system file, really close */
552 res = ntfs_inode_real_close(ni);
553 }
554 } else
555 res = 0;
556 #else
557 res = ntfs_inode_real_close(ni);
558 #endif
559 return (res);
560 }
561
562 /**
563 * ntfs_extent_inode_open - load an extent inode and attach it to its base
564 * @base_ni: base ntfs inode
565 * @mref: mft reference of the extent inode to load (in little endian)
566 *
567 * First check if the extent inode @mref is already attached to the base ntfs
568 * inode @base_ni, and if so, return a pointer to the attached extent inode.
569 *
570 * If the extent inode is not already attached to the base inode, allocate an
571 * ntfs_inode structure and initialize it for the given inode @mref. @mref
572 * specifies the inode number / mft record to read, including the sequence
573 * number, which can be 0 if no sequence number checking is to be performed.
574 *
575 * Then, allocate a buffer for the mft record, read the mft record from the
576 * volume @base_ni->vol, and attach it to the ntfs_inode structure (->mrec).
577 * The mft record is mst deprotected and sanity checked for validity and we
578 * abort if deprotection or checks fail.
579 *
580 * Finally attach the ntfs inode to its base inode @base_ni and return a
581 * pointer to the ntfs_inode structure on success or NULL on error, with errno
582 * set to the error code.
583 *
584 * Note, extent inodes are never closed directly. They are automatically
585 * disposed off by the closing of the base inode.
586 */
ntfs_extent_inode_open(ntfs_inode * base_ni,const leMFT_REF mref)587 ntfs_inode *ntfs_extent_inode_open(ntfs_inode *base_ni, const leMFT_REF mref)
588 {
589 u64 mft_no = MREF_LE(mref);
590 VCN extent_vcn;
591 runlist_element *rl;
592 ntfs_volume *vol;
593 ntfs_inode *ni = NULL;
594 ntfs_inode **extent_nis;
595 int i;
596
597 if (!base_ni) {
598 errno = EINVAL;
599 ntfs_log_perror("%s", __FUNCTION__);
600 return NULL;
601 }
602
603 ntfs_log_enter("Opening extent inode %lld (base mft record %lld).\n",
604 (unsigned long long)mft_no,
605 (unsigned long long)base_ni->mft_no);
606
607 if (!base_ni->mft_no) {
608 /*
609 * When getting extents of MFT, we must be sure
610 * they are in the MFT part which has already
611 * been mapped, otherwise we fall into an endless
612 * recursion.
613 * Situations have been met where extents locations
614 * are described in themselves.
615 * This is a severe error which chkdsk cannot fix.
616 */
617 vol = base_ni->vol;
618 extent_vcn = mft_no << vol->mft_record_size_bits
619 >> vol->cluster_size_bits;
620 rl = vol->mft_na->rl;
621 if (rl) {
622 while (rl->length
623 && ((rl->vcn + rl->length) <= extent_vcn))
624 rl++;
625 }
626 if (!rl || (rl->lcn < 0)) {
627 ntfs_log_error("MFT is corrupt, cannot read"
628 " its unmapped extent record %lld\n",
629 (long long)mft_no);
630 ntfs_log_error("Note : chkdsk cannot fix this,"
631 " try ntfsfix\n");
632 errno = EIO;
633 ni = (ntfs_inode*)NULL;
634 goto out;
635 }
636 }
637
638 /* Is the extent inode already open and attached to the base inode? */
639 if (base_ni->nr_extents > 0) {
640 extent_nis = base_ni->extent_nis;
641 for (i = 0; i < base_ni->nr_extents; i++) {
642 u16 seq_no;
643
644 ni = extent_nis[i];
645 if (mft_no != ni->mft_no)
646 continue;
647 /* Verify the sequence number if given. */
648 seq_no = MSEQNO_LE(mref);
649 if (seq_no && seq_no != le16_to_cpu(
650 ni->mrec->sequence_number)) {
651 errno = EIO;
652 ntfs_log_perror("Found stale extent mft "
653 "reference mft=%lld",
654 (long long)ni->mft_no);
655 goto out;
656 }
657 goto out;
658 }
659 }
660 /* Wasn't there, we need to load the extent inode. */
661 ni = __ntfs_inode_allocate(base_ni->vol);
662 if (!ni)
663 goto out;
664 if (ntfs_file_record_read(base_ni->vol, le64_to_cpu(mref), &ni->mrec, NULL))
665 goto err_out;
666 ni->mft_no = mft_no;
667 ni->nr_extents = -1;
668 ni->base_ni = base_ni;
669 /* Attach extent inode to base inode, reallocating memory if needed. */
670 if (!(base_ni->nr_extents & 3)) {
671 i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *);
672
673 extent_nis = ntfs_malloc(i);
674 if (!extent_nis)
675 goto err_out;
676 if (base_ni->nr_extents) {
677 memcpy(extent_nis, base_ni->extent_nis,
678 i - 4 * sizeof(ntfs_inode *));
679 free(base_ni->extent_nis);
680 }
681 base_ni->extent_nis = extent_nis;
682 }
683 base_ni->extent_nis[base_ni->nr_extents++] = ni;
684 out:
685 ntfs_log_leave("\n");
686 return ni;
687 err_out:
688 __ntfs_inode_release(ni);
689 ni = NULL;
690 goto out;
691 }
692
693 /**
694 * ntfs_inode_attach_all_extents - attach all extents for target inode
695 * @ni: opened ntfs inode for which perform attach
696 *
697 * Return 0 on success and -1 on error with errno set to the error code.
698 */
ntfs_inode_attach_all_extents(ntfs_inode * ni)699 int ntfs_inode_attach_all_extents(ntfs_inode *ni)
700 {
701 ATTR_LIST_ENTRY *ale;
702 u64 prev_attached = 0;
703
704 if (!ni) {
705 ntfs_log_trace("Invalid arguments.\n");
706 errno = EINVAL;
707 return -1;
708 }
709
710 if (ni->nr_extents == -1)
711 ni = ni->base_ni;
712
713 ntfs_log_trace("Entering for inode 0x%llx.\n", (long long) ni->mft_no);
714
715 /* Inode haven't got attribute list, thus nothing to attach. */
716 if (!NInoAttrList(ni))
717 return 0;
718
719 if (!ni->attr_list) {
720 ntfs_log_trace("Corrupt in-memory struct.\n");
721 errno = EINVAL;
722 return -1;
723 }
724
725 /* Walk through attribute list and attach all extents. */
726 errno = 0;
727 ale = (ATTR_LIST_ENTRY *)ni->attr_list;
728 while ((u8*)ale < ni->attr_list + ni->attr_list_size) {
729 if (ni->mft_no != MREF_LE(ale->mft_reference) &&
730 prev_attached != MREF_LE(ale->mft_reference)) {
731 if (!ntfs_extent_inode_open(ni, ale->mft_reference)) {
732 ntfs_log_trace("Couldn't attach extent inode.\n");
733 return -1;
734 }
735 prev_attached = MREF_LE(ale->mft_reference);
736 }
737 ale = (ATTR_LIST_ENTRY *)((u8*)ale + le16_to_cpu(ale->length));
738 }
739 return 0;
740 }
741
742 /**
743 * ntfs_inode_sync_standard_information - update standard information attribute
744 * @ni: ntfs inode to update standard information
745 *
746 * Return 0 on success or -1 on error with errno set to the error code.
747 */
ntfs_inode_sync_standard_information(ntfs_inode * ni)748 static int ntfs_inode_sync_standard_information(ntfs_inode *ni)
749 {
750 ntfs_attr_search_ctx *ctx;
751 STANDARD_INFORMATION *std_info;
752 u32 lth;
753 le32 lthle;
754
755 ntfs_log_trace("Entering for inode %lld\n", (long long)ni->mft_no);
756
757 ctx = ntfs_attr_get_search_ctx(ni, NULL);
758 if (!ctx)
759 return -1;
760 if (ntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED,
761 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
762 ntfs_log_perror("Failed to sync standard info (inode %lld)",
763 (long long)ni->mft_no);
764 ntfs_attr_put_search_ctx(ctx);
765 return -1;
766 }
767 std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr +
768 le16_to_cpu(ctx->attr->value_offset));
769 std_info->file_attributes = ni->flags;
770 if (!test_nino_flag(ni, TimesSet)) {
771 std_info->creation_time = ni->creation_time;
772 std_info->last_data_change_time = ni->last_data_change_time;
773 std_info->last_mft_change_time = ni->last_mft_change_time;
774 std_info->last_access_time = ni->last_access_time;
775 }
776
777 /* JPA update v3.x extensions, ensuring consistency */
778
779 lthle = ctx->attr->value_length;
780 lth = le32_to_cpu(lthle);
781 if (test_nino_flag(ni, v3_Extensions)
782 && (lth < offsetof(STANDARD_INFORMATION, v3_end)))
783 ntfs_log_error("bad sync of standard information\n");
784
785 if (lth >= offsetof(STANDARD_INFORMATION, v3_end)) {
786 std_info->owner_id = ni->owner_id;
787 std_info->security_id = ni->security_id;
788 std_info->quota_charged = ni->quota_charged;
789 std_info->usn = ni->usn;
790 }
791 ntfs_inode_mark_dirty(ctx->ntfs_ino);
792 ntfs_attr_put_search_ctx(ctx);
793 return 0;
794 }
795
796 /**
797 * ntfs_inode_sync_file_name - update FILE_NAME attributes
798 * @ni: ntfs inode to update FILE_NAME attributes
799 *
800 * Update all FILE_NAME attributes for inode @ni in the index.
801 *
802 * Return 0 on success or -1 on error with errno set to the error code.
803 */
ntfs_inode_sync_file_name(ntfs_inode * ni,ntfs_inode * dir_ni)804 static int ntfs_inode_sync_file_name(ntfs_inode *ni, ntfs_inode *dir_ni)
805 {
806 ntfs_attr_search_ctx *ctx = NULL;
807 ntfs_index_context *ictx;
808 ntfs_inode *index_ni;
809 FILE_NAME_ATTR *fn;
810 FILE_NAME_ATTR *fnx;
811 REPARSE_POINT *rpp;
812 le32 reparse_tag;
813 int err = 0;
814
815 ntfs_log_trace("Entering for inode %lld\n", (long long)ni->mft_no);
816
817 ctx = ntfs_attr_get_search_ctx(ni, NULL);
818 if (!ctx) {
819 err = errno;
820 goto err_out;
821 }
822 /* Collect the reparse tag, if any */
823 reparse_tag = const_cpu_to_le32(0);
824 if (ni->flags & FILE_ATTR_REPARSE_POINT) {
825 if (!ntfs_attr_lookup(AT_REPARSE_POINT, NULL,
826 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
827 rpp = (REPARSE_POINT*)((u8 *)ctx->attr +
828 le16_to_cpu(ctx->attr->value_offset));
829 reparse_tag = rpp->reparse_tag;
830 }
831 ntfs_attr_reinit_search_ctx(ctx);
832 }
833 /* Walk through all FILE_NAME attributes and update them. */
834 while (!ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx)) {
835 fn = (FILE_NAME_ATTR *)((u8 *)ctx->attr +
836 le16_to_cpu(ctx->attr->value_offset));
837 if (MREF_LE(fn->parent_directory) == ni->mft_no) {
838 /*
839 * WARNING: We cheat here and obtain 2 attribute
840 * search contexts for one inode (first we obtained
841 * above, second will be obtained inside
842 * ntfs_index_lookup), it's acceptable for library,
843 * but will deadlock in the kernel.
844 */
845 index_ni = ni;
846 } else
847 if (dir_ni)
848 index_ni = dir_ni;
849 else
850 index_ni = ntfs_inode_open(ni->vol,
851 le64_to_cpu(fn->parent_directory));
852 if (!index_ni) {
853 if (!err)
854 err = errno;
855 ntfs_log_perror("Failed to open inode %lld with index",
856 (long long)MREF_LE(fn->parent_directory));
857 continue;
858 }
859 ictx = ntfs_index_ctx_get(index_ni, NTFS_INDEX_I30, 4);
860 if (!ictx) {
861 if (!err)
862 err = errno;
863 ntfs_log_perror("Failed to get index ctx, inode %lld",
864 (long long)index_ni->mft_no);
865 if ((ni != index_ni) && !dir_ni
866 && ntfs_inode_close(index_ni) && !err)
867 err = errno;
868 continue;
869 }
870 if (ntfs_index_lookup(fn, sizeof(FILE_NAME_ATTR), ictx)) {
871 if (!err) {
872 if (errno == ENOENT)
873 err = EIO;
874 else
875 err = errno;
876 }
877 ntfs_log_perror("Index lookup failed, inode %lld",
878 (long long)index_ni->mft_no);
879 ntfs_index_ctx_put(ictx);
880 if (ni != index_ni && ntfs_inode_close(index_ni) && !err)
881 err = errno;
882 continue;
883 }
884 /* Update flags and file size. */
885 fnx = (FILE_NAME_ATTR *)ictx->data;
886 fnx->file_attributes =
887 (fnx->file_attributes & ~FILE_ATTR_VALID_FLAGS) |
888 (ni->flags & FILE_ATTR_VALID_FLAGS);
889 if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
890 fnx->data_size = fnx->allocated_size
891 = const_cpu_to_sle64(0);
892 else {
893 fnx->allocated_size = cpu_to_sle64(ni->allocated_size);
894 fnx->data_size = cpu_to_sle64(ni->data_size);
895 /*
896 * The file name record has also to be fixed if some
897 * attribute update implied the unnamed data to be
898 * made non-resident
899 */
900 fn->allocated_size = fnx->allocated_size;
901 }
902 /* update or clear the reparse tag in the index */
903 fnx->reparse_point_tag = reparse_tag;
904 if (!test_nino_flag(ni, TimesSet)) {
905 fnx->creation_time = ni->creation_time;
906 fnx->last_data_change_time = ni->last_data_change_time;
907 fnx->last_mft_change_time = ni->last_mft_change_time;
908 fnx->last_access_time = ni->last_access_time;
909 } else {
910 fnx->creation_time = fn->creation_time;
911 fnx->last_data_change_time = fn->last_data_change_time;
912 fnx->last_mft_change_time = fn->last_mft_change_time;
913 fnx->last_access_time = fn->last_access_time;
914 }
915 ntfs_index_entry_mark_dirty(ictx);
916 ntfs_index_ctx_put(ictx);
917 if ((ni != index_ni) && !dir_ni
918 && ntfs_inode_close(index_ni) && !err)
919 err = errno;
920 }
921 /* Check for real error occurred. */
922 if (errno != ENOENT) {
923 err = errno;
924 ntfs_log_perror("Attribute lookup failed, inode %lld",
925 (long long)ni->mft_no);
926 goto err_out;
927 }
928 ntfs_attr_put_search_ctx(ctx);
929 if (err) {
930 errno = err;
931 return -1;
932 }
933 return 0;
934 err_out:
935 if (ctx)
936 ntfs_attr_put_search_ctx(ctx);
937 errno = err;
938 return -1;
939 }
940
941 /**
942 * ntfs_inode_sync - write the inode (and its dirty extents) to disk
943 * @ni: ntfs inode to write
944 *
945 * Write the inode @ni to disk as well as its dirty extent inodes if such
946 * exist and @ni is a base inode. If @ni is an extent inode, only @ni is
947 * written completely disregarding its base inode and any other extent inodes.
948 *
949 * For a base inode with dirty extent inodes if any writes fail for whatever
950 * reason, the failing inode is skipped and the sync process is continued. At
951 * the end the error condition that brought about the failure is returned. Thus
952 * the smallest amount of data loss possible occurs.
953 *
954 * Return 0 on success or -1 on error with errno set to the error code.
955 * The following error codes are defined:
956 * EINVAL - Invalid arguments were passed to the function.
957 * EBUSY - Inode and/or one of its extents is busy, try again later.
958 * EIO - I/O error while writing the inode (or one of its extents).
959 */
ntfs_inode_sync_in_dir(ntfs_inode * ni,ntfs_inode * dir_ni)960 static int ntfs_inode_sync_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni)
961 {
962 int ret = 0;
963 int err = 0;
964 if (!ni) {
965 errno = EINVAL;
966 ntfs_log_error("Failed to sync NULL inode\n");
967 return -1;
968 }
969
970 ntfs_log_enter("Entering for inode %lld\n", (long long)ni->mft_no);
971
972 /* Update STANDARD_INFORMATION. */
973 if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 &&
974 ntfs_inode_sync_standard_information(ni)) {
975 if (!err || errno == EIO) {
976 err = errno;
977 if (err != EIO)
978 err = EBUSY;
979 }
980 }
981
982 /* Update FILE_NAME's in the index. */
983 if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 &&
984 NInoFileNameTestAndClearDirty(ni) &&
985 ntfs_inode_sync_file_name(ni, dir_ni)) {
986 if (!err || errno == EIO) {
987 err = errno;
988 if (err != EIO)
989 err = EBUSY;
990 }
991 ntfs_log_perror("Failed to sync FILE_NAME (inode %lld)",
992 (long long)ni->mft_no);
993 NInoFileNameSetDirty(ni);
994 }
995
996 /* Write out attribute list from cache to disk. */
997 if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 &&
998 NInoAttrList(ni) && NInoAttrListTestAndClearDirty(ni)) {
999 ntfs_attr *na;
1000
1001 na = ntfs_attr_open(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0);
1002 if (!na) {
1003 if (!err || errno == EIO) {
1004 err = errno;
1005 if (err != EIO)
1006 err = EBUSY;
1007 ntfs_log_perror("Attribute list sync failed "
1008 "(open, inode %lld)",
1009 (long long)ni->mft_no);
1010 }
1011 NInoAttrListSetDirty(ni);
1012 goto sync_inode;
1013 }
1014
1015 if (na->data_size == ni->attr_list_size) {
1016 if (ntfs_attr_pwrite(na, 0, ni->attr_list_size,
1017 ni->attr_list) != ni->attr_list_size) {
1018 if (!err || errno == EIO) {
1019 err = errno;
1020 if (err != EIO)
1021 err = EBUSY;
1022 ntfs_log_perror("Attribute list sync "
1023 "failed (write, inode %lld)",
1024 (long long)ni->mft_no);
1025 }
1026 NInoAttrListSetDirty(ni);
1027 }
1028 } else {
1029 err = EIO;
1030 ntfs_log_error("Attribute list sync failed (bad size, "
1031 "inode %lld)\n", (long long)ni->mft_no);
1032 NInoAttrListSetDirty(ni);
1033 }
1034 ntfs_attr_close(na);
1035 }
1036
1037 sync_inode:
1038 /* Write this inode out to the $MFT (and $MFTMirr if applicable). */
1039 if (NInoTestAndClearDirty(ni)) {
1040 if (ntfs_mft_record_write(ni->vol, ni->mft_no, ni->mrec)) {
1041 if (!err || errno == EIO) {
1042 err = errno;
1043 if (err != EIO)
1044 err = EBUSY;
1045 }
1046 NInoSetDirty(ni);
1047 ntfs_log_perror("MFT record sync failed, inode %lld",
1048 (long long)ni->mft_no);
1049 }
1050 }
1051
1052 /* If this is a base inode with extents write all dirty extents, too. */
1053 if (ni->nr_extents > 0) {
1054 s32 i;
1055
1056 for (i = 0; i < ni->nr_extents; ++i) {
1057 ntfs_inode *eni;
1058
1059 eni = ni->extent_nis[i];
1060 if (!NInoTestAndClearDirty(eni))
1061 continue;
1062
1063 if (ntfs_mft_record_write(eni->vol, eni->mft_no,
1064 eni->mrec)) {
1065 if (!err || errno == EIO) {
1066 err = errno;
1067 if (err != EIO)
1068 err = EBUSY;
1069 }
1070 NInoSetDirty(eni);
1071 ntfs_log_perror("Extent MFT record sync failed,"
1072 " inode %lld/%lld",
1073 (long long)ni->mft_no,
1074 (long long)eni->mft_no);
1075 }
1076 }
1077 }
1078
1079 if (err) {
1080 errno = err;
1081 ret = -1;
1082 }
1083
1084 ntfs_log_leave("\n");
1085 return ret;
1086 }
1087
ntfs_inode_sync(ntfs_inode * ni)1088 int ntfs_inode_sync(ntfs_inode *ni)
1089 {
1090 return (ntfs_inode_sync_in_dir(ni, (ntfs_inode*)NULL));
1091 }
1092
1093 /*
1094 * Close an inode with an open parent inode
1095 */
1096
ntfs_inode_close_in_dir(ntfs_inode * ni,ntfs_inode * dir_ni)1097 int ntfs_inode_close_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni)
1098 {
1099 int res;
1100
1101 res = ntfs_inode_sync_in_dir(ni, dir_ni);
1102 if (res) {
1103 if (errno != EIO)
1104 errno = EBUSY;
1105 } else
1106 res = ntfs_inode_close(ni);
1107 return (res);
1108 }
1109
1110 /**
1111 * ntfs_inode_add_attrlist - add attribute list to inode and fill it
1112 * @ni: opened ntfs inode to which add attribute list
1113 *
1114 * Return 0 on success or -1 on error with errno set to the error code.
1115 * The following error codes are defined:
1116 * EINVAL - Invalid arguments were passed to the function.
1117 * EEXIST - Attribute list already exist.
1118 * EIO - Input/Ouput error occurred.
1119 * ENOMEM - Not enough memory to perform add.
1120 */
ntfs_inode_add_attrlist(ntfs_inode * ni)1121 int ntfs_inode_add_attrlist(ntfs_inode *ni)
1122 {
1123 int err;
1124 ntfs_attr_search_ctx *ctx;
1125 u8 *al = NULL, *aln;
1126 int al_len = 0;
1127 ATTR_LIST_ENTRY *ale = NULL;
1128 ntfs_attr *na;
1129
1130 if (!ni) {
1131 errno = EINVAL;
1132 ntfs_log_perror("%s", __FUNCTION__);
1133 return -1;
1134 }
1135
1136 ntfs_log_trace("inode %llu\n", (unsigned long long) ni->mft_no);
1137
1138 if (NInoAttrList(ni) || ni->nr_extents) {
1139 errno = EEXIST;
1140 ntfs_log_perror("Inode already has attribute list");
1141 return -1;
1142 }
1143
1144 /* Form attribute list. */
1145 ctx = ntfs_attr_get_search_ctx(ni, NULL);
1146 if (!ctx) {
1147 err = errno;
1148 goto err_out;
1149 }
1150 /* Walk through all attributes. */
1151 while (!ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx)) {
1152
1153 int ale_size;
1154
1155 if (ctx->attr->type == AT_ATTRIBUTE_LIST) {
1156 err = EIO;
1157 ntfs_log_perror("Attribute list already present");
1158 goto put_err_out;
1159 }
1160
1161 ale_size = (sizeof(ATTR_LIST_ENTRY) + sizeof(ntfschar) *
1162 ctx->attr->name_length + 7) & ~7;
1163 al_len += ale_size;
1164
1165 aln = realloc(al, al_len);
1166 if (!aln) {
1167 err = errno;
1168 ntfs_log_perror("Failed to realloc %d bytes", al_len);
1169 goto put_err_out;
1170 }
1171 ale = (ATTR_LIST_ENTRY *)(aln + ((u8 *)ale - al));
1172 al = aln;
1173
1174 memset(ale, 0, ale_size);
1175
1176 /* Add attribute to attribute list. */
1177 ale->type = ctx->attr->type;
1178 ale->length = cpu_to_le16((sizeof(ATTR_LIST_ENTRY) +
1179 sizeof(ntfschar) * ctx->attr->name_length + 7) & ~7);
1180 ale->name_length = ctx->attr->name_length;
1181 ale->name_offset = (u8 *)ale->name - (u8 *)ale;
1182 if (ctx->attr->non_resident)
1183 ale->lowest_vcn = ctx->attr->lowest_vcn;
1184 else
1185 ale->lowest_vcn = const_cpu_to_sle64(0);
1186 ale->mft_reference = MK_LE_MREF(ni->mft_no,
1187 le16_to_cpu(ni->mrec->sequence_number));
1188 ale->instance = ctx->attr->instance;
1189 memcpy(ale->name, (u8 *)ctx->attr +
1190 le16_to_cpu(ctx->attr->name_offset),
1191 ctx->attr->name_length * sizeof(ntfschar));
1192 ale = (ATTR_LIST_ENTRY *)(al + al_len);
1193 }
1194 /* Check for real error occurred. */
1195 if (errno != ENOENT) {
1196 err = errno;
1197 ntfs_log_perror("%s: Attribute lookup failed, inode %lld",
1198 __FUNCTION__, (long long)ni->mft_no);
1199 goto put_err_out;
1200 }
1201
1202 /* Set in-memory attribute list. */
1203 ni->attr_list = al;
1204 ni->attr_list_size = al_len;
1205 NInoSetAttrList(ni);
1206 NInoAttrListSetDirty(ni);
1207
1208 /* Free space if there is not enough it for $ATTRIBUTE_LIST. */
1209 if (le32_to_cpu(ni->mrec->bytes_allocated) -
1210 le32_to_cpu(ni->mrec->bytes_in_use) <
1211 offsetof(ATTR_RECORD, resident_end)) {
1212 if (ntfs_inode_free_space(ni,
1213 offsetof(ATTR_RECORD, resident_end))) {
1214 /* Failed to free space. */
1215 err = errno;
1216 ntfs_log_perror("Failed to free space for attrlist");
1217 goto rollback;
1218 }
1219 }
1220
1221 /* Add $ATTRIBUTE_LIST to mft record. */
1222 if (ntfs_resident_attr_record_add(ni,
1223 AT_ATTRIBUTE_LIST, NULL, 0, NULL, 0, const_cpu_to_le16(0)) < 0) {
1224 err = errno;
1225 ntfs_log_perror("Couldn't add $ATTRIBUTE_LIST to MFT");
1226 goto rollback;
1227 }
1228
1229 /* Resize it. */
1230 na = ntfs_attr_open(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0);
1231 if (!na) {
1232 err = errno;
1233 ntfs_log_perror("Failed to open just added $ATTRIBUTE_LIST");
1234 goto remove_attrlist_record;
1235 }
1236 if (ntfs_attr_truncate(na, al_len)) {
1237 err = errno;
1238 ntfs_log_perror("Failed to resize just added $ATTRIBUTE_LIST");
1239 ntfs_attr_close(na);
1240 goto remove_attrlist_record;;
1241 }
1242
1243 ntfs_attr_put_search_ctx(ctx);
1244 ntfs_attr_close(na);
1245 return 0;
1246
1247 remove_attrlist_record:
1248 /* Prevent ntfs_attr_recorm_rm from freeing attribute list. */
1249 ni->attr_list = NULL;
1250 NInoClearAttrList(ni);
1251 /* Remove $ATTRIBUTE_LIST record. */
1252 ntfs_attr_reinit_search_ctx(ctx);
1253 if (!ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0,
1254 CASE_SENSITIVE, 0, NULL, 0, ctx)) {
1255 if (ntfs_attr_record_rm(ctx))
1256 ntfs_log_perror("Rollback failed to remove attrlist");
1257 } else
1258 ntfs_log_perror("Rollback failed to find attrlist");
1259 /* Setup back in-memory runlist. */
1260 ni->attr_list = al;
1261 ni->attr_list_size = al_len;
1262 NInoSetAttrList(ni);
1263 rollback:
1264 /*
1265 * Scan attribute list for attributes that placed not in the base MFT
1266 * record and move them to it.
1267 */
1268 ntfs_attr_reinit_search_ctx(ctx);
1269 ale = (ATTR_LIST_ENTRY*)al;
1270 while ((u8*)ale < al + al_len) {
1271 if (MREF_LE(ale->mft_reference) != ni->mft_no) {
1272 if (!ntfs_attr_lookup(ale->type, ale->name,
1273 ale->name_length,
1274 CASE_SENSITIVE,
1275 sle64_to_cpu(ale->lowest_vcn),
1276 NULL, 0, ctx)) {
1277 if (ntfs_attr_record_move_to(ctx, ni))
1278 ntfs_log_perror("Rollback failed to "
1279 "move attribute");
1280 } else
1281 ntfs_log_perror("Rollback failed to find attr");
1282 ntfs_attr_reinit_search_ctx(ctx);
1283 }
1284 ale = (ATTR_LIST_ENTRY*)((u8*)ale + le16_to_cpu(ale->length));
1285 }
1286 /* Remove in-memory attribute list. */
1287 ni->attr_list = NULL;
1288 ni->attr_list_size = 0;
1289 NInoClearAttrList(ni);
1290 NInoAttrListClearDirty(ni);
1291 put_err_out:
1292 ntfs_attr_put_search_ctx(ctx);
1293 err_out:
1294 free(al);
1295 errno = err;
1296 return -1;
1297 }
1298
1299 /**
1300 * ntfs_inode_free_space - free space in the MFT record of an inode
1301 * @ni: ntfs inode in which MFT record needs more free space
1302 * @size: amount of space needed to free
1303 *
1304 * Return 0 on success or -1 on error with errno set to the error code.
1305 */
ntfs_inode_free_space(ntfs_inode * ni,int size)1306 int ntfs_inode_free_space(ntfs_inode *ni, int size)
1307 {
1308 ntfs_attr_search_ctx *ctx;
1309 int freed;
1310
1311 if (!ni || size < 0) {
1312 errno = EINVAL;
1313 ntfs_log_perror("%s: ni=%p size=%d", __FUNCTION__, ni, size);
1314 return -1;
1315 }
1316
1317 ntfs_log_trace("Entering for inode %lld, size %d\n",
1318 (unsigned long long)ni->mft_no, size);
1319
1320 freed = (le32_to_cpu(ni->mrec->bytes_allocated) -
1321 le32_to_cpu(ni->mrec->bytes_in_use));
1322
1323 if (size <= freed)
1324 return 0;
1325
1326 ctx = ntfs_attr_get_search_ctx(ni, NULL);
1327 if (!ctx)
1328 return -1;
1329 /*
1330 * $STANDARD_INFORMATION and $ATTRIBUTE_LIST must stay in the base MFT
1331 * record, so position search context on the first attribute after them.
1332 */
1333 if (ntfs_attr_position(AT_FILE_NAME, ctx))
1334 goto put_err_out;
1335
1336 while (1) {
1337 int record_size;
1338 /*
1339 * Check whether attribute is from different MFT record. If so,
1340 * find next, because we don't need such.
1341 */
1342 while (ctx->ntfs_ino->mft_no != ni->mft_no) {
1343 retry:
1344 if (ntfs_attr_position(AT_UNUSED, ctx))
1345 goto put_err_out;
1346 }
1347
1348 if (ntfs_inode_base(ctx->ntfs_ino)->mft_no == FILE_MFT &&
1349 ctx->attr->type == AT_DATA)
1350 goto retry;
1351
1352 if (ctx->attr->type == AT_INDEX_ROOT)
1353 goto retry;
1354
1355 record_size = le32_to_cpu(ctx->attr->length);
1356
1357 if (ntfs_attr_record_move_away(ctx, 0)) {
1358 ntfs_log_perror("Failed to move out attribute #2");
1359 break;
1360 }
1361 freed += record_size;
1362
1363 /* Check whether we are done. */
1364 if (size <= freed) {
1365 ntfs_attr_put_search_ctx(ctx);
1366 return 0;
1367 }
1368 /*
1369 * Reposition to first attribute after $STANDARD_INFORMATION
1370 * and $ATTRIBUTE_LIST instead of simply skipping this attribute
1371 * because in the case when we have got only in-memory attribute
1372 * list then ntfs_attr_lookup will fail when it tries to find
1373 * $ATTRIBUTE_LIST.
1374 */
1375 ntfs_attr_reinit_search_ctx(ctx);
1376 if (ntfs_attr_position(AT_FILE_NAME, ctx))
1377 break;
1378 }
1379 put_err_out:
1380 ntfs_attr_put_search_ctx(ctx);
1381 if (errno == ENOSPC)
1382 ntfs_log_trace("No attributes left that could be moved out.\n");
1383 return -1;
1384 }
1385
1386 /**
1387 * ntfs_inode_update_times - update selected time fields for ntfs inode
1388 * @ni: ntfs inode for which update time fields
1389 * @mask: select which time fields should be updated
1390 *
1391 * This function updates time fields to current time. Fields to update are
1392 * selected using @mask (see enum @ntfs_time_update_flags for posssible values).
1393 */
ntfs_inode_update_times(ntfs_inode * ni,ntfs_time_update_flags mask)1394 void ntfs_inode_update_times(ntfs_inode *ni, ntfs_time_update_flags mask)
1395 {
1396 ntfs_time now;
1397
1398 if (!ni) {
1399 ntfs_log_error("%s(): Invalid arguments.\n", __FUNCTION__);
1400 return;
1401 }
1402
1403 if ((ni->mft_no < FILE_first_user && ni->mft_no != FILE_root) ||
1404 NVolReadOnly(ni->vol) || !mask)
1405 return;
1406
1407 now = ntfs_current_time();
1408 if (mask & NTFS_UPDATE_ATIME)
1409 ni->last_access_time = now;
1410 if (mask & NTFS_UPDATE_MTIME)
1411 ni->last_data_change_time = now;
1412 if (mask & NTFS_UPDATE_CTIME)
1413 ni->last_mft_change_time = now;
1414
1415 NInoFileNameSetDirty(ni);
1416 NInoSetDirty(ni);
1417 }
1418
1419 /**
1420 * ntfs_inode_badclus_bad - check for $Badclus:$Bad data attribute
1421 * @mft_no: mft record number where @attr is present
1422 * @attr: attribute record used to check for the $Bad attribute
1423 *
1424 * Check if the mft record given by @mft_no and @attr contains the bad sector
1425 * list. Please note that mft record numbers describing $Badclus extent inodes
1426 * will not match the current $Badclus:$Bad check.
1427 *
1428 * On success return 1 if the file is $Badclus:$Bad, otherwise return 0.
1429 * On error return -1 with errno set to the error code.
1430 */
ntfs_inode_badclus_bad(u64 mft_no,ATTR_RECORD * attr)1431 int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *attr)
1432 {
1433 int len, ret = 0;
1434 ntfschar *ustr;
1435
1436 if (!attr) {
1437 ntfs_log_error("Invalid argument.\n");
1438 errno = EINVAL;
1439 return -1;
1440 }
1441
1442 if (mft_no != FILE_BadClus)
1443 return 0;
1444
1445 if (attr->type != AT_DATA)
1446 return 0;
1447
1448 if ((ustr = ntfs_str2ucs("$Bad", &len)) == NULL) {
1449 ntfs_log_perror("Couldn't convert '$Bad' to Unicode");
1450 return -1;
1451 }
1452
1453 if (ustr && ntfs_names_are_equal(ustr, len,
1454 (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)),
1455 attr->name_length, 0, NULL, 0))
1456 ret = 1;
1457
1458 ntfs_ucsfree(ustr);
1459
1460 return ret;
1461 }
1462
1463 /*
1464 * Get high precision NTFS times
1465 *
1466 * They are returned in following order : create, update, access, change
1467 * provided they fit in requested size.
1468 *
1469 * Returns the modified size if successfull (or 32 if buffer size is null)
1470 * -errno if failed
1471 */
1472
ntfs_inode_get_times(ntfs_inode * ni,char * value,size_t size)1473 int ntfs_inode_get_times(ntfs_inode *ni, char *value, size_t size)
1474 {
1475 ntfs_attr_search_ctx *ctx;
1476 STANDARD_INFORMATION *std_info;
1477 u64 *times;
1478 int ret;
1479
1480 ret = 0;
1481 ctx = ntfs_attr_get_search_ctx(ni, NULL);
1482 if (ctx) {
1483 if (ntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED,
1484 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
1485 ntfs_log_perror("Failed to get standard info (inode %lld)",
1486 (long long)ni->mft_no);
1487 } else {
1488 std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr +
1489 le16_to_cpu(ctx->attr->value_offset));
1490 if (value && (size >= 8)) {
1491 times = (u64*)value;
1492 times[0] = sle64_to_cpu(std_info->creation_time);
1493 ret = 8;
1494 if (size >= 16) {
1495 times[1] = sle64_to_cpu(std_info->last_data_change_time);
1496 ret = 16;
1497 }
1498 if (size >= 24) {
1499 times[2] = sle64_to_cpu(std_info->last_access_time);
1500 ret = 24;
1501 }
1502 if (size >= 32) {
1503 times[3] = sle64_to_cpu(std_info->last_mft_change_time);
1504 ret = 32;
1505 }
1506 } else
1507 if (!size)
1508 ret = 32;
1509 else
1510 ret = -ERANGE;
1511 }
1512 ntfs_attr_put_search_ctx(ctx);
1513 }
1514 return (ret ? ret : -errno);
1515 }
1516
1517 /*
1518 * Set high precision NTFS times
1519 *
1520 * They are expected in this order : create, update, access
1521 * provided they are present in input. The change time is set to
1522 * current time.
1523 *
1524 * The times are inserted directly in the standard_information and
1525 * file names attributes to avoid manipulating low precision times
1526 *
1527 * Returns 0 if success
1528 * -1 if there were an error (described by errno)
1529 */
1530
ntfs_inode_set_times(ntfs_inode * ni,const char * value,size_t size,int flags)1531 int ntfs_inode_set_times(ntfs_inode *ni, const char *value, size_t size,
1532 int flags)
1533 {
1534 ntfs_attr_search_ctx *ctx;
1535 STANDARD_INFORMATION *std_info;
1536 FILE_NAME_ATTR *fn;
1537 u64 times[4];
1538 ntfs_time now;
1539 int cnt;
1540 int ret;
1541
1542 ret = -1;
1543 if ((size >= 8) && !(flags & XATTR_CREATE)) {
1544 /* Copy, to avoid alignment issue encountered on ARM */
1545 memcpy(times, value,
1546 (size < sizeof(times) ? size : sizeof(times)));
1547 now = ntfs_current_time();
1548 /* update the standard information attribute */
1549 ctx = ntfs_attr_get_search_ctx(ni, NULL);
1550 if (ctx) {
1551 if (ntfs_attr_lookup(AT_STANDARD_INFORMATION,
1552 AT_UNNAMED, 0, CASE_SENSITIVE,
1553 0, NULL, 0, ctx)) {
1554 ntfs_log_perror("Failed to get standard info (inode %lld)",
1555 (long long)ni->mft_no);
1556 } else {
1557 std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr +
1558 le16_to_cpu(ctx->attr->value_offset));
1559 /*
1560 * Mark times set to avoid overwriting
1561 * them when the inode is closed.
1562 * The inode structure must also be updated
1563 * (with loss of precision) because of cacheing.
1564 * TODO : use NTFS precision in inode, and
1565 * return sub-second times in getattr()
1566 */
1567 set_nino_flag(ni, TimesSet);
1568 std_info->creation_time = cpu_to_sle64(times[0]);
1569 ni->creation_time
1570 = std_info->creation_time;
1571 if (size >= 16) {
1572 std_info->last_data_change_time = cpu_to_sle64(times[1]);
1573 ni->last_data_change_time
1574 = std_info->last_data_change_time;
1575 }
1576 if (size >= 24) {
1577 std_info->last_access_time = cpu_to_sle64(times[2]);
1578 ni->last_access_time
1579 = std_info->last_access_time;
1580 }
1581 std_info->last_mft_change_time = now;
1582 ni->last_mft_change_time = now;
1583 ntfs_inode_mark_dirty(ctx->ntfs_ino);
1584 NInoFileNameSetDirty(ni);
1585
1586 /* update the file names attributes */
1587 ntfs_attr_reinit_search_ctx(ctx);
1588 cnt = 0;
1589 while (!ntfs_attr_lookup(AT_FILE_NAME,
1590 AT_UNNAMED, 0, CASE_SENSITIVE,
1591 0, NULL, 0, ctx)) {
1592 fn = (FILE_NAME_ATTR*)((u8 *)ctx->attr +
1593 le16_to_cpu(ctx->attr->value_offset));
1594 fn->creation_time
1595 = cpu_to_sle64(times[0]);
1596 if (size >= 16)
1597 fn->last_data_change_time
1598 = cpu_to_sle64(times[1]);
1599 if (size >= 24)
1600 fn->last_access_time
1601 = cpu_to_sle64(times[2]);
1602 fn->last_mft_change_time = now;
1603 cnt++;
1604 }
1605 if (cnt)
1606 ret = 0;
1607 else {
1608 ntfs_log_perror("Failed to get file names (inode %lld)",
1609 (long long)ni->mft_no);
1610 }
1611 }
1612 ntfs_attr_put_search_ctx(ctx);
1613 }
1614 } else
1615 if (size < 8)
1616 errno = ERANGE;
1617 else
1618 errno = EEXIST;
1619 return (ret);
1620 }
1621