| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* NILFS directory entry operations |
| 4 |
* |
| 5 |
* Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. |
| 6 |
* |
| 7 |
* Modified for NILFS by Amagai Yoshiji. |
| 8 |
*/ |
| 9 |
/* |
| 10 |
* linux/fs/ext2/dir.c |
| 11 |
* |
| 12 |
* Copyright (C) 1992, 1993, 1994, 1995 |
| 13 |
* Remy Card ([email protected]) |
| 14 |
* Laboratoire MASI - Institut Blaise Pascal |
| 15 |
* Universite Pierre et Marie Curie (Paris VI) |
| 16 |
* |
| 17 |
* from |
| 18 |
* |
| 19 |
* linux/fs/minix/dir.c |
| 20 |
* |
| 21 |
* Copyright (C) 1991, 1992 Linus Torvalds |
| 22 |
* |
| 23 |
* ext2 directory handling functions |
| 24 |
* |
| 25 |
* Big-endian to little-endian byte-swapping/bitmaps by |
| 26 |
* David S. Miller ([email protected]), 1995 |
| 27 |
* |
| 28 |
* All code that works with directory layout had been switched to pagecache |
| 29 |
* and moved here. AV |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <linux/pagemap.h> |
| 33 |
#include <linux/filelock.h> |
| 34 |
#include "nilfs.h" |
| 35 |
#include "page.h" |
| 36 |
|
| 37 |
static inline unsigned int nilfs_rec_len_from_disk(__le16 dlen) |
| 38 |
{ |
| 39 |
unsigned int len = le16_to_cpu(dlen); |
| 40 |
|
| 41 |
#if (PAGE_SIZE >= 65536) |
| 42 |
if (len == NILFS_MAX_REC_LEN) |
| 43 |
return 1 << 16; |
| 44 |
#endif |
| 45 |
return len; |
| 46 |
} |
| 47 |
|
| 48 |
static inline __le16 nilfs_rec_len_to_disk(unsigned int len) |
| 49 |
{ |
| 50 |
#if (PAGE_SIZE >= 65536) |
| 51 |
if (len == (1 << 16)) |
| 52 |
return cpu_to_le16(NILFS_MAX_REC_LEN); |
| 53 |
|
| 54 |
BUG_ON(len > (1 << 16)); |
| 55 |
#endif |
| 56 |
return cpu_to_le16(len); |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* nilfs uses block-sized chunks. Arguably, sector-sized ones would be |
| 61 |
* more robust, but we have what we have |
| 62 |
*/ |
| 63 |
static inline unsigned int nilfs_chunk_size(struct inode *inode) |
| 64 |
{ |
| 65 |
return inode->i_sb->s_blocksize; |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Return the offset into page `page_nr' of the last valid |
| 70 |
* byte in that page, plus one. |
| 71 |
*/ |
| 72 |
static unsigned int nilfs_last_byte(struct inode *inode, unsigned long page_nr) |
| 73 |
{ |
| 74 |
u64 last_byte = inode->i_size; |
| 75 |
|
| 76 |
last_byte -= page_nr << PAGE_SHIFT; |
| 77 |
if (last_byte > PAGE_SIZE) |
| 78 |
last_byte = PAGE_SIZE; |
| 79 |
return last_byte; |
| 80 |
} |
| 81 |
|
| 82 |
static int nilfs_prepare_chunk(struct folio *folio, unsigned int from, |
| 83 |
unsigned int to) |
| 84 |
{ |
| 85 |
loff_t pos = folio_pos(folio) + from; |
| 86 |
|
| 87 |
return __block_write_begin(folio, pos, to - from, nilfs_get_block); |
| 88 |
} |
| 89 |
|
| 90 |
static void nilfs_commit_chunk(struct folio *folio, |
| 91 |
struct address_space *mapping, size_t from, size_t to) |
| 92 |
{ |
| 93 |
struct inode *dir = mapping->host; |
| 94 |
loff_t pos = folio_pos(folio) + from; |
| 95 |
size_t copied, len = to - from; |
| 96 |
unsigned int nr_dirty; |
| 97 |
int err; |
| 98 |
|
| 99 |
nr_dirty = nilfs_page_count_clean_buffers(folio, from, to); |
| 100 |
copied = block_write_end(pos, len, len, folio); |
| 101 |
if (pos + copied > dir->i_size) |
| 102 |
i_size_write(dir, pos + copied); |
| 103 |
if (IS_DIRSYNC(dir)) |
| 104 |
nilfs_set_transaction_flag(NILFS_TI_SYNC); |
| 105 |
err = nilfs_set_file_dirty(dir, nr_dirty); |
| 106 |
WARN_ON(err); /* do not happen */ |
| 107 |
folio_unlock(folio); |
| 108 |
} |
| 109 |
|
| 110 |
static bool nilfs_check_folio(struct folio *folio, char *kaddr) |
| 111 |
{ |
| 112 |
struct inode *dir = folio->mapping->host; |
| 113 |
struct super_block *sb = dir->i_sb; |
| 114 |
unsigned int chunk_size = nilfs_chunk_size(dir); |
| 115 |
size_t offs, rec_len; |
| 116 |
size_t limit = folio_size(folio); |
| 117 |
struct nilfs_dir_entry *p; |
| 118 |
char *error; |
| 119 |
|
| 120 |
if (dir->i_size < folio_pos(folio) + limit) { |
| 121 |
limit = dir->i_size - folio_pos(folio); |
| 122 |
if (limit & (chunk_size - 1)) |
| 123 |
goto Ebadsize; |
| 124 |
if (!limit) |
| 125 |
goto out; |
| 126 |
} |
| 127 |
for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) { |
| 128 |
p = (struct nilfs_dir_entry *)(kaddr + offs); |
| 129 |
rec_len = nilfs_rec_len_from_disk(p->rec_len); |
| 130 |
|
| 131 |
if (rec_len < NILFS_DIR_REC_LEN(1)) |
| 132 |
goto Eshort; |
| 133 |
if (rec_len & 3) |
| 134 |
goto Ealign; |
| 135 |
if (rec_len < NILFS_DIR_REC_LEN(p->name_len)) |
| 136 |
goto Enamelen; |
| 137 |
if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)) |
| 138 |
goto Espan; |
| 139 |
if (unlikely(p->inode && |
| 140 |
NILFS_PRIVATE_INODE(le64_to_cpu(p->inode)))) |
| 141 |
goto Einumber; |
| 142 |
} |
| 143 |
if (offs != limit) |
| 144 |
goto Eend; |
| 145 |
out: |
| 146 |
folio_set_checked(folio); |
| 147 |
return true; |
| 148 |
|
| 149 |
/* Too bad, we had an error */ |
| 150 |
|
| 151 |
Ebadsize: |
| 152 |
nilfs_error(sb, |
| 153 |
"size of directory #%lu is not a multiple of chunk size", |
| 154 |
dir->i_ino); |
| 155 |
goto fail; |
| 156 |
Eshort: |
| 157 |
error = "rec_len is smaller than minimal"; |
| 158 |
goto bad_entry; |
| 159 |
Ealign: |
| 160 |
error = "unaligned directory entry"; |
| 161 |
goto bad_entry; |
| 162 |
Enamelen: |
| 163 |
error = "rec_len is too small for name_len"; |
| 164 |
goto bad_entry; |
| 165 |
Espan: |
| 166 |
error = "directory entry across blocks"; |
| 167 |
goto bad_entry; |
| 168 |
Einumber: |
| 169 |
error = "disallowed inode number"; |
| 170 |
bad_entry: |
| 171 |
nilfs_error(sb, |
| 172 |
"bad entry in directory #%lu: %s - offset=%lu, inode=%lu, rec_len=%zd, name_len=%d", |
| 173 |
dir->i_ino, error, (folio->index << PAGE_SHIFT) + offs, |
| 174 |
(unsigned long)le64_to_cpu(p->inode), |
| 175 |
rec_len, p->name_len); |
| 176 |
goto fail; |
| 177 |
Eend: |
| 178 |
p = (struct nilfs_dir_entry *)(kaddr + offs); |
| 179 |
nilfs_error(sb, |
| 180 |
"entry in directory #%lu spans the page boundary offset=%lu, inode=%lu", |
| 181 |
dir->i_ino, (folio->index << PAGE_SHIFT) + offs, |
| 182 |
(unsigned long)le64_to_cpu(p->inode)); |
| 183 |
fail: |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
static void *nilfs_get_folio(struct inode *dir, unsigned long n, |
| 188 |
struct folio **foliop) |
| 189 |
{ |
| 190 |
struct address_space *mapping = dir->i_mapping; |
| 191 |
struct folio *folio = read_mapping_folio(mapping, n, NULL); |
| 192 |
void *kaddr; |
| 193 |
|
| 194 |
if (IS_ERR(folio)) |
| 195 |
return folio; |
| 196 |
|
| 197 |
kaddr = kmap_local_folio(folio, 0); |
| 198 |
if (unlikely(!folio_test_checked(folio))) { |
| 199 |
if (!nilfs_check_folio(folio, kaddr)) |
| 200 |
goto fail; |
| 201 |
} |
| 202 |
|
| 203 |
*foliop = folio; |
| 204 |
return kaddr; |
| 205 |
|
| 206 |
fail: |
| 207 |
folio_release_kmap(folio, kaddr); |
| 208 |
return ERR_PTR(-EIO); |
| 209 |
} |
| 210 |
|
| 211 |
/* |
| 212 |
* NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure. |
| 213 |
* |
| 214 |
* len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller. |
| 215 |
*/ |
| 216 |
static int |
| 217 |
nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de) |
| 218 |
{ |
| 219 |
if (len != de->name_len) |
| 220 |
return 0; |
| 221 |
if (!de->inode) |
| 222 |
return 0; |
| 223 |
return !memcmp(name, de->name, len); |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* p is at least 6 bytes before the end of page |
| 228 |
*/ |
| 229 |
static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p) |
| 230 |
{ |
| 231 |
return (struct nilfs_dir_entry *)((char *)p + |
| 232 |
nilfs_rec_len_from_disk(p->rec_len)); |
| 233 |
} |
| 234 |
|
| 235 |
static int nilfs_readdir(struct file *file, struct dir_context *ctx) |
| 236 |
{ |
| 237 |
loff_t pos = ctx->pos; |
| 238 |
struct inode *inode = file_inode(file); |
| 239 |
struct super_block *sb = inode->i_sb; |
| 240 |
unsigned int offset = pos & ~PAGE_MASK; |
| 241 |
unsigned long n = pos >> PAGE_SHIFT; |
| 242 |
unsigned long npages = dir_pages(inode); |
| 243 |
|
| 244 |
if (pos > inode->i_size - NILFS_DIR_REC_LEN(1)) |
| 245 |
return 0; |
| 246 |
|
| 247 |
for ( ; n < npages; n++, offset = 0) { |
| 248 |
char *kaddr, *limit; |
| 249 |
struct nilfs_dir_entry *de; |
| 250 |
struct folio *folio; |
| 251 |
|
| 252 |
kaddr = nilfs_get_folio(inode, n, &folio); |
| 253 |
if (IS_ERR(kaddr)) { |
| 254 |
nilfs_error(sb, "bad page in #%lu", inode->i_ino); |
| 255 |
ctx->pos += PAGE_SIZE - offset; |
| 256 |
return -EIO; |
| 257 |
} |
| 258 |
de = (struct nilfs_dir_entry *)(kaddr + offset); |
| 259 |
limit = kaddr + nilfs_last_byte(inode, n) - |
| 260 |
NILFS_DIR_REC_LEN(1); |
| 261 |
for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) { |
| 262 |
if (de->rec_len == 0) { |
| 263 |
nilfs_error(sb, "zero-length directory entry"); |
| 264 |
folio_release_kmap(folio, kaddr); |
| 265 |
return -EIO; |
| 266 |
} |
| 267 |
if (de->inode) { |
| 268 |
unsigned char t; |
| 269 |
|
| 270 |
t = fs_ftype_to_dtype(de->file_type); |
| 271 |
|
| 272 |
if (!dir_emit(ctx, de->name, de->name_len, |
| 273 |
le64_to_cpu(de->inode), t)) { |
| 274 |
folio_release_kmap(folio, kaddr); |
| 275 |
return 0; |
| 276 |
} |
| 277 |
} |
| 278 |
ctx->pos += nilfs_rec_len_from_disk(de->rec_len); |
| 279 |
} |
| 280 |
folio_release_kmap(folio, kaddr); |
| 281 |
} |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* nilfs_find_entry() |
| 287 |
* |
| 288 |
* Finds an entry in the specified directory with the wanted name. It |
| 289 |
* returns the folio in which the entry was found, and the entry itself. |
| 290 |
* The folio is mapped and unlocked. When the caller is finished with |
| 291 |
* the entry, it should call folio_release_kmap(). |
| 292 |
* |
| 293 |
* On failure, returns an error pointer and the caller should ignore foliop. |
| 294 |
*/ |
| 295 |
struct nilfs_dir_entry *nilfs_find_entry(struct inode *dir, |
| 296 |
const struct qstr *qstr, struct folio **foliop) |
| 297 |
{ |
| 298 |
const unsigned char *name = qstr->name; |
| 299 |
int namelen = qstr->len; |
| 300 |
unsigned int reclen = NILFS_DIR_REC_LEN(namelen); |
| 301 |
unsigned long start, n; |
| 302 |
unsigned long npages = dir_pages(dir); |
| 303 |
struct nilfs_inode_info *ei = NILFS_I(dir); |
| 304 |
struct nilfs_dir_entry *de; |
| 305 |
|
| 306 |
if (npages == 0) |
| 307 |
goto out; |
| 308 |
|
| 309 |
start = ei->i_dir_start_lookup; |
| 310 |
if (start >= npages) |
| 311 |
start = 0; |
| 312 |
n = start; |
| 313 |
do { |
| 314 |
char *kaddr = nilfs_get_folio(dir, n, foliop); |
| 315 |
|
| 316 |
if (IS_ERR(kaddr)) |
| 317 |
return ERR_CAST(kaddr); |
| 318 |
|
| 319 |
de = (struct nilfs_dir_entry *)kaddr; |
| 320 |
kaddr += nilfs_last_byte(dir, n) - reclen; |
| 321 |
while ((char *)de <= kaddr) { |
| 322 |
if (de->rec_len == 0) { |
| 323 |
nilfs_error(dir->i_sb, |
| 324 |
"zero-length directory entry"); |
| 325 |
folio_release_kmap(*foliop, kaddr); |
| 326 |
goto out; |
| 327 |
} |
| 328 |
if (nilfs_match(namelen, name, de)) |
| 329 |
goto found; |
| 330 |
de = nilfs_next_entry(de); |
| 331 |
} |
| 332 |
folio_release_kmap(*foliop, kaddr); |
| 333 |
|
| 334 |
if (++n >= npages) |
| 335 |
n = 0; |
| 336 |
/* next folio is past the blocks we've got */ |
| 337 |
if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) { |
| 338 |
nilfs_error(dir->i_sb, |
| 339 |
"dir %lu size %lld exceeds block count %llu", |
| 340 |
dir->i_ino, dir->i_size, |
| 341 |
(unsigned long long)dir->i_blocks); |
| 342 |
goto out; |
| 343 |
} |
| 344 |
} while (n != start); |
| 345 |
out: |
| 346 |
return ERR_PTR(-ENOENT); |
| 347 |
|
| 348 |
found: |
| 349 |
ei->i_dir_start_lookup = n; |
| 350 |
return de; |
| 351 |
} |
| 352 |
|
| 353 |
struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct folio **foliop) |
| 354 |
{ |
| 355 |
struct folio *folio; |
| 356 |
struct nilfs_dir_entry *de, *next_de; |
| 357 |
size_t limit; |
| 358 |
char *msg; |
| 359 |
|
| 360 |
de = nilfs_get_folio(dir, 0, &folio); |
| 361 |
if (IS_ERR(de)) |
| 362 |
return NULL; |
| 363 |
|
| 364 |
limit = nilfs_last_byte(dir, 0); /* is a multiple of chunk size */ |
| 365 |
if (unlikely(!limit || le64_to_cpu(de->inode) != dir->i_ino || |
| 366 |
!nilfs_match(1, ".", de))) { |
| 367 |
msg = "missing '.'"; |
| 368 |
goto fail; |
| 369 |
} |
| 370 |
|
| 371 |
next_de = nilfs_next_entry(de); |
| 372 |
/* |
| 373 |
* If "next_de" has not reached the end of the chunk, there is |
| 374 |
* at least one more record. Check whether it matches "..". |
| 375 |
*/ |
| 376 |
if (unlikely((char *)next_de == (char *)de + nilfs_chunk_size(dir) || |
| 377 |
!nilfs_match(2, "..", next_de))) { |
| 378 |
msg = "missing '..'"; |
| 379 |
goto fail; |
| 380 |
} |
| 381 |
*foliop = folio; |
| 382 |
return next_de; |
| 383 |
|
| 384 |
fail: |
| 385 |
nilfs_error(dir->i_sb, "directory #%lu %s", dir->i_ino, msg); |
| 386 |
folio_release_kmap(folio, de); |
| 387 |
return NULL; |
| 388 |
} |
| 389 |
|
| 390 |
int nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr, ino_t *ino) |
| 391 |
{ |
| 392 |
struct nilfs_dir_entry *de; |
| 393 |
struct folio *folio; |
| 394 |
|
| 395 |
de = nilfs_find_entry(dir, qstr, &folio); |
| 396 |
if (IS_ERR(de)) |
| 397 |
return PTR_ERR(de); |
| 398 |
|
| 399 |
*ino = le64_to_cpu(de->inode); |
| 400 |
folio_release_kmap(folio, de); |
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
int nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de, |
| 405 |
struct folio *folio, struct inode *inode) |
| 406 |
{ |
| 407 |
size_t from = offset_in_folio(folio, de); |
| 408 |
size_t to = from + nilfs_rec_len_from_disk(de->rec_len); |
| 409 |
struct address_space *mapping = folio->mapping; |
| 410 |
int err; |
| 411 |
|
| 412 |
folio_lock(folio); |
| 413 |
err = nilfs_prepare_chunk(folio, from, to); |
| 414 |
if (unlikely(err)) { |
| 415 |
folio_unlock(folio); |
| 416 |
return err; |
| 417 |
} |
| 418 |
de->inode = cpu_to_le64(inode->i_ino); |
| 419 |
de->file_type = fs_umode_to_ftype(inode->i_mode); |
| 420 |
nilfs_commit_chunk(folio, mapping, from, to); |
| 421 |
inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); |
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
/* |
| 426 |
* Parent is locked. |
| 427 |
*/ |
| 428 |
int nilfs_add_link(struct dentry *dentry, struct inode *inode) |
| 429 |
{ |
| 430 |
struct inode *dir = d_inode(dentry->d_parent); |
| 431 |
const unsigned char *name = dentry->d_name.name; |
| 432 |
int namelen = dentry->d_name.len; |
| 433 |
unsigned int chunk_size = nilfs_chunk_size(dir); |
| 434 |
unsigned int reclen = NILFS_DIR_REC_LEN(namelen); |
| 435 |
unsigned short rec_len, name_len; |
| 436 |
struct folio *folio = NULL; |
| 437 |
struct nilfs_dir_entry *de; |
| 438 |
unsigned long npages = dir_pages(dir); |
| 439 |
unsigned long n; |
| 440 |
size_t from, to; |
| 441 |
int err; |
| 442 |
|
| 443 |
/* |
| 444 |
* We take care of directory expansion in the same loop. |
| 445 |
* This code plays outside i_size, so it locks the folio |
| 446 |
* to protect that region. |
| 447 |
*/ |
| 448 |
for (n = 0; n <= npages; n++) { |
| 449 |
char *kaddr = nilfs_get_folio(dir, n, &folio); |
| 450 |
char *dir_end; |
| 451 |
|
| 452 |
if (IS_ERR(kaddr)) |
| 453 |
return PTR_ERR(kaddr); |
| 454 |
folio_lock(folio); |
| 455 |
dir_end = kaddr + nilfs_last_byte(dir, n); |
| 456 |
de = (struct nilfs_dir_entry *)kaddr; |
| 457 |
kaddr += folio_size(folio) - reclen; |
| 458 |
while ((char *)de <= kaddr) { |
| 459 |
if ((char *)de == dir_end) { |
| 460 |
/* We hit i_size */ |
| 461 |
name_len = 0; |
| 462 |
rec_len = chunk_size; |
| 463 |
de->rec_len = nilfs_rec_len_to_disk(chunk_size); |
| 464 |
de->inode = 0; |
| 465 |
goto got_it; |
| 466 |
} |
| 467 |
if (de->rec_len == 0) { |
| 468 |
nilfs_error(dir->i_sb, |
| 469 |
"zero-length directory entry"); |
| 470 |
err = -EIO; |
| 471 |
goto out_unlock; |
| 472 |
} |
| 473 |
err = -EEXIST; |
| 474 |
if (nilfs_match(namelen, name, de)) |
| 475 |
goto out_unlock; |
| 476 |
name_len = NILFS_DIR_REC_LEN(de->name_len); |
| 477 |
rec_len = nilfs_rec_len_from_disk(de->rec_len); |
| 478 |
if (!de->inode && rec_len >= reclen) |
| 479 |
goto got_it; |
| 480 |
if (rec_len >= name_len + reclen) |
| 481 |
goto got_it; |
| 482 |
de = (struct nilfs_dir_entry *)((char *)de + rec_len); |
| 483 |
} |
| 484 |
folio_unlock(folio); |
| 485 |
folio_release_kmap(folio, kaddr); |
| 486 |
} |
| 487 |
BUG(); |
| 488 |
return -EINVAL; |
| 489 |
|
| 490 |
got_it: |
| 491 |
from = offset_in_folio(folio, de); |
| 492 |
to = from + rec_len; |
| 493 |
err = nilfs_prepare_chunk(folio, from, to); |
| 494 |
if (err) |
| 495 |
goto out_unlock; |
| 496 |
if (de->inode) { |
| 497 |
struct nilfs_dir_entry *de1; |
| 498 |
|
| 499 |
de1 = (struct nilfs_dir_entry *)((char *)de + name_len); |
| 500 |
de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len); |
| 501 |
de->rec_len = nilfs_rec_len_to_disk(name_len); |
| 502 |
de = de1; |
| 503 |
} |
| 504 |
de->name_len = namelen; |
| 505 |
memcpy(de->name, name, namelen); |
| 506 |
de->inode = cpu_to_le64(inode->i_ino); |
| 507 |
de->file_type = fs_umode_to_ftype(inode->i_mode); |
| 508 |
nilfs_commit_chunk(folio, folio->mapping, from, to); |
| 509 |
inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); |
| 510 |
nilfs_mark_inode_dirty(dir); |
| 511 |
/* OFFSET_CACHE */ |
| 512 |
out_put: |
| 513 |
folio_release_kmap(folio, de); |
| 514 |
return err; |
| 515 |
out_unlock: |
| 516 |
folio_unlock(folio); |
| 517 |
goto out_put; |
| 518 |
} |
| 519 |
|
| 520 |
/* |
| 521 |
* nilfs_delete_entry deletes a directory entry by merging it with the |
| 522 |
* previous entry. Folio is up-to-date. |
| 523 |
*/ |
| 524 |
int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct folio *folio) |
| 525 |
{ |
| 526 |
struct address_space *mapping = folio->mapping; |
| 527 |
struct inode *inode = mapping->host; |
| 528 |
char *kaddr = (char *)((unsigned long)dir & ~(folio_size(folio) - 1)); |
| 529 |
size_t from, to; |
| 530 |
struct nilfs_dir_entry *de, *pde = NULL; |
| 531 |
int err; |
| 532 |
|
| 533 |
from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1); |
| 534 |
to = ((char *)dir - kaddr) + nilfs_rec_len_from_disk(dir->rec_len); |
| 535 |
de = (struct nilfs_dir_entry *)(kaddr + from); |
| 536 |
|
| 537 |
while ((char *)de < (char *)dir) { |
| 538 |
if (de->rec_len == 0) { |
| 539 |
nilfs_error(inode->i_sb, |
| 540 |
"zero-length directory entry"); |
| 541 |
err = -EIO; |
| 542 |
goto out; |
| 543 |
} |
| 544 |
pde = de; |
| 545 |
de = nilfs_next_entry(de); |
| 546 |
} |
| 547 |
if (pde) |
| 548 |
from = (char *)pde - kaddr; |
| 549 |
folio_lock(folio); |
| 550 |
err = nilfs_prepare_chunk(folio, from, to); |
| 551 |
if (unlikely(err)) { |
| 552 |
folio_unlock(folio); |
| 553 |
goto out; |
| 554 |
} |
| 555 |
if (pde) |
| 556 |
pde->rec_len = nilfs_rec_len_to_disk(to - from); |
| 557 |
dir->inode = 0; |
| 558 |
nilfs_commit_chunk(folio, mapping, from, to); |
| 559 |
inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 560 |
out: |
| 561 |
return err; |
| 562 |
} |
| 563 |
|
| 564 |
/* |
| 565 |
* Set the first fragment of directory. |
| 566 |
*/ |
| 567 |
int nilfs_make_empty(struct inode *inode, struct inode *parent) |
| 568 |
{ |
| 569 |
struct address_space *mapping = inode->i_mapping; |
| 570 |
struct folio *folio = filemap_grab_folio(mapping, 0); |
| 571 |
unsigned int chunk_size = nilfs_chunk_size(inode); |
| 572 |
struct nilfs_dir_entry *de; |
| 573 |
int err; |
| 574 |
void *kaddr; |
| 575 |
|
| 576 |
if (IS_ERR(folio)) |
| 577 |
return PTR_ERR(folio); |
| 578 |
|
| 579 |
err = nilfs_prepare_chunk(folio, 0, chunk_size); |
| 580 |
if (unlikely(err)) { |
| 581 |
folio_unlock(folio); |
| 582 |
goto fail; |
| 583 |
} |
| 584 |
kaddr = kmap_local_folio(folio, 0); |
| 585 |
memset(kaddr, 0, chunk_size); |
| 586 |
de = (struct nilfs_dir_entry *)kaddr; |
| 587 |
de->name_len = 1; |
| 588 |
de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1)); |
| 589 |
memcpy(de->name, ".\0\0", 4); |
| 590 |
de->inode = cpu_to_le64(inode->i_ino); |
| 591 |
de->file_type = fs_umode_to_ftype(inode->i_mode); |
| 592 |
|
| 593 |
de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1)); |
| 594 |
de->name_len = 2; |
| 595 |
de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1)); |
| 596 |
de->inode = cpu_to_le64(parent->i_ino); |
| 597 |
memcpy(de->name, "..\0", 4); |
| 598 |
de->file_type = fs_umode_to_ftype(inode->i_mode); |
| 599 |
kunmap_local(kaddr); |
| 600 |
nilfs_commit_chunk(folio, mapping, 0, chunk_size); |
| 601 |
fail: |
| 602 |
folio_put(folio); |
| 603 |
return err; |
| 604 |
} |
| 605 |
|
| 606 |
/* |
| 607 |
* routine to check that the specified directory is empty (for rmdir) |
| 608 |
*/ |
| 609 |
int nilfs_empty_dir(struct inode *inode) |
| 610 |
{ |
| 611 |
struct folio *folio = NULL; |
| 612 |
char *kaddr; |
| 613 |
unsigned long i, npages = dir_pages(inode); |
| 614 |
|
| 615 |
for (i = 0; i < npages; i++) { |
| 616 |
struct nilfs_dir_entry *de; |
| 617 |
|
| 618 |
kaddr = nilfs_get_folio(inode, i, &folio); |
| 619 |
if (IS_ERR(kaddr)) |
| 620 |
return 0; |
| 621 |
|
| 622 |
de = (struct nilfs_dir_entry *)kaddr; |
| 623 |
kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1); |
| 624 |
|
| 625 |
while ((char *)de <= kaddr) { |
| 626 |
if (de->rec_len == 0) { |
| 627 |
nilfs_error(inode->i_sb, |
| 628 |
"zero-length directory entry (kaddr=%p, de=%p)", |
| 629 |
kaddr, de); |
| 630 |
goto not_empty; |
| 631 |
} |
| 632 |
if (de->inode != 0) { |
| 633 |
/* check for . and .. */ |
| 634 |
if (de->name[0] != '.') |
| 635 |
goto not_empty; |
| 636 |
if (de->name_len > 2) |
| 637 |
goto not_empty; |
| 638 |
if (de->name_len < 2) { |
| 639 |
if (de->inode != |
| 640 |
cpu_to_le64(inode->i_ino)) |
| 641 |
goto not_empty; |
| 642 |
} else if (de->name[1] != '.') |
| 643 |
goto not_empty; |
| 644 |
} |
| 645 |
de = nilfs_next_entry(de); |
| 646 |
} |
| 647 |
folio_release_kmap(folio, kaddr); |
| 648 |
} |
| 649 |
return 1; |
| 650 |
|
| 651 |
not_empty: |
| 652 |
folio_release_kmap(folio, kaddr); |
| 653 |
return 0; |
| 654 |
} |
| 655 |
|
| 656 |
const struct file_operations nilfs_dir_operations = { |
| 657 |
.llseek = generic_file_llseek, |
| 658 |
.read = generic_read_dir, |
| 659 |
.iterate_shared = nilfs_readdir, |
| 660 |
.unlocked_ioctl = nilfs_ioctl, |
| 661 |
#ifdef CONFIG_COMPAT |
| 662 |
.compat_ioctl = nilfs_compat_ioctl, |
| 663 |
#endif /* CONFIG_COMPAT */ |
| 664 |
.fsync = nilfs_sync_file, |
| 665 |
.setlease = generic_setlease, |
| 666 |
}; |