/** * ntfs_inode_update_times - update selected time fields for ntfs inode * @ni: ntfs inode for which update time fields * @mask: select which time fields should be updated * * This function updates time fields to current time. Fields to update are * selected using @mask (see enum @ntfs_time_update_flags for posssible values). */ void ntfs_inode_update_times(ntfs_inode *ni, ntfs_time_update_flags mask) { time_t now; if (!ni) { ntfs_log_error("%s(): Invalid arguments.\n", __FUNCTION__); return; } if ((ni->mft_no < FILE_first_user && ni->mft_no != FILE_root) || NVolReadOnly(ni->vol) || !mask) return; now = time(NULL); if (mask & NTFS_UPDATE_ATIME) ni->last_access_time = now; if (mask & NTFS_UPDATE_MTIME) ni->last_data_change_time = now; if (mask & NTFS_UPDATE_CTIME) ni->last_mft_change_time = now; set_nino_flag(ni, TimesDirty); NInoFileNameSetDirty(ni); NInoSetDirty(ni); }
status_t fs_remove_attrib(fs_volume *_vol, fs_vnode *_node, const char* name) { nspace *ns = (nspace *)_vol->private_volume; vnode *node = (vnode *)_node->private_node; char ntfs_attr_name[MAX_PATH]={0}; ntfschar *uname = NULL; int ulen; ntfs_inode *ni = NULL; status_t result = B_NO_ERROR; TRACE("%s - ENTER - name: [%s]\n", __FUNCTION__, name); if (ns->flags & B_FS_IS_READONLY) { ERROR("ntfs is read-only\n"); return B_READ_ONLY_DEVICE; } LOCK_VOL(ns); if (node == NULL) { result = EINVAL; goto exit; } ni = ntfs_inode_open(ns->ntvol, node->vnid); if (ni == NULL) { result = errno; goto exit; } strcat(ntfs_attr_name, kHaikuAttrPrefix); strcat(ntfs_attr_name, name); uname = ntfs_calloc(MAX_PATH); ulen = ntfs_mbstoucs(ntfs_attr_name, &uname); if (ulen < 0) { result = EILSEQ; goto exit; } if (ntfs_attr_remove(ni, AT_DATA, uname, ulen)) { result = ENOENT; goto exit; } if (!(ni->flags & FILE_ATTR_ARCHIVE)) { ni->flags |= FILE_ATTR_ARCHIVE; NInoFileNameSetDirty(ni); } notify_attribute_changed(ns->id, MREF(ni->mft_no), name, B_ATTR_REMOVED); exit: if (uname != NULL) free(uname); if (ni != NULL) ntfs_inode_close(ni); TRACE("%s - EXIT, result is %s\n", __FUNCTION__, strerror(result)); UNLOCK_VOL(ns); return result; }
static int ntfs_fallocate(ntfs_inode *ni, s64 alloc_offs, s64 alloc_len) { s64 allocated_size; s64 data_size; ntfs_attr_search_ctx *ctx; ntfs_attr *na; runlist_element *oldrl; const char *errmess; int save_errno; int err; err = 0; /* Open the specified attribute. */ na = ntfs_attr_open(ni, attr_type, attr_name, attr_name_len); if (!na) { ntfs_log_perror("Failed to open attribute 0x%lx: ", (unsigned long)le32_to_cpu(attr_type)); err = -1; } else { errmess = (const char*)NULL; if (na->data_flags & ATTR_IS_COMPRESSED) { errmess= "Cannot fallocate a compressed file"; } /* Locate the attribute record, needed for updating sizes */ ctx = ntfs_attr_get_search_ctx(ni, NULL); if (!ctx) { errmess = "Failed to allocate a search context"; } if (errmess) { ntfs_log_error("%s\n",errmess); err = -1; } else { /* Get and save the initial allocations */ allocated_size = na->allocated_size; data_size = ni->data_size; err = ntfs_attr_map_whole_runlist(na); if (!err) { oldrl = ntfs_save_rl(na->rl); if (oldrl) { err = ntfs_full_allocation(na, ctx, alloc_offs, alloc_len); if (err) { save_errno = errno; ni->allocated_size = allocated_size; ni->data_size = data_size; ntfs_restore_rl(na, oldrl); errno = save_errno; } else { free(oldrl); /* Mark file name dirty, to update the sizes in directories */ NInoFileNameSetDirty(ni); NInoSetDirty(ni); } } else err = -1; } ntfs_attr_put_search_ctx(ctx); } /* Close the attribute. */ ntfs_attr_close(na); } return (err); }
/** * ntfs_inode_sync - write the inode (and its dirty extents) to disk * @ni: ntfs inode to write * * Write the inode @ni to disk as well as its dirty extent inodes if such * exist and @ni is a base inode. If @ni is an extent inode, only @ni is * written completely disregarding its base inode and any other extent inodes. * * For a base inode with dirty extent inodes if any writes fail for whatever * reason, the failing inode is skipped and the sync process is continued. At * the end the error condition that brought about the failure is returned. Thus * the smallest amount of data loss possible occurs. * * Return 0 on success or -1 on error with errno set to the error code. * The following error codes are defined: * EINVAL - Invalid arguments were passed to the function. * EBUSY - Inode and/or one of its extents is busy, try again later. * EIO - I/O error while writing the inode (or one of its extents). */ int ntfs_inode_sync(ntfs_inode *ni) { int ret = 0; int err = 0; if (!ni) { errno = EINVAL; ntfs_log_error("Failed to sync NULL inode\n"); return -1; } ntfs_log_enter("Entering for inode %lld\n", (long long)ni->mft_no); /* Update STANDARD_INFORMATION. */ if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 && ntfs_inode_sync_standard_information(ni)) { if (!err || errno == EIO) { err = errno; if (err != EIO) err = EBUSY; } } /* Update FILE_NAME's in the index. */ if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 && NInoFileNameTestAndClearDirty(ni) && ntfs_inode_sync_file_name(ni)) { if (!err || errno == EIO) { err = errno; if (err != EIO) err = EBUSY; } ntfs_log_perror("Failed to sync FILE_NAME (inode %lld)", (long long)ni->mft_no); NInoFileNameSetDirty(ni); } /* Write out attribute list from cache to disk. */ if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 && NInoAttrList(ni) && NInoAttrListTestAndClearDirty(ni)) { ntfs_attr *na; na = ntfs_attr_open(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0); if (!na) { if (!err || errno == EIO) { err = errno; if (err != EIO) err = EBUSY; ntfs_log_perror("Attribute list sync failed " "(open, inode %lld)", (long long)ni->mft_no); } NInoAttrListSetDirty(ni); goto sync_inode; } if (na->data_size == ni->attr_list_size) { if (ntfs_attr_pwrite(na, 0, ni->attr_list_size, ni->attr_list) != ni->attr_list_size) { if (!err || errno == EIO) { err = errno; if (err != EIO) err = EBUSY; ntfs_log_perror("Attribute list sync " "failed (write, inode %lld)", (long long)ni->mft_no); } NInoAttrListSetDirty(ni); } } else { err = EIO; ntfs_log_error("Attribute list sync failed (bad size, " "inode %lld)\n", (long long)ni->mft_no); NInoAttrListSetDirty(ni); } ntfs_attr_close(na); } sync_inode: /* Write this inode out to the $MFT (and $MFTMirr if applicable). */ if (NInoTestAndClearDirty(ni)) { if (ntfs_mft_record_write(ni->vol, ni->mft_no, ni->mrec)) { if (!err || errno == EIO) { err = errno; if (err != EIO) err = EBUSY; } NInoSetDirty(ni); ntfs_log_perror("MFT record sync failed, inode %lld", (long long)ni->mft_no); } } /* If this is a base inode with extents write all dirty extents, too. */ if (ni->nr_extents > 0) { s32 i; for (i = 0; i < ni->nr_extents; ++i) { ntfs_inode *eni; eni = ni->extent_nis[i]; if (!NInoTestAndClearDirty(eni)) continue; if (ntfs_mft_record_write(eni->vol, eni->mft_no, eni->mrec)) { if (!err || errno == EIO) { err = errno; if (err != EIO) err = EBUSY; } NInoSetDirty(eni); ntfs_log_perror("Extent MFT record sync failed," " inode %lld/%lld", (long long)ni->mft_no, (long long)eni->mft_no); } } } if (err) { errno = err; ret = -1; } ntfs_log_leave("\n"); return ret; }
int ntfs_inode_set_times(ntfs_inode *ni, const char *value, size_t size, int flags) { ntfs_attr_search_ctx *ctx; STANDARD_INFORMATION *std_info; FILE_NAME_ATTR *fn; const u64 *times; ntfs_time now; int cnt; int ret; ret = -1; if ((size >= 8) && !(flags & XATTR_CREATE)) { times = (const u64*)value; now = ntfs_current_time(); /* update the standard information attribute */ ctx = ntfs_attr_get_search_ctx(ni, NULL); if (ctx) { if (ntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED, 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { ntfs_log_perror("Failed to get standard info (inode %lld)", (long long)ni->mft_no); } else { std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->value_offset)); /* * Mark times set to avoid overwriting * them when the inode is closed. * The inode structure must also be updated * (with loss of precision) because of cacheing. * TODO : use NTFS precision in inode, and * return sub-second times in getattr() */ set_nino_flag(ni, TimesSet); std_info->creation_time = cpu_to_le64(times[0]); ni->creation_time = std_info->creation_time; if (size >= 16) { std_info->last_data_change_time = cpu_to_le64(times[1]); ni->last_data_change_time = std_info->last_data_change_time; } if (size >= 24) { std_info->last_access_time = cpu_to_le64(times[2]); ni->last_access_time = std_info->last_access_time; } std_info->last_mft_change_time = now; ni->last_mft_change_time = now; ntfs_inode_mark_dirty(ctx->ntfs_ino); NInoFileNameSetDirty(ni); /* update the file names attributes */ ntfs_attr_reinit_search_ctx(ctx); cnt = 0; while (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { fn = (FILE_NAME_ATTR*)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->value_offset)); fn->creation_time = cpu_to_le64(times[0]); if (size >= 16) fn->last_data_change_time = cpu_to_le64(times[1]); if (size >= 24) fn->last_access_time = cpu_to_le64(times[2]); fn->last_mft_change_time = now; cnt++; } if (cnt) ret = 0; else { ntfs_log_perror("Failed to get file names (inode %lld)", (long long)ni->mft_no); } } ntfs_attr_put_search_ctx(ctx); } } else if (size < 8) errno = ERANGE; else errno = EEXIST; return (ret); }
int ntfs_remove_ntfs_reparse_data(ntfs_inode *ni) { int res; int olderrno; ntfs_attr *na; ntfs_inode *xrni; ntfs_index_context *xr; le32 reparse_tag; res = 0; if (ni) { /* * open and delete the reparse data */ na = ntfs_attr_open(ni, AT_REPARSE_POINT, AT_UNNAMED,0); if (na) { /* first remove index (reparse data needed) */ xr = open_reparse_index(ni->vol); if (xr) { if (remove_reparse_index(na,xr, &reparse_tag) < 0) { res = -1; } else { /* now remove attribute */ res = ntfs_attr_rm(na); if (!res) { ni->flags &= ~FILE_ATTR_REPARSE_POINT; NInoFileNameSetDirty(ni); } else { /* * If we could not remove the * attribute, try to restore the * index and log the error. There * will be an inconsistency if * the reindexing fails. */ set_reparse_index(ni, xr, reparse_tag); ntfs_log_error( "Failed to remove reparse data." " Possible corruption.\n"); } } xrni = xr->ni; ntfs_index_entry_mark_dirty(xr); NInoSetDirty(xrni); ntfs_index_ctx_put(xr); ntfs_inode_close(xrni); } olderrno = errno; ntfs_attr_close(na); /* avoid errno pollution */ if (errno == ENOENT) errno = olderrno; } else { errno = ENODATA; res = -1; } NInoSetDirty(ni); } else { errno = EINVAL; res = -1; } return (res ? -1 : 0); }
int ntfs_set_ntfs_reparse_data(ntfs_inode *ni, const char *value, size_t size, int flags) { int res; u8 dummy; ntfs_inode *xrni; ntfs_index_context *xr; res = 0; if (ni && valid_reparse_data(ni, (const REPARSE_POINT*)value, size)) { xr = open_reparse_index(ni->vol); if (xr) { if (!ntfs_attr_exist(ni,AT_REPARSE_POINT, AT_UNNAMED,0)) { if (!(flags & XATTR_REPLACE)) { /* * no reparse data attribute : add one, * apparently, this does not feed the new value in * Note : NTFS version must be >= 3 */ if (ni->vol->major_ver >= 3) { res = ntfs_attr_add(ni, AT_REPARSE_POINT, AT_UNNAMED,0,&dummy, (s64)0); if (!res) { ni->flags |= FILE_ATTR_REPARSE_POINT; NInoFileNameSetDirty(ni); } NInoSetDirty(ni); } else { errno = EOPNOTSUPP; res = -1; } } else { errno = ENODATA; res = -1; } } else { if (flags & XATTR_CREATE) { errno = EEXIST; res = -1; } } if (!res) { /* update value and index */ res = update_reparse_data(ni,xr,value,size); } xrni = xr->ni; ntfs_index_entry_mark_dirty(xr); NInoSetDirty(xrni); ntfs_index_ctx_put(xr); ntfs_inode_close(xrni); } else { res = -1; } } else { errno = EINVAL; res = -1; } return (res ? -1 : 0); }