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