| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <linux/iversion.h> |
| 7 |
#include <linux/namei.h> |
| 8 |
#include <linux/slab.h> |
| 9 |
#include <linux/buffer_head.h> |
| 10 |
#include <linux/nls.h> |
| 11 |
|
| 12 |
#include "exfat_raw.h" |
| 13 |
#include "exfat_fs.h" |
| 14 |
|
| 15 |
static inline unsigned long exfat_d_version(struct dentry *dentry) |
| 16 |
{ |
| 17 |
return (unsigned long) dentry->d_fsdata; |
| 18 |
} |
| 19 |
|
| 20 |
static inline void exfat_d_version_set(struct dentry *dentry, |
| 21 |
unsigned long version) |
| 22 |
{ |
| 23 |
dentry->d_fsdata = (void *) version; |
| 24 |
} |
| 25 |
|
| 26 |
/* |
| 27 |
* If new entry was created in the parent, it could create the 8.3 alias (the |
| 28 |
* shortname of logname). So, the parent may have the negative-dentry which |
| 29 |
* matches the created 8.3 alias. |
| 30 |
* |
| 31 |
* If it happened, the negative dentry isn't actually negative anymore. So, |
| 32 |
* drop it. |
| 33 |
*/ |
| 34 |
static int exfat_d_revalidate(struct inode *dir, const struct qstr *name, |
| 35 |
struct dentry *dentry, unsigned int flags) |
| 36 |
{ |
| 37 |
if (flags & LOOKUP_RCU) |
| 38 |
return -ECHILD; |
| 39 |
|
| 40 |
/* |
| 41 |
* This is not negative dentry. Always valid. |
| 42 |
* |
| 43 |
* Note, rename() to existing directory entry will have ->d_inode, and |
| 44 |
* will use existing name which isn't specified name by user. |
| 45 |
* |
| 46 |
* We may be able to drop this positive dentry here. But dropping |
| 47 |
* positive dentry isn't good idea. So it's unsupported like |
| 48 |
* rename("filename", "FILENAME") for now. |
| 49 |
*/ |
| 50 |
if (d_really_is_positive(dentry)) |
| 51 |
return 1; |
| 52 |
|
| 53 |
/* |
| 54 |
* Drop the negative dentry, in order to make sure to use the case |
| 55 |
* sensitive name which is specified by user if this is for creation. |
| 56 |
*/ |
| 57 |
if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) |
| 58 |
return 0; |
| 59 |
|
| 60 |
return inode_eq_iversion(dir, exfat_d_version(dentry)); |
| 61 |
} |
| 62 |
|
| 63 |
/* returns the length of a struct qstr, ignoring trailing dots if necessary */ |
| 64 |
static unsigned int exfat_striptail_len(unsigned int len, const char *name, |
| 65 |
bool keep_last_dots) |
| 66 |
{ |
| 67 |
if (!keep_last_dots) { |
| 68 |
while (len && name[len - 1] == '.') |
| 69 |
len--; |
| 70 |
} |
| 71 |
return len; |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* Compute the hash for the exfat name corresponding to the dentry. If the name |
| 76 |
* is invalid, we leave the hash code unchanged so that the existing dentry can |
| 77 |
* be used. The exfat fs routines will return ENOENT or EINVAL as appropriate. |
| 78 |
*/ |
| 79 |
static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr) |
| 80 |
{ |
| 81 |
struct super_block *sb = dentry->d_sb; |
| 82 |
struct nls_table *t = EXFAT_SB(sb)->nls_io; |
| 83 |
const unsigned char *name = qstr->name; |
| 84 |
unsigned int len = exfat_striptail_len(qstr->len, qstr->name, |
| 85 |
EXFAT_SB(sb)->options.keep_last_dots); |
| 86 |
unsigned long hash = init_name_hash(dentry); |
| 87 |
int i, charlen; |
| 88 |
wchar_t c; |
| 89 |
|
| 90 |
for (i = 0; i < len; i += charlen) { |
| 91 |
charlen = t->char2uni(&name[i], len - i, &c); |
| 92 |
if (charlen < 0) |
| 93 |
return charlen; |
| 94 |
hash = partial_name_hash(exfat_toupper(sb, c), hash); |
| 95 |
} |
| 96 |
|
| 97 |
qstr->hash = end_name_hash(hash); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
static int exfat_d_cmp(const struct dentry *dentry, unsigned int len, |
| 102 |
const char *str, const struct qstr *name) |
| 103 |
{ |
| 104 |
struct super_block *sb = dentry->d_sb; |
| 105 |
struct nls_table *t = EXFAT_SB(sb)->nls_io; |
| 106 |
unsigned int alen = exfat_striptail_len(name->len, name->name, |
| 107 |
EXFAT_SB(sb)->options.keep_last_dots); |
| 108 |
unsigned int blen = exfat_striptail_len(len, str, |
| 109 |
EXFAT_SB(sb)->options.keep_last_dots); |
| 110 |
wchar_t c1, c2; |
| 111 |
int charlen, i; |
| 112 |
|
| 113 |
if (alen != blen) |
| 114 |
return 1; |
| 115 |
|
| 116 |
for (i = 0; i < len; i += charlen) { |
| 117 |
charlen = t->char2uni(&name->name[i], alen - i, &c1); |
| 118 |
if (charlen < 0) |
| 119 |
return 1; |
| 120 |
if (charlen != t->char2uni(&str[i], blen - i, &c2)) |
| 121 |
return 1; |
| 122 |
|
| 123 |
if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2)) |
| 124 |
return 1; |
| 125 |
} |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
const struct dentry_operations exfat_dentry_ops = { |
| 131 |
.d_revalidate = exfat_d_revalidate, |
| 132 |
.d_hash = exfat_d_hash, |
| 133 |
.d_compare = exfat_d_cmp, |
| 134 |
}; |
| 135 |
|
| 136 |
static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr) |
| 137 |
{ |
| 138 |
struct super_block *sb = dentry->d_sb; |
| 139 |
const unsigned char *name = qstr->name; |
| 140 |
unsigned int len = exfat_striptail_len(qstr->len, qstr->name, |
| 141 |
EXFAT_SB(sb)->options.keep_last_dots); |
| 142 |
unsigned long hash = init_name_hash(dentry); |
| 143 |
int i, charlen; |
| 144 |
unicode_t u; |
| 145 |
|
| 146 |
for (i = 0; i < len; i += charlen) { |
| 147 |
charlen = utf8_to_utf32(&name[i], len - i, &u); |
| 148 |
if (charlen < 0) |
| 149 |
return charlen; |
| 150 |
|
| 151 |
/* |
| 152 |
* exfat_toupper() works only for code points up to the U+FFFF. |
| 153 |
*/ |
| 154 |
hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u, |
| 155 |
hash); |
| 156 |
} |
| 157 |
|
| 158 |
qstr->hash = end_name_hash(hash); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len, |
| 163 |
const char *str, const struct qstr *name) |
| 164 |
{ |
| 165 |
struct super_block *sb = dentry->d_sb; |
| 166 |
unsigned int alen = exfat_striptail_len(name->len, name->name, |
| 167 |
EXFAT_SB(sb)->options.keep_last_dots); |
| 168 |
unsigned int blen = exfat_striptail_len(len, str, |
| 169 |
EXFAT_SB(sb)->options.keep_last_dots); |
| 170 |
|
| 171 |
unicode_t u_a, u_b; |
| 172 |
int charlen, i; |
| 173 |
|
| 174 |
if (alen != blen) |
| 175 |
return 1; |
| 176 |
|
| 177 |
for (i = 0; i < alen; i += charlen) { |
| 178 |
charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a); |
| 179 |
if (charlen < 0) |
| 180 |
return 1; |
| 181 |
if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b)) |
| 182 |
return 1; |
| 183 |
|
| 184 |
if (u_a <= 0xFFFF && u_b <= 0xFFFF) { |
| 185 |
if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b)) |
| 186 |
return 1; |
| 187 |
} else { |
| 188 |
if (u_a != u_b) |
| 189 |
return 1; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
const struct dentry_operations exfat_utf8_dentry_ops = { |
| 197 |
.d_revalidate = exfat_d_revalidate, |
| 198 |
.d_hash = exfat_utf8_d_hash, |
| 199 |
.d_compare = exfat_utf8_d_cmp, |
| 200 |
}; |
| 201 |
|
| 202 |
/* search EMPTY CONTINUOUS "num_entries" entries */ |
| 203 |
static int exfat_search_empty_slot(struct super_block *sb, |
| 204 |
struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir, |
| 205 |
int num_entries, struct exfat_entry_set_cache *es) |
| 206 |
{ |
| 207 |
int i, dentry, ret; |
| 208 |
int dentries_per_clu; |
| 209 |
struct exfat_chain clu; |
| 210 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 211 |
int total_entries = EXFAT_CLU_TO_DEN(p_dir->size, sbi); |
| 212 |
|
| 213 |
dentries_per_clu = sbi->dentries_per_clu; |
| 214 |
|
| 215 |
if (hint_femp->eidx != EXFAT_HINT_NONE) { |
| 216 |
dentry = hint_femp->eidx; |
| 217 |
|
| 218 |
/* |
| 219 |
* If hint_femp->count is enough, it is needed to check if |
| 220 |
* there are actual empty entries. |
| 221 |
* Otherwise, and if "dentry + hint_famp->count" is also equal |
| 222 |
* to "p_dir->size * dentries_per_clu", it means ENOSPC. |
| 223 |
*/ |
| 224 |
if (dentry + hint_femp->count == total_entries && |
| 225 |
num_entries > hint_femp->count) |
| 226 |
return -ENOSPC; |
| 227 |
|
| 228 |
hint_femp->eidx = EXFAT_HINT_NONE; |
| 229 |
exfat_chain_dup(&clu, &hint_femp->cur); |
| 230 |
} else { |
| 231 |
exfat_chain_dup(&clu, p_dir); |
| 232 |
dentry = 0; |
| 233 |
} |
| 234 |
|
| 235 |
while (dentry + num_entries <= total_entries && |
| 236 |
clu.dir != EXFAT_EOF_CLUSTER) { |
| 237 |
i = dentry & (dentries_per_clu - 1); |
| 238 |
|
| 239 |
ret = exfat_get_empty_dentry_set(es, sb, &clu, i, num_entries); |
| 240 |
if (ret < 0) |
| 241 |
return ret; |
| 242 |
else if (ret == 0) |
| 243 |
return dentry; |
| 244 |
|
| 245 |
dentry += ret; |
| 246 |
i += ret; |
| 247 |
|
| 248 |
while (i >= dentries_per_clu) { |
| 249 |
if (clu.flags == ALLOC_NO_FAT_CHAIN) { |
| 250 |
if (--clu.size > 0) |
| 251 |
clu.dir++; |
| 252 |
else |
| 253 |
clu.dir = EXFAT_EOF_CLUSTER; |
| 254 |
} else { |
| 255 |
if (exfat_get_next_cluster(sb, &clu.dir)) |
| 256 |
return -EIO; |
| 257 |
} |
| 258 |
|
| 259 |
i -= dentries_per_clu; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
hint_femp->eidx = dentry; |
| 264 |
hint_femp->count = 0; |
| 265 |
if (dentry == total_entries || clu.dir == EXFAT_EOF_CLUSTER) |
| 266 |
exfat_chain_set(&hint_femp->cur, EXFAT_EOF_CLUSTER, 0, |
| 267 |
clu.flags); |
| 268 |
else |
| 269 |
hint_femp->cur = clu; |
| 270 |
|
| 271 |
return -ENOSPC; |
| 272 |
} |
| 273 |
|
| 274 |
static int exfat_check_max_dentries(struct inode *inode) |
| 275 |
{ |
| 276 |
if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) { |
| 277 |
/* |
| 278 |
* exFAT spec allows a dir to grow up to 8388608(256MB) |
| 279 |
* dentries |
| 280 |
*/ |
| 281 |
return -ENOSPC; |
| 282 |
} |
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
/* |
| 287 |
* Find an empty directory entry set. |
| 288 |
* |
| 289 |
* If there isn't any empty slot, expand cluster chain. |
| 290 |
* |
| 291 |
* in: |
| 292 |
* inode: inode of the parent directory |
| 293 |
* num_entries: specifies how many dentries in the empty directory entry set |
| 294 |
* |
| 295 |
* out: |
| 296 |
* p_dir: the cluster where the empty directory entry set is located |
| 297 |
* es: The found empty directory entry set |
| 298 |
* |
| 299 |
* return: |
| 300 |
* the directory entry index in p_dir is returned on succeeds |
| 301 |
* -error code is returned on failure |
| 302 |
*/ |
| 303 |
int exfat_find_empty_entry(struct inode *inode, |
| 304 |
struct exfat_chain *p_dir, int num_entries, |
| 305 |
struct exfat_entry_set_cache *es) |
| 306 |
{ |
| 307 |
int dentry, ret; |
| 308 |
unsigned int last_clu; |
| 309 |
loff_t size = 0; |
| 310 |
struct exfat_chain clu; |
| 311 |
struct super_block *sb = inode->i_sb; |
| 312 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 313 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 314 |
struct exfat_hint_femp hint_femp; |
| 315 |
|
| 316 |
hint_femp.eidx = EXFAT_HINT_NONE; |
| 317 |
|
| 318 |
if (ei->hint_femp.eidx != EXFAT_HINT_NONE) { |
| 319 |
hint_femp = ei->hint_femp; |
| 320 |
ei->hint_femp.eidx = EXFAT_HINT_NONE; |
| 321 |
} |
| 322 |
|
| 323 |
exfat_chain_set(p_dir, ei->start_clu, |
| 324 |
EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); |
| 325 |
|
| 326 |
while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir, |
| 327 |
num_entries, es)) < 0) { |
| 328 |
if (dentry != -ENOSPC) |
| 329 |
return dentry; |
| 330 |
|
| 331 |
if (exfat_check_max_dentries(inode)) |
| 332 |
return -ENOSPC; |
| 333 |
|
| 334 |
/* |
| 335 |
* Allocate new cluster to this directory |
| 336 |
*/ |
| 337 |
if (ei->start_clu != EXFAT_EOF_CLUSTER) { |
| 338 |
/* we trust p_dir->size regardless of FAT type */ |
| 339 |
if (exfat_find_last_cluster(sb, p_dir, &last_clu)) |
| 340 |
return -EIO; |
| 341 |
|
| 342 |
exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags); |
| 343 |
} else { |
| 344 |
/* This directory is empty */ |
| 345 |
exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0, |
| 346 |
ALLOC_NO_FAT_CHAIN); |
| 347 |
} |
| 348 |
|
| 349 |
/* allocate a cluster */ |
| 350 |
ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode)); |
| 351 |
if (ret) |
| 352 |
return ret; |
| 353 |
|
| 354 |
if (exfat_zeroed_cluster(inode, clu.dir)) |
| 355 |
return -EIO; |
| 356 |
|
| 357 |
if (ei->start_clu == EXFAT_EOF_CLUSTER) { |
| 358 |
ei->start_clu = clu.dir; |
| 359 |
p_dir->dir = clu.dir; |
| 360 |
hint_femp.eidx = 0; |
| 361 |
} |
| 362 |
|
| 363 |
/* append to the FAT chain */ |
| 364 |
if (clu.flags != p_dir->flags) { |
| 365 |
/* no-fat-chain bit is disabled, |
| 366 |
* so fat-chain should be synced with alloc-bitmap |
| 367 |
*/ |
| 368 |
exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size); |
| 369 |
p_dir->flags = ALLOC_FAT_CHAIN; |
| 370 |
hint_femp.cur.flags = ALLOC_FAT_CHAIN; |
| 371 |
} |
| 372 |
|
| 373 |
if (clu.flags == ALLOC_FAT_CHAIN) |
| 374 |
if (exfat_ent_set(sb, last_clu, clu.dir)) |
| 375 |
return -EIO; |
| 376 |
|
| 377 |
if (hint_femp.cur.dir == EXFAT_EOF_CLUSTER) |
| 378 |
exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags); |
| 379 |
|
| 380 |
hint_femp.count += sbi->dentries_per_clu; |
| 381 |
|
| 382 |
hint_femp.cur.size++; |
| 383 |
p_dir->size++; |
| 384 |
size = EXFAT_CLU_TO_B(p_dir->size, sbi); |
| 385 |
|
| 386 |
/* directory inode should be updated in here */ |
| 387 |
i_size_write(inode, size); |
| 388 |
ei->valid_size += sbi->cluster_size; |
| 389 |
ei->flags = p_dir->flags; |
| 390 |
inode->i_blocks += sbi->cluster_size >> 9; |
| 391 |
} |
| 392 |
|
| 393 |
p_dir->dir = exfat_sector_to_cluster(sbi, es->bh[0]->b_blocknr); |
| 394 |
p_dir->size -= dentry / sbi->dentries_per_clu; |
| 395 |
|
| 396 |
return dentry & (sbi->dentries_per_clu - 1); |
| 397 |
} |
| 398 |
|
| 399 |
/* |
| 400 |
* Name Resolution Functions : |
| 401 |
* Zero if it was successful; otherwise nonzero. |
| 402 |
*/ |
| 403 |
static int __exfat_resolve_path(struct inode *inode, const unsigned char *path, |
| 404 |
struct exfat_uni_name *p_uniname, int lookup) |
| 405 |
{ |
| 406 |
int namelen; |
| 407 |
int lossy = NLS_NAME_NO_LOSSY; |
| 408 |
struct super_block *sb = inode->i_sb; |
| 409 |
int pathlen = strlen(path); |
| 410 |
|
| 411 |
/* |
| 412 |
* get the length of the pathname excluding |
| 413 |
* trailing periods, if any. |
| 414 |
*/ |
| 415 |
namelen = exfat_striptail_len(pathlen, path, false); |
| 416 |
if (EXFAT_SB(sb)->options.keep_last_dots) { |
| 417 |
/* |
| 418 |
* Do not allow the creation of files with names |
| 419 |
* ending with period(s). |
| 420 |
*/ |
| 421 |
if (!lookup && (namelen < pathlen)) |
| 422 |
return -EINVAL; |
| 423 |
namelen = pathlen; |
| 424 |
} |
| 425 |
if (!namelen) |
| 426 |
return -ENOENT; |
| 427 |
if (pathlen > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE)) |
| 428 |
return -ENAMETOOLONG; |
| 429 |
|
| 430 |
/* |
| 431 |
* strip all leading spaces : |
| 432 |
* "MS windows 7" supports leading spaces. |
| 433 |
* So we should skip this preprocessing for compatibility. |
| 434 |
*/ |
| 435 |
|
| 436 |
/* file name conversion : |
| 437 |
* If lookup case, we allow bad-name for compatibility. |
| 438 |
*/ |
| 439 |
namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname, |
| 440 |
&lossy); |
| 441 |
if (namelen < 0) |
| 442 |
return namelen; /* return error value */ |
| 443 |
|
| 444 |
if ((lossy && !lookup) || !namelen) |
| 445 |
return -EINVAL; |
| 446 |
|
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
static inline int exfat_resolve_path(struct inode *inode, |
| 451 |
const unsigned char *path, struct exfat_uni_name *uni) |
| 452 |
{ |
| 453 |
return __exfat_resolve_path(inode, path, uni, 0); |
| 454 |
} |
| 455 |
|
| 456 |
static inline int exfat_resolve_path_for_lookup(struct inode *inode, |
| 457 |
const unsigned char *path, struct exfat_uni_name *uni) |
| 458 |
{ |
| 459 |
return __exfat_resolve_path(inode, path, uni, 1); |
| 460 |
} |
| 461 |
|
| 462 |
static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info) |
| 463 |
{ |
| 464 |
return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff); |
| 465 |
} |
| 466 |
|
| 467 |
static int exfat_add_entry(struct inode *inode, const char *path, |
| 468 |
unsigned int type, struct exfat_dir_entry *info) |
| 469 |
{ |
| 470 |
int ret, dentry, num_entries; |
| 471 |
struct super_block *sb = inode->i_sb; |
| 472 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 473 |
struct exfat_uni_name uniname; |
| 474 |
struct exfat_chain clu; |
| 475 |
struct timespec64 ts = current_time(inode); |
| 476 |
struct exfat_entry_set_cache es; |
| 477 |
int clu_size = 0; |
| 478 |
unsigned int start_clu = EXFAT_FREE_CLUSTER; |
| 479 |
|
| 480 |
ret = exfat_resolve_path(inode, path, &uniname); |
| 481 |
if (ret) |
| 482 |
goto out; |
| 483 |
|
| 484 |
num_entries = exfat_calc_num_entries(&uniname); |
| 485 |
if (num_entries < 0) { |
| 486 |
ret = num_entries; |
| 487 |
goto out; |
| 488 |
} |
| 489 |
|
| 490 |
/* exfat_find_empty_entry must be called before alloc_cluster() */ |
| 491 |
dentry = exfat_find_empty_entry(inode, &info->dir, num_entries, &es); |
| 492 |
if (dentry < 0) { |
| 493 |
ret = dentry; /* -EIO or -ENOSPC */ |
| 494 |
goto out; |
| 495 |
} |
| 496 |
|
| 497 |
if (type == TYPE_DIR && !sbi->options.zero_size_dir) { |
| 498 |
ret = exfat_alloc_new_dir(inode, &clu); |
| 499 |
if (ret) { |
| 500 |
exfat_put_dentry_set(&es, false); |
| 501 |
goto out; |
| 502 |
} |
| 503 |
start_clu = clu.dir; |
| 504 |
clu_size = sbi->cluster_size; |
| 505 |
} |
| 506 |
|
| 507 |
/* update the directory entry */ |
| 508 |
/* fill the dos name directory entry information of the created file. |
| 509 |
* the first cluster is not determined yet. (0) |
| 510 |
*/ |
| 511 |
exfat_init_dir_entry(&es, type, start_clu, clu_size, &ts); |
| 512 |
exfat_init_ext_entry(&es, num_entries, &uniname); |
| 513 |
|
| 514 |
ret = exfat_put_dentry_set(&es, IS_DIRSYNC(inode)); |
| 515 |
if (ret) |
| 516 |
goto out; |
| 517 |
|
| 518 |
info->entry = dentry; |
| 519 |
info->flags = ALLOC_NO_FAT_CHAIN; |
| 520 |
info->type = type; |
| 521 |
|
| 522 |
if (type == TYPE_FILE) { |
| 523 |
info->attr = EXFAT_ATTR_ARCHIVE; |
| 524 |
info->start_clu = EXFAT_EOF_CLUSTER; |
| 525 |
info->size = 0; |
| 526 |
info->num_subdirs = 0; |
| 527 |
} else { |
| 528 |
info->attr = EXFAT_ATTR_SUBDIR; |
| 529 |
if (sbi->options.zero_size_dir) |
| 530 |
info->start_clu = EXFAT_EOF_CLUSTER; |
| 531 |
else |
| 532 |
info->start_clu = start_clu; |
| 533 |
info->size = clu_size; |
| 534 |
info->num_subdirs = EXFAT_MIN_SUBDIR; |
| 535 |
} |
| 536 |
info->valid_size = info->size; |
| 537 |
|
| 538 |
memset(&info->crtime, 0, sizeof(info->crtime)); |
| 539 |
memset(&info->mtime, 0, sizeof(info->mtime)); |
| 540 |
memset(&info->atime, 0, sizeof(info->atime)); |
| 541 |
out: |
| 542 |
return ret; |
| 543 |
} |
| 544 |
|
| 545 |
static int exfat_create(struct mnt_idmap *idmap, struct inode *dir, |
| 546 |
struct dentry *dentry, umode_t mode, bool excl) |
| 547 |
{ |
| 548 |
struct super_block *sb = dir->i_sb; |
| 549 |
struct inode *inode; |
| 550 |
struct exfat_dir_entry info; |
| 551 |
loff_t i_pos; |
| 552 |
int err; |
| 553 |
loff_t size = i_size_read(dir); |
| 554 |
|
| 555 |
if (unlikely(exfat_forced_shutdown(sb))) |
| 556 |
return -EIO; |
| 557 |
|
| 558 |
mutex_lock(&EXFAT_SB(sb)->s_lock); |
| 559 |
exfat_set_volume_dirty(sb); |
| 560 |
err = exfat_add_entry(dir, dentry->d_name.name, TYPE_FILE, &info); |
| 561 |
if (err) |
| 562 |
goto unlock; |
| 563 |
|
| 564 |
inode_inc_iversion(dir); |
| 565 |
inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); |
| 566 |
if (IS_DIRSYNC(dir) && size != i_size_read(dir)) |
| 567 |
exfat_sync_inode(dir); |
| 568 |
else |
| 569 |
mark_inode_dirty(dir); |
| 570 |
|
| 571 |
i_pos = exfat_make_i_pos(&info); |
| 572 |
inode = exfat_build_inode(sb, &info, i_pos); |
| 573 |
err = PTR_ERR_OR_ZERO(inode); |
| 574 |
if (err) |
| 575 |
goto unlock; |
| 576 |
|
| 577 |
inode_inc_iversion(inode); |
| 578 |
EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode); |
| 579 |
exfat_truncate_inode_atime(inode); |
| 580 |
|
| 581 |
/* timestamp is already written, so mark_inode_dirty() is unneeded. */ |
| 582 |
|
| 583 |
d_instantiate(dentry, inode); |
| 584 |
unlock: |
| 585 |
mutex_unlock(&EXFAT_SB(sb)->s_lock); |
| 586 |
return err; |
| 587 |
} |
| 588 |
|
| 589 |
/* lookup a file */ |
| 590 |
static int exfat_find(struct inode *dir, const struct qstr *qname, |
| 591 |
struct exfat_dir_entry *info) |
| 592 |
{ |
| 593 |
int ret, dentry, count; |
| 594 |
struct exfat_chain cdir; |
| 595 |
struct exfat_uni_name uni_name; |
| 596 |
struct super_block *sb = dir->i_sb; |
| 597 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 598 |
struct exfat_inode_info *ei = EXFAT_I(dir); |
| 599 |
struct exfat_dentry *ep, *ep2; |
| 600 |
struct exfat_entry_set_cache es; |
| 601 |
/* for optimized dir & entry to prevent long traverse of cluster chain */ |
| 602 |
struct exfat_hint hint_opt; |
| 603 |
|
| 604 |
if (qname->len == 0) |
| 605 |
return -ENOENT; |
| 606 |
|
| 607 |
/* check the validity of directory name in the given pathname */ |
| 608 |
ret = exfat_resolve_path_for_lookup(dir, qname->name, &uni_name); |
| 609 |
if (ret) |
| 610 |
return ret; |
| 611 |
|
| 612 |
exfat_chain_set(&cdir, ei->start_clu, |
| 613 |
EXFAT_B_TO_CLU(i_size_read(dir), sbi), ei->flags); |
| 614 |
|
| 615 |
/* check the validation of hint_stat and initialize it if required */ |
| 616 |
if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) { |
| 617 |
ei->hint_stat.clu = cdir.dir; |
| 618 |
ei->hint_stat.eidx = 0; |
| 619 |
ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff); |
| 620 |
ei->hint_femp.eidx = EXFAT_HINT_NONE; |
| 621 |
} |
| 622 |
|
| 623 |
/* search the file name for directories */ |
| 624 |
dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt); |
| 625 |
if (dentry < 0) |
| 626 |
return dentry; /* -error value */ |
| 627 |
|
| 628 |
/* adjust cdir to the optimized value */ |
| 629 |
cdir.dir = hint_opt.clu; |
| 630 |
if (cdir.flags & ALLOC_NO_FAT_CHAIN) |
| 631 |
cdir.size -= dentry / sbi->dentries_per_clu; |
| 632 |
dentry = hint_opt.eidx; |
| 633 |
|
| 634 |
info->dir = cdir; |
| 635 |
info->entry = dentry; |
| 636 |
info->num_subdirs = 0; |
| 637 |
|
| 638 |
if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES)) |
| 639 |
return -EIO; |
| 640 |
ep = exfat_get_dentry_cached(&es, ES_IDX_FILE); |
| 641 |
ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM); |
| 642 |
|
| 643 |
info->type = exfat_get_entry_type(ep); |
| 644 |
info->attr = le16_to_cpu(ep->dentry.file.attr); |
| 645 |
info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size); |
| 646 |
info->size = le64_to_cpu(ep2->dentry.stream.size); |
| 647 |
|
| 648 |
info->start_clu = le32_to_cpu(ep2->dentry.stream.start_clu); |
| 649 |
if (!is_valid_cluster(sbi, info->start_clu) && info->size) { |
| 650 |
exfat_warn(sb, "start_clu is invalid cluster(0x%x)", |
| 651 |
info->start_clu); |
| 652 |
info->size = 0; |
| 653 |
info->valid_size = 0; |
| 654 |
} |
| 655 |
|
| 656 |
if (info->valid_size > info->size) { |
| 657 |
exfat_warn(sb, "valid_size(%lld) is greater than size(%lld)", |
| 658 |
info->valid_size, info->size); |
| 659 |
info->valid_size = info->size; |
| 660 |
} |
| 661 |
|
| 662 |
if (info->size == 0) { |
| 663 |
info->flags = ALLOC_NO_FAT_CHAIN; |
| 664 |
info->start_clu = EXFAT_EOF_CLUSTER; |
| 665 |
} else |
| 666 |
info->flags = ep2->dentry.stream.flags; |
| 667 |
|
| 668 |
exfat_get_entry_time(sbi, &info->crtime, |
| 669 |
ep->dentry.file.create_tz, |
| 670 |
ep->dentry.file.create_time, |
| 671 |
ep->dentry.file.create_date, |
| 672 |
ep->dentry.file.create_time_cs); |
| 673 |
exfat_get_entry_time(sbi, &info->mtime, |
| 674 |
ep->dentry.file.modify_tz, |
| 675 |
ep->dentry.file.modify_time, |
| 676 |
ep->dentry.file.modify_date, |
| 677 |
ep->dentry.file.modify_time_cs); |
| 678 |
exfat_get_entry_time(sbi, &info->atime, |
| 679 |
ep->dentry.file.access_tz, |
| 680 |
ep->dentry.file.access_time, |
| 681 |
ep->dentry.file.access_date, |
| 682 |
0); |
| 683 |
exfat_put_dentry_set(&es, false); |
| 684 |
|
| 685 |
if (info->valid_size < 0) { |
| 686 |
exfat_fs_error(sb, "data valid size is invalid(%lld)", info->valid_size); |
| 687 |
return -EIO; |
| 688 |
} |
| 689 |
|
| 690 |
if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) { |
| 691 |
exfat_fs_error(sb, "data size is invalid(%lld)", info->size); |
| 692 |
return -EIO; |
| 693 |
} |
| 694 |
|
| 695 |
if (ei->start_clu == EXFAT_FREE_CLUSTER) { |
| 696 |
exfat_fs_error(sb, |
| 697 |
"non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)", |
| 698 |
i_size_read(dir), ei->dir.dir, ei->entry); |
| 699 |
return -EIO; |
| 700 |
} |
| 701 |
|
| 702 |
if (info->type == TYPE_DIR) { |
| 703 |
exfat_chain_set(&cdir, info->start_clu, |
| 704 |
EXFAT_B_TO_CLU(info->size, sbi), info->flags); |
| 705 |
count = exfat_count_dir_entries(sb, &cdir); |
| 706 |
if (count < 0) |
| 707 |
return -EIO; |
| 708 |
|
| 709 |
info->num_subdirs = count + EXFAT_MIN_SUBDIR; |
| 710 |
} |
| 711 |
return 0; |
| 712 |
} |
| 713 |
|
| 714 |
static int exfat_d_anon_disconn(struct dentry *dentry) |
| 715 |
{ |
| 716 |
return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED); |
| 717 |
} |
| 718 |
|
| 719 |
static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry, |
| 720 |
unsigned int flags) |
| 721 |
{ |
| 722 |
struct super_block *sb = dir->i_sb; |
| 723 |
struct inode *inode; |
| 724 |
struct dentry *alias; |
| 725 |
struct exfat_dir_entry info; |
| 726 |
int err; |
| 727 |
loff_t i_pos; |
| 728 |
mode_t i_mode; |
| 729 |
|
| 730 |
mutex_lock(&EXFAT_SB(sb)->s_lock); |
| 731 |
err = exfat_find(dir, &dentry->d_name, &info); |
| 732 |
if (err) { |
| 733 |
if (err == -ENOENT) { |
| 734 |
inode = NULL; |
| 735 |
goto out; |
| 736 |
} |
| 737 |
goto unlock; |
| 738 |
} |
| 739 |
|
| 740 |
i_pos = exfat_make_i_pos(&info); |
| 741 |
inode = exfat_build_inode(sb, &info, i_pos); |
| 742 |
err = PTR_ERR_OR_ZERO(inode); |
| 743 |
if (err) |
| 744 |
goto unlock; |
| 745 |
|
| 746 |
i_mode = inode->i_mode; |
| 747 |
alias = d_find_alias(inode); |
| 748 |
|
| 749 |
/* |
| 750 |
* Checking "alias->d_parent == dentry->d_parent" to make sure |
| 751 |
* FS is not corrupted (especially double linked dir). |
| 752 |
*/ |
| 753 |
if (alias && alias->d_parent == dentry->d_parent && |
| 754 |
!exfat_d_anon_disconn(alias)) { |
| 755 |
|
| 756 |
/* |
| 757 |
* Unhashed alias is able to exist because of revalidate() |
| 758 |
* called by lookup_fast. You can easily make this status |
| 759 |
* by calling create and lookup concurrently |
| 760 |
* In such case, we reuse an alias instead of new dentry |
| 761 |
*/ |
| 762 |
if (d_unhashed(alias)) { |
| 763 |
WARN_ON(alias->d_name.hash_len != |
| 764 |
dentry->d_name.hash_len); |
| 765 |
exfat_info(sb, "rehashed a dentry(%p) in read lookup", |
| 766 |
alias); |
| 767 |
d_drop(dentry); |
| 768 |
d_rehash(alias); |
| 769 |
} else if (!S_ISDIR(i_mode)) { |
| 770 |
/* |
| 771 |
* This inode has non anonymous-DCACHE_DISCONNECTED |
| 772 |
* dentry. This means, the user did ->lookup() by an |
| 773 |
* another name (longname vs 8.3 alias of it) in past. |
| 774 |
* |
| 775 |
* Switch to new one for reason of locality if possible. |
| 776 |
*/ |
| 777 |
d_move(alias, dentry); |
| 778 |
} |
| 779 |
iput(inode); |
| 780 |
mutex_unlock(&EXFAT_SB(sb)->s_lock); |
| 781 |
return alias; |
| 782 |
} |
| 783 |
dput(alias); |
| 784 |
out: |
| 785 |
mutex_unlock(&EXFAT_SB(sb)->s_lock); |
| 786 |
if (!inode) |
| 787 |
exfat_d_version_set(dentry, inode_query_iversion(dir)); |
| 788 |
|
| 789 |
return d_splice_alias(inode, dentry); |
| 790 |
unlock: |
| 791 |
mutex_unlock(&EXFAT_SB(sb)->s_lock); |
| 792 |
return ERR_PTR(err); |
| 793 |
} |
| 794 |
|
| 795 |
/* remove an entry, BUT don't truncate */ |
| 796 |
static int exfat_unlink(struct inode *dir, struct dentry *dentry) |
| 797 |
{ |
| 798 |
struct super_block *sb = dir->i_sb; |
| 799 |
struct inode *inode = dentry->d_inode; |
| 800 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 801 |
struct exfat_entry_set_cache es; |
| 802 |
int err = 0; |
| 803 |
|
| 804 |
if (unlikely(exfat_forced_shutdown(sb))) |
| 805 |
return -EIO; |
| 806 |
|
| 807 |
mutex_lock(&EXFAT_SB(sb)->s_lock); |
| 808 |
if (ei->dir.dir == DIR_DELETED) { |
| 809 |
exfat_err(sb, "abnormal access to deleted dentry"); |
| 810 |
err = -ENOENT; |
| 811 |
goto unlock; |
| 812 |
} |
| 813 |
|
| 814 |
err = exfat_get_dentry_set_by_ei(&es, sb, ei); |
| 815 |
if (err) { |
| 816 |
err = -EIO; |
| 817 |
goto unlock; |
| 818 |
} |
| 819 |
|
| 820 |
exfat_set_volume_dirty(sb); |
| 821 |
|
| 822 |
/* update the directory entry */ |
| 823 |
exfat_remove_entries(inode, &es, ES_IDX_FILE); |
| 824 |
|
| 825 |
err = exfat_put_dentry_set(&es, IS_DIRSYNC(inode)); |
| 826 |
if (err) |
| 827 |
goto unlock; |
| 828 |
|
| 829 |
/* This doesn't modify ei */ |
| 830 |
ei->dir.dir = DIR_DELETED; |
| 831 |
|
| 832 |
inode_inc_iversion(dir); |
| 833 |
simple_inode_init_ts(dir); |
| 834 |
exfat_truncate_inode_atime(dir); |
| 835 |
mark_inode_dirty(dir); |
| 836 |
|
| 837 |
clear_nlink(inode); |
| 838 |
simple_inode_init_ts(inode); |
| 839 |
exfat_truncate_inode_atime(inode); |
| 840 |
exfat_unhash_inode(inode); |
| 841 |
exfat_d_version_set(dentry, inode_query_iversion(dir)); |
| 842 |
unlock: |
| 843 |
mutex_unlock(&EXFAT_SB(sb)->s_lock); |
| 844 |
return err; |
| 845 |
} |
| 846 |
|
| 847 |
static struct dentry *exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
| 848 |
struct dentry *dentry, umode_t mode) |
| 849 |
{ |
| 850 |
struct super_block *sb = dir->i_sb; |
| 851 |
struct inode *inode; |
| 852 |
struct exfat_dir_entry info; |
| 853 |
loff_t i_pos; |
| 854 |
int err; |
| 855 |
loff_t size = i_size_read(dir); |
| 856 |
|
| 857 |
if (unlikely(exfat_forced_shutdown(sb))) |
| 858 |
return ERR_PTR(-EIO); |
| 859 |
|
| 860 |
mutex_lock(&EXFAT_SB(sb)->s_lock); |
| 861 |
exfat_set_volume_dirty(sb); |
| 862 |
err = exfat_add_entry(dir, dentry->d_name.name, TYPE_DIR, &info); |
| 863 |
if (err) |
| 864 |
goto unlock; |
| 865 |
|
| 866 |
inode_inc_iversion(dir); |
| 867 |
inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); |
| 868 |
if (IS_DIRSYNC(dir) && size != i_size_read(dir)) |
| 869 |
exfat_sync_inode(dir); |
| 870 |
else |
| 871 |
mark_inode_dirty(dir); |
| 872 |
inc_nlink(dir); |
| 873 |
|
| 874 |
i_pos = exfat_make_i_pos(&info); |
| 875 |
inode = exfat_build_inode(sb, &info, i_pos); |
| 876 |
err = PTR_ERR_OR_ZERO(inode); |
| 877 |
if (err) |
| 878 |
goto unlock; |
| 879 |
|
| 880 |
inode_inc_iversion(inode); |
| 881 |
EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode); |
| 882 |
exfat_truncate_inode_atime(inode); |
| 883 |
/* timestamp is already written, so mark_inode_dirty() is unneeded. */ |
| 884 |
|
| 885 |
d_instantiate(dentry, inode); |
| 886 |
|
| 887 |
unlock: |
| 888 |
mutex_unlock(&EXFAT_SB(sb)->s_lock); |
| 889 |
return ERR_PTR(err); |
| 890 |
} |
| 891 |
|
| 892 |
static int exfat_check_dir_empty(struct super_block *sb, |
| 893 |
struct exfat_chain *p_dir) |
| 894 |
{ |
| 895 |
int i, dentries_per_clu; |
| 896 |
unsigned int type; |
| 897 |
unsigned int clu_count = 0; |
| 898 |
struct exfat_chain clu; |
| 899 |
struct exfat_dentry *ep; |
| 900 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 901 |
struct buffer_head *bh; |
| 902 |
|
| 903 |
dentries_per_clu = sbi->dentries_per_clu; |
| 904 |
|
| 905 |
if (p_dir->dir == EXFAT_EOF_CLUSTER) |
| 906 |
return 0; |
| 907 |
|
| 908 |
exfat_chain_dup(&clu, p_dir); |
| 909 |
|
| 910 |
while (clu.dir != EXFAT_EOF_CLUSTER) { |
| 911 |
for (i = 0; i < dentries_per_clu; i++) { |
| 912 |
ep = exfat_get_dentry(sb, &clu, i, &bh); |
| 913 |
if (!ep) |
| 914 |
return -EIO; |
| 915 |
type = exfat_get_entry_type(ep); |
| 916 |
brelse(bh); |
| 917 |
if (type == TYPE_UNUSED) |
| 918 |
return 0; |
| 919 |
|
| 920 |
if (type != TYPE_FILE && type != TYPE_DIR) |
| 921 |
continue; |
| 922 |
|
| 923 |
return -ENOTEMPTY; |
| 924 |
} |
| 925 |
|
| 926 |
if (clu.flags == ALLOC_NO_FAT_CHAIN) { |
| 927 |
if (--clu.size > 0) |
| 928 |
clu.dir++; |
| 929 |
else |
| 930 |
clu.dir = EXFAT_EOF_CLUSTER; |
| 931 |
} else { |
| 932 |
if (exfat_get_next_cluster(sb, &(clu.dir))) |
| 933 |
return -EIO; |
| 934 |
|
| 935 |
/* break if the cluster chain includes a loop */ |
| 936 |
if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi))) |
| 937 |
break; |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
return 0; |
| 942 |
} |
| 943 |
|
| 944 |
static int exfat_rmdir(struct inode *dir, struct dentry *dentry) |
| 945 |
{ |
| 946 |
struct inode *inode = dentry->d_inode; |
| 947 |
struct exfat_chain clu_to_free; |
| 948 |
struct super_block *sb = inode->i_sb; |
| 949 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 950 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 951 |
struct exfat_entry_set_cache es; |
| 952 |
int err; |
| 953 |
|
| 954 |
if (unlikely(exfat_forced_shutdown(sb))) |
| 955 |
return -EIO; |
| 956 |
|
| 957 |
mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); |
| 958 |
|
| 959 |
if (ei->dir.dir == DIR_DELETED) { |
| 960 |
exfat_err(sb, "abnormal access to deleted dentry"); |
| 961 |
err = -ENOENT; |
| 962 |
goto unlock; |
| 963 |
} |
| 964 |
|
| 965 |
exfat_chain_set(&clu_to_free, ei->start_clu, |
| 966 |
EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags); |
| 967 |
|
| 968 |
err = exfat_check_dir_empty(sb, &clu_to_free); |
| 969 |
if (err) { |
| 970 |
if (err == -EIO) |
| 971 |
exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)", |
| 972 |
err); |
| 973 |
goto unlock; |
| 974 |
} |
| 975 |
|
| 976 |
err = exfat_get_dentry_set_by_ei(&es, sb, ei); |
| 977 |
if (err) { |
| 978 |
err = -EIO; |
| 979 |
goto unlock; |
| 980 |
} |
| 981 |
|
| 982 |
exfat_set_volume_dirty(sb); |
| 983 |
|
| 984 |
exfat_remove_entries(inode, &es, ES_IDX_FILE); |
| 985 |
|
| 986 |
err = exfat_put_dentry_set(&es, IS_DIRSYNC(dir)); |
| 987 |
if (err) |
| 988 |
goto unlock; |
| 989 |
|
| 990 |
ei->dir.dir = DIR_DELETED; |
| 991 |
|
| 992 |
inode_inc_iversion(dir); |
| 993 |
simple_inode_init_ts(dir); |
| 994 |
exfat_truncate_inode_atime(dir); |
| 995 |
if (IS_DIRSYNC(dir)) |
| 996 |
exfat_sync_inode(dir); |
| 997 |
else |
| 998 |
mark_inode_dirty(dir); |
| 999 |
drop_nlink(dir); |
| 1000 |
|
| 1001 |
clear_nlink(inode); |
| 1002 |
simple_inode_init_ts(inode); |
| 1003 |
exfat_truncate_inode_atime(inode); |
| 1004 |
exfat_unhash_inode(inode); |
| 1005 |
exfat_d_version_set(dentry, inode_query_iversion(dir)); |
| 1006 |
unlock: |
| 1007 |
mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); |
| 1008 |
return err; |
| 1009 |
} |
| 1010 |
|
| 1011 |
static int exfat_rename_file(struct inode *parent_inode, |
| 1012 |
struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei) |
| 1013 |
{ |
| 1014 |
int ret, num_new_entries; |
| 1015 |
struct exfat_dentry *epold, *epnew; |
| 1016 |
struct super_block *sb = parent_inode->i_sb; |
| 1017 |
struct exfat_entry_set_cache old_es, new_es; |
| 1018 |
int sync = IS_DIRSYNC(parent_inode); |
| 1019 |
|
| 1020 |
if (unlikely(exfat_forced_shutdown(sb))) |
| 1021 |
return -EIO; |
| 1022 |
|
| 1023 |
num_new_entries = exfat_calc_num_entries(p_uniname); |
| 1024 |
if (num_new_entries < 0) |
| 1025 |
return num_new_entries; |
| 1026 |
|
| 1027 |
ret = exfat_get_dentry_set_by_ei(&old_es, sb, ei); |
| 1028 |
if (ret) { |
| 1029 |
ret = -EIO; |
| 1030 |
return ret; |
| 1031 |
} |
| 1032 |
|
| 1033 |
epold = exfat_get_dentry_cached(&old_es, ES_IDX_FILE); |
| 1034 |
|
| 1035 |
if (old_es.num_entries < num_new_entries) { |
| 1036 |
int newentry; |
| 1037 |
struct exfat_chain dir; |
| 1038 |
|
| 1039 |
newentry = exfat_find_empty_entry(parent_inode, &dir, |
| 1040 |
num_new_entries, &new_es); |
| 1041 |
if (newentry < 0) { |
| 1042 |
ret = newentry; /* -EIO or -ENOSPC */ |
| 1043 |
goto put_old_es; |
| 1044 |
} |
| 1045 |
|
| 1046 |
epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE); |
| 1047 |
*epnew = *epold; |
| 1048 |
if (exfat_get_entry_type(epnew) == TYPE_FILE) { |
| 1049 |
epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE); |
| 1050 |
ei->attr |= EXFAT_ATTR_ARCHIVE; |
| 1051 |
} |
| 1052 |
|
| 1053 |
epold = exfat_get_dentry_cached(&old_es, ES_IDX_STREAM); |
| 1054 |
epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM); |
| 1055 |
*epnew = *epold; |
| 1056 |
|
| 1057 |
exfat_init_ext_entry(&new_es, num_new_entries, p_uniname); |
| 1058 |
|
| 1059 |
ret = exfat_put_dentry_set(&new_es, sync); |
| 1060 |
if (ret) |
| 1061 |
goto put_old_es; |
| 1062 |
|
| 1063 |
exfat_remove_entries(parent_inode, &old_es, ES_IDX_FILE); |
| 1064 |
ei->dir = dir; |
| 1065 |
ei->entry = newentry; |
| 1066 |
} else { |
| 1067 |
if (exfat_get_entry_type(epold) == TYPE_FILE) { |
| 1068 |
epold->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE); |
| 1069 |
ei->attr |= EXFAT_ATTR_ARCHIVE; |
| 1070 |
} |
| 1071 |
|
| 1072 |
exfat_remove_entries(parent_inode, &old_es, ES_IDX_FIRST_FILENAME + 1); |
| 1073 |
exfat_init_ext_entry(&old_es, num_new_entries, p_uniname); |
| 1074 |
} |
| 1075 |
return exfat_put_dentry_set(&old_es, sync); |
| 1076 |
|
| 1077 |
put_old_es: |
| 1078 |
exfat_put_dentry_set(&old_es, false); |
| 1079 |
return ret; |
| 1080 |
} |
| 1081 |
|
| 1082 |
static int exfat_move_file(struct inode *parent_inode, |
| 1083 |
struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei) |
| 1084 |
{ |
| 1085 |
int ret, newentry, num_new_entries; |
| 1086 |
struct exfat_dentry *epmov, *epnew; |
| 1087 |
struct exfat_entry_set_cache mov_es, new_es; |
| 1088 |
struct exfat_chain newdir; |
| 1089 |
|
| 1090 |
num_new_entries = exfat_calc_num_entries(p_uniname); |
| 1091 |
if (num_new_entries < 0) |
| 1092 |
return num_new_entries; |
| 1093 |
|
| 1094 |
ret = exfat_get_dentry_set_by_ei(&mov_es, parent_inode->i_sb, ei); |
| 1095 |
if (ret) |
| 1096 |
return -EIO; |
| 1097 |
|
| 1098 |
newentry = exfat_find_empty_entry(parent_inode, &newdir, |
| 1099 |
num_new_entries, &new_es); |
| 1100 |
if (newentry < 0) { |
| 1101 |
ret = newentry; /* -EIO or -ENOSPC */ |
| 1102 |
goto put_mov_es; |
| 1103 |
} |
| 1104 |
|
| 1105 |
epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_FILE); |
| 1106 |
epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE); |
| 1107 |
*epnew = *epmov; |
| 1108 |
if (exfat_get_entry_type(epnew) == TYPE_FILE) { |
| 1109 |
epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE); |
| 1110 |
ei->attr |= EXFAT_ATTR_ARCHIVE; |
| 1111 |
} |
| 1112 |
|
| 1113 |
epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_STREAM); |
| 1114 |
epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM); |
| 1115 |
*epnew = *epmov; |
| 1116 |
|
| 1117 |
exfat_init_ext_entry(&new_es, num_new_entries, p_uniname); |
| 1118 |
exfat_remove_entries(parent_inode, &mov_es, ES_IDX_FILE); |
| 1119 |
|
| 1120 |
ei->dir = newdir; |
| 1121 |
ei->entry = newentry; |
| 1122 |
|
| 1123 |
ret = exfat_put_dentry_set(&new_es, IS_DIRSYNC(parent_inode)); |
| 1124 |
if (ret) |
| 1125 |
goto put_mov_es; |
| 1126 |
|
| 1127 |
return exfat_put_dentry_set(&mov_es, IS_DIRSYNC(parent_inode)); |
| 1128 |
|
| 1129 |
put_mov_es: |
| 1130 |
exfat_put_dentry_set(&mov_es, false); |
| 1131 |
|
| 1132 |
return ret; |
| 1133 |
} |
| 1134 |
|
| 1135 |
/* rename or move a old file into a new file */ |
| 1136 |
static int __exfat_rename(struct inode *old_parent_inode, |
| 1137 |
struct exfat_inode_info *ei, struct inode *new_parent_inode, |
| 1138 |
struct dentry *new_dentry) |
| 1139 |
{ |
| 1140 |
int ret; |
| 1141 |
struct exfat_uni_name uni_name; |
| 1142 |
struct super_block *sb = old_parent_inode->i_sb; |
| 1143 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 1144 |
const unsigned char *new_path = new_dentry->d_name.name; |
| 1145 |
struct inode *new_inode = new_dentry->d_inode; |
| 1146 |
struct exfat_inode_info *new_ei = NULL; |
| 1147 |
|
| 1148 |
/* check the validity of pointer parameters */ |
| 1149 |
if (new_path == NULL || strlen(new_path) == 0) |
| 1150 |
return -EINVAL; |
| 1151 |
|
| 1152 |
if (ei->dir.dir == DIR_DELETED) { |
| 1153 |
exfat_err(sb, "abnormal access to deleted source dentry"); |
| 1154 |
return -ENOENT; |
| 1155 |
} |
| 1156 |
|
| 1157 |
/* check whether new dir is existing directory and empty */ |
| 1158 |
if (new_inode) { |
| 1159 |
ret = -EIO; |
| 1160 |
new_ei = EXFAT_I(new_inode); |
| 1161 |
|
| 1162 |
if (new_ei->dir.dir == DIR_DELETED) { |
| 1163 |
exfat_err(sb, "abnormal access to deleted target dentry"); |
| 1164 |
goto out; |
| 1165 |
} |
| 1166 |
|
| 1167 |
/* if new_inode exists, update ei */ |
| 1168 |
if (S_ISDIR(new_inode->i_mode)) { |
| 1169 |
struct exfat_chain new_clu; |
| 1170 |
|
| 1171 |
new_clu.dir = new_ei->start_clu; |
| 1172 |
new_clu.size = |
| 1173 |
EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), |
| 1174 |
sbi); |
| 1175 |
new_clu.flags = new_ei->flags; |
| 1176 |
|
| 1177 |
ret = exfat_check_dir_empty(sb, &new_clu); |
| 1178 |
if (ret) |
| 1179 |
goto out; |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* check the validity of directory name in the given new pathname */ |
| 1184 |
ret = exfat_resolve_path(new_parent_inode, new_path, &uni_name); |
| 1185 |
if (ret) |
| 1186 |
goto out; |
| 1187 |
|
| 1188 |
exfat_set_volume_dirty(sb); |
| 1189 |
|
| 1190 |
if (new_parent_inode == old_parent_inode) |
| 1191 |
ret = exfat_rename_file(new_parent_inode, &uni_name, ei); |
| 1192 |
else |
| 1193 |
ret = exfat_move_file(new_parent_inode, &uni_name, ei); |
| 1194 |
|
| 1195 |
if (!ret && new_inode) { |
| 1196 |
struct exfat_entry_set_cache es; |
| 1197 |
|
| 1198 |
/* delete entries of new_dir */ |
| 1199 |
ret = exfat_get_dentry_set_by_ei(&es, sb, new_ei); |
| 1200 |
if (ret) { |
| 1201 |
ret = -EIO; |
| 1202 |
goto del_out; |
| 1203 |
} |
| 1204 |
|
| 1205 |
exfat_remove_entries(new_inode, &es, ES_IDX_FILE); |
| 1206 |
|
| 1207 |
ret = exfat_put_dentry_set(&es, IS_DIRSYNC(new_inode)); |
| 1208 |
if (ret) |
| 1209 |
goto del_out; |
| 1210 |
|
| 1211 |
/* Free the clusters if new_inode is a dir(as if exfat_rmdir) */ |
| 1212 |
if (S_ISDIR(new_inode->i_mode) && |
| 1213 |
new_ei->start_clu != EXFAT_EOF_CLUSTER) { |
| 1214 |
/* new_ei, new_clu_to_free */ |
| 1215 |
struct exfat_chain new_clu_to_free; |
| 1216 |
|
| 1217 |
exfat_chain_set(&new_clu_to_free, new_ei->start_clu, |
| 1218 |
EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), |
| 1219 |
sbi), new_ei->flags); |
| 1220 |
|
| 1221 |
if (exfat_free_cluster(new_inode, &new_clu_to_free)) { |
| 1222 |
/* just set I/O error only */ |
| 1223 |
ret = -EIO; |
| 1224 |
} |
| 1225 |
|
| 1226 |
i_size_write(new_inode, 0); |
| 1227 |
new_ei->valid_size = 0; |
| 1228 |
new_ei->start_clu = EXFAT_EOF_CLUSTER; |
| 1229 |
new_ei->flags = ALLOC_NO_FAT_CHAIN; |
| 1230 |
} |
| 1231 |
del_out: |
| 1232 |
/* Update new_inode ei |
| 1233 |
* Prevent syncing removed new_inode |
| 1234 |
* (new_ei is already initialized above code ("if (new_inode)") |
| 1235 |
*/ |
| 1236 |
new_ei->dir.dir = DIR_DELETED; |
| 1237 |
} |
| 1238 |
out: |
| 1239 |
return ret; |
| 1240 |
} |
| 1241 |
|
| 1242 |
static int exfat_rename(struct mnt_idmap *idmap, |
| 1243 |
struct inode *old_dir, struct dentry *old_dentry, |
| 1244 |
struct inode *new_dir, struct dentry *new_dentry, |
| 1245 |
unsigned int flags) |
| 1246 |
{ |
| 1247 |
struct inode *old_inode, *new_inode; |
| 1248 |
struct super_block *sb = old_dir->i_sb; |
| 1249 |
loff_t i_pos; |
| 1250 |
int err; |
| 1251 |
loff_t size = i_size_read(new_dir); |
| 1252 |
|
| 1253 |
/* |
| 1254 |
* The VFS already checks for existence, so for local filesystems |
| 1255 |
* the RENAME_NOREPLACE implementation is equivalent to plain rename. |
| 1256 |
* Don't support any other flags |
| 1257 |
*/ |
| 1258 |
if (flags & ~RENAME_NOREPLACE) |
| 1259 |
return -EINVAL; |
| 1260 |
|
| 1261 |
mutex_lock(&EXFAT_SB(sb)->s_lock); |
| 1262 |
old_inode = old_dentry->d_inode; |
| 1263 |
new_inode = new_dentry->d_inode; |
| 1264 |
|
| 1265 |
err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry); |
| 1266 |
if (err) |
| 1267 |
goto unlock; |
| 1268 |
|
| 1269 |
inode_inc_iversion(new_dir); |
| 1270 |
simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); |
| 1271 |
EXFAT_I(new_dir)->i_crtime = current_time(new_dir); |
| 1272 |
exfat_truncate_inode_atime(new_dir); |
| 1273 |
if (IS_DIRSYNC(new_dir) && size != i_size_read(new_dir)) |
| 1274 |
exfat_sync_inode(new_dir); |
| 1275 |
else |
| 1276 |
mark_inode_dirty(new_dir); |
| 1277 |
|
| 1278 |
i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) | |
| 1279 |
(EXFAT_I(old_inode)->entry & 0xffffffff); |
| 1280 |
exfat_unhash_inode(old_inode); |
| 1281 |
exfat_hash_inode(old_inode, i_pos); |
| 1282 |
if (IS_DIRSYNC(new_dir)) |
| 1283 |
exfat_sync_inode(old_inode); |
| 1284 |
else |
| 1285 |
mark_inode_dirty(old_inode); |
| 1286 |
|
| 1287 |
if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) { |
| 1288 |
drop_nlink(old_dir); |
| 1289 |
if (!new_inode) |
| 1290 |
inc_nlink(new_dir); |
| 1291 |
} |
| 1292 |
|
| 1293 |
inode_inc_iversion(old_dir); |
| 1294 |
if (new_dir != old_dir) |
| 1295 |
mark_inode_dirty(old_dir); |
| 1296 |
|
| 1297 |
if (new_inode) { |
| 1298 |
exfat_unhash_inode(new_inode); |
| 1299 |
|
| 1300 |
/* skip drop_nlink if new_inode already has been dropped */ |
| 1301 |
if (new_inode->i_nlink) { |
| 1302 |
drop_nlink(new_inode); |
| 1303 |
if (S_ISDIR(new_inode->i_mode)) |
| 1304 |
drop_nlink(new_inode); |
| 1305 |
} else { |
| 1306 |
exfat_warn(sb, "abnormal access to an inode dropped"); |
| 1307 |
WARN_ON(new_inode->i_nlink == 0); |
| 1308 |
} |
| 1309 |
EXFAT_I(new_inode)->i_crtime = current_time(new_inode); |
| 1310 |
} |
| 1311 |
|
| 1312 |
unlock: |
| 1313 |
mutex_unlock(&EXFAT_SB(sb)->s_lock); |
| 1314 |
return err; |
| 1315 |
} |
| 1316 |
|
| 1317 |
const struct inode_operations exfat_dir_inode_operations = { |
| 1318 |
.create = exfat_create, |
| 1319 |
.lookup = exfat_lookup, |
| 1320 |
.unlink = exfat_unlink, |
| 1321 |
.mkdir = exfat_mkdir, |
| 1322 |
.rmdir = exfat_rmdir, |
| 1323 |
.rename = exfat_rename, |
| 1324 |
.setattr = exfat_setattr, |
| 1325 |
.getattr = exfat_getattr, |
| 1326 |
}; |