Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dir.c - NILFS directory entry operations |
| 3 | * |
| 4 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net> |
| 21 | */ |
| 22 | /* |
| 23 | * linux/fs/ext2/dir.c |
| 24 | * |
| 25 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 26 | * Remy Card (card@masi.ibp.fr) |
| 27 | * Laboratoire MASI - Institut Blaise Pascal |
| 28 | * Universite Pierre et Marie Curie (Paris VI) |
| 29 | * |
| 30 | * from |
| 31 | * |
| 32 | * linux/fs/minix/dir.c |
| 33 | * |
| 34 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 35 | * |
| 36 | * ext2 directory handling functions |
| 37 | * |
| 38 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 39 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 40 | * |
| 41 | * All code that works with directory layout had been switched to pagecache |
| 42 | * and moved here. AV |
| 43 | */ |
| 44 | |
| 45 | #include <linux/pagemap.h> |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 46 | #include "nilfs.h" |
| 47 | #include "page.h" |
| 48 | |
| 49 | /* |
| 50 | * nilfs uses block-sized chunks. Arguably, sector-sized ones would be |
| 51 | * more robust, but we have what we have |
| 52 | */ |
| 53 | static inline unsigned nilfs_chunk_size(struct inode *inode) |
| 54 | { |
| 55 | return inode->i_sb->s_blocksize; |
| 56 | } |
| 57 | |
| 58 | static inline void nilfs_put_page(struct page *page) |
| 59 | { |
| 60 | kunmap(page); |
| 61 | page_cache_release(page); |
| 62 | } |
| 63 | |
| 64 | static inline unsigned long dir_pages(struct inode *inode) |
| 65 | { |
| 66 | return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Return the offset into page `page_nr' of the last valid |
| 71 | * byte in that page, plus one. |
| 72 | */ |
| 73 | static unsigned nilfs_last_byte(struct inode *inode, unsigned long page_nr) |
| 74 | { |
| 75 | unsigned last_byte = inode->i_size; |
| 76 | |
| 77 | last_byte -= page_nr << PAGE_CACHE_SHIFT; |
| 78 | if (last_byte > PAGE_CACHE_SIZE) |
| 79 | last_byte = PAGE_CACHE_SIZE; |
| 80 | return last_byte; |
| 81 | } |
| 82 | |
| 83 | static int nilfs_prepare_chunk_uninterruptible(struct page *page, |
| 84 | struct address_space *mapping, |
| 85 | unsigned from, unsigned to) |
| 86 | { |
| 87 | loff_t pos = page_offset(page) + from; |
| 88 | return block_write_begin(NULL, mapping, pos, to - from, |
| 89 | AOP_FLAG_UNINTERRUPTIBLE, &page, |
| 90 | NULL, nilfs_get_block); |
| 91 | } |
| 92 | |
| 93 | static int nilfs_prepare_chunk(struct page *page, |
| 94 | struct address_space *mapping, |
| 95 | unsigned from, unsigned to) |
| 96 | { |
| 97 | loff_t pos = page_offset(page) + from; |
| 98 | return block_write_begin(NULL, mapping, pos, to - from, 0, &page, |
| 99 | NULL, nilfs_get_block); |
| 100 | } |
| 101 | |
Jiro SEKIBA | 2093abf | 2009-11-27 19:41:11 +0900 | [diff] [blame] | 102 | static void nilfs_commit_chunk(struct page *page, |
| 103 | struct address_space *mapping, |
| 104 | unsigned from, unsigned to) |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 105 | { |
| 106 | struct inode *dir = mapping->host; |
| 107 | struct nilfs_sb_info *sbi = NILFS_SB(dir->i_sb); |
| 108 | loff_t pos = page_offset(page) + from; |
| 109 | unsigned len = to - from; |
| 110 | unsigned nr_dirty, copied; |
| 111 | int err; |
| 112 | |
| 113 | nr_dirty = nilfs_page_count_clean_buffers(page, from, to); |
| 114 | copied = block_write_end(NULL, mapping, pos, len, len, page, NULL); |
Jiro SEKIBA | 58d5547 | 2009-11-27 19:41:12 +0900 | [diff] [blame] | 115 | if (pos + copied > dir->i_size) |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 116 | i_size_write(dir, pos + copied); |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 117 | if (IS_DIRSYNC(dir)) |
| 118 | nilfs_set_transaction_flag(NILFS_TI_SYNC); |
| 119 | err = nilfs_set_file_dirty(sbi, dir, nr_dirty); |
Jiro SEKIBA | 2093abf | 2009-11-27 19:41:11 +0900 | [diff] [blame] | 120 | WARN_ON(err); /* do not happen */ |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 121 | unlock_page(page); |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void nilfs_check_page(struct page *page) |
| 125 | { |
| 126 | struct inode *dir = page->mapping->host; |
| 127 | struct super_block *sb = dir->i_sb; |
| 128 | unsigned chunk_size = nilfs_chunk_size(dir); |
| 129 | char *kaddr = page_address(page); |
| 130 | unsigned offs, rec_len; |
| 131 | unsigned limit = PAGE_CACHE_SIZE; |
| 132 | struct nilfs_dir_entry *p; |
| 133 | char *error; |
| 134 | |
| 135 | if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) { |
| 136 | limit = dir->i_size & ~PAGE_CACHE_MASK; |
| 137 | if (limit & (chunk_size - 1)) |
| 138 | goto Ebadsize; |
| 139 | if (!limit) |
| 140 | goto out; |
| 141 | } |
| 142 | for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) { |
| 143 | p = (struct nilfs_dir_entry *)(kaddr + offs); |
| 144 | rec_len = le16_to_cpu(p->rec_len); |
| 145 | |
| 146 | if (rec_len < NILFS_DIR_REC_LEN(1)) |
| 147 | goto Eshort; |
| 148 | if (rec_len & 3) |
| 149 | goto Ealign; |
| 150 | if (rec_len < NILFS_DIR_REC_LEN(p->name_len)) |
| 151 | goto Enamelen; |
| 152 | if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)) |
| 153 | goto Espan; |
| 154 | } |
| 155 | if (offs != limit) |
| 156 | goto Eend; |
| 157 | out: |
| 158 | SetPageChecked(page); |
| 159 | return; |
| 160 | |
| 161 | /* Too bad, we had an error */ |
| 162 | |
| 163 | Ebadsize: |
| 164 | nilfs_error(sb, "nilfs_check_page", |
| 165 | "size of directory #%lu is not a multiple of chunk size", |
| 166 | dir->i_ino |
| 167 | ); |
| 168 | goto fail; |
| 169 | Eshort: |
| 170 | error = "rec_len is smaller than minimal"; |
| 171 | goto bad_entry; |
| 172 | Ealign: |
| 173 | error = "unaligned directory entry"; |
| 174 | goto bad_entry; |
| 175 | Enamelen: |
| 176 | error = "rec_len is too small for name_len"; |
| 177 | goto bad_entry; |
| 178 | Espan: |
| 179 | error = "directory entry across blocks"; |
| 180 | bad_entry: |
| 181 | nilfs_error(sb, "nilfs_check_page", "bad entry in directory #%lu: %s - " |
| 182 | "offset=%lu, inode=%lu, rec_len=%d, name_len=%d", |
| 183 | dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs, |
| 184 | (unsigned long) le64_to_cpu(p->inode), |
| 185 | rec_len, p->name_len); |
| 186 | goto fail; |
| 187 | Eend: |
| 188 | p = (struct nilfs_dir_entry *)(kaddr + offs); |
| 189 | nilfs_error(sb, "nilfs_check_page", |
| 190 | "entry in directory #%lu spans the page boundary" |
| 191 | "offset=%lu, inode=%lu", |
| 192 | dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs, |
| 193 | (unsigned long) le64_to_cpu(p->inode)); |
| 194 | fail: |
| 195 | SetPageChecked(page); |
| 196 | SetPageError(page); |
| 197 | } |
| 198 | |
| 199 | static struct page *nilfs_get_page(struct inode *dir, unsigned long n) |
| 200 | { |
| 201 | struct address_space *mapping = dir->i_mapping; |
| 202 | struct page *page = read_cache_page(mapping, n, |
| 203 | (filler_t *)mapping->a_ops->readpage, NULL); |
| 204 | if (!IS_ERR(page)) { |
| 205 | wait_on_page_locked(page); |
| 206 | kmap(page); |
| 207 | if (!PageUptodate(page)) |
| 208 | goto fail; |
| 209 | if (!PageChecked(page)) |
| 210 | nilfs_check_page(page); |
| 211 | if (PageError(page)) |
| 212 | goto fail; |
| 213 | } |
| 214 | return page; |
| 215 | |
| 216 | fail: |
| 217 | nilfs_put_page(page); |
| 218 | return ERR_PTR(-EIO); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure. |
| 223 | * |
| 224 | * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller. |
| 225 | */ |
| 226 | static int |
Al Viro | 072f98b | 2010-01-31 21:03:58 -0500 | [diff] [blame] | 227 | nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de) |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 228 | { |
| 229 | if (len != de->name_len) |
| 230 | return 0; |
| 231 | if (!de->inode) |
| 232 | return 0; |
| 233 | return !memcmp(name, de->name, len); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * p is at least 6 bytes before the end of page |
| 238 | */ |
| 239 | static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p) |
| 240 | { |
| 241 | return (struct nilfs_dir_entry *)((char *)p + le16_to_cpu(p->rec_len)); |
| 242 | } |
| 243 | |
| 244 | static unsigned char |
| 245 | nilfs_filetype_table[NILFS_FT_MAX] = { |
| 246 | [NILFS_FT_UNKNOWN] = DT_UNKNOWN, |
| 247 | [NILFS_FT_REG_FILE] = DT_REG, |
| 248 | [NILFS_FT_DIR] = DT_DIR, |
| 249 | [NILFS_FT_CHRDEV] = DT_CHR, |
| 250 | [NILFS_FT_BLKDEV] = DT_BLK, |
| 251 | [NILFS_FT_FIFO] = DT_FIFO, |
| 252 | [NILFS_FT_SOCK] = DT_SOCK, |
| 253 | [NILFS_FT_SYMLINK] = DT_LNK, |
| 254 | }; |
| 255 | |
| 256 | #define S_SHIFT 12 |
| 257 | static unsigned char |
| 258 | nilfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 259 | [S_IFREG >> S_SHIFT] = NILFS_FT_REG_FILE, |
| 260 | [S_IFDIR >> S_SHIFT] = NILFS_FT_DIR, |
| 261 | [S_IFCHR >> S_SHIFT] = NILFS_FT_CHRDEV, |
| 262 | [S_IFBLK >> S_SHIFT] = NILFS_FT_BLKDEV, |
| 263 | [S_IFIFO >> S_SHIFT] = NILFS_FT_FIFO, |
| 264 | [S_IFSOCK >> S_SHIFT] = NILFS_FT_SOCK, |
| 265 | [S_IFLNK >> S_SHIFT] = NILFS_FT_SYMLINK, |
| 266 | }; |
| 267 | |
| 268 | static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode) |
| 269 | { |
| 270 | mode_t mode = inode->i_mode; |
| 271 | |
| 272 | de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; |
| 273 | } |
| 274 | |
| 275 | static int nilfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 276 | { |
| 277 | loff_t pos = filp->f_pos; |
| 278 | struct inode *inode = filp->f_dentry->d_inode; |
| 279 | struct super_block *sb = inode->i_sb; |
| 280 | unsigned int offset = pos & ~PAGE_CACHE_MASK; |
| 281 | unsigned long n = pos >> PAGE_CACHE_SHIFT; |
| 282 | unsigned long npages = dir_pages(inode); |
| 283 | /* unsigned chunk_mask = ~(nilfs_chunk_size(inode)-1); */ |
| 284 | unsigned char *types = NULL; |
| 285 | int ret; |
| 286 | |
| 287 | if (pos > inode->i_size - NILFS_DIR_REC_LEN(1)) |
| 288 | goto success; |
| 289 | |
| 290 | types = nilfs_filetype_table; |
| 291 | |
| 292 | for ( ; n < npages; n++, offset = 0) { |
| 293 | char *kaddr, *limit; |
| 294 | struct nilfs_dir_entry *de; |
| 295 | struct page *page = nilfs_get_page(inode, n); |
| 296 | |
| 297 | if (IS_ERR(page)) { |
| 298 | nilfs_error(sb, __func__, "bad page in #%lu", |
| 299 | inode->i_ino); |
| 300 | filp->f_pos += PAGE_CACHE_SIZE - offset; |
| 301 | ret = -EIO; |
| 302 | goto done; |
| 303 | } |
| 304 | kaddr = page_address(page); |
| 305 | de = (struct nilfs_dir_entry *)(kaddr + offset); |
| 306 | limit = kaddr + nilfs_last_byte(inode, n) - |
| 307 | NILFS_DIR_REC_LEN(1); |
| 308 | for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) { |
| 309 | if (de->rec_len == 0) { |
| 310 | nilfs_error(sb, __func__, |
| 311 | "zero-length directory entry"); |
| 312 | ret = -EIO; |
| 313 | nilfs_put_page(page); |
| 314 | goto done; |
| 315 | } |
| 316 | if (de->inode) { |
| 317 | int over; |
| 318 | unsigned char d_type = DT_UNKNOWN; |
| 319 | |
| 320 | if (types && de->file_type < NILFS_FT_MAX) |
| 321 | d_type = types[de->file_type]; |
| 322 | |
| 323 | offset = (char *)de - kaddr; |
| 324 | over = filldir(dirent, de->name, de->name_len, |
| 325 | (n<<PAGE_CACHE_SHIFT) | offset, |
| 326 | le64_to_cpu(de->inode), d_type); |
| 327 | if (over) { |
| 328 | nilfs_put_page(page); |
| 329 | goto success; |
| 330 | } |
| 331 | } |
| 332 | filp->f_pos += le16_to_cpu(de->rec_len); |
| 333 | } |
| 334 | nilfs_put_page(page); |
| 335 | } |
| 336 | |
| 337 | success: |
| 338 | ret = 0; |
| 339 | done: |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * nilfs_find_entry() |
| 345 | * |
| 346 | * finds an entry in the specified directory with the wanted name. It |
| 347 | * returns the page in which the entry was found, and the entry itself |
| 348 | * (as a parameter - res_dir). Page is returned mapped and unlocked. |
| 349 | * Entry is guaranteed to be valid. |
| 350 | */ |
| 351 | struct nilfs_dir_entry * |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 352 | nilfs_find_entry(struct inode *dir, const struct qstr *qstr, |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 353 | struct page **res_page) |
| 354 | { |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 355 | const unsigned char *name = qstr->name; |
| 356 | int namelen = qstr->len; |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 357 | unsigned reclen = NILFS_DIR_REC_LEN(namelen); |
| 358 | unsigned long start, n; |
| 359 | unsigned long npages = dir_pages(dir); |
| 360 | struct page *page = NULL; |
| 361 | struct nilfs_inode_info *ei = NILFS_I(dir); |
| 362 | struct nilfs_dir_entry *de; |
| 363 | |
| 364 | if (npages == 0) |
| 365 | goto out; |
| 366 | |
| 367 | /* OFFSET_CACHE */ |
| 368 | *res_page = NULL; |
| 369 | |
| 370 | start = ei->i_dir_start_lookup; |
| 371 | if (start >= npages) |
| 372 | start = 0; |
| 373 | n = start; |
| 374 | do { |
| 375 | char *kaddr; |
| 376 | page = nilfs_get_page(dir, n); |
| 377 | if (!IS_ERR(page)) { |
| 378 | kaddr = page_address(page); |
| 379 | de = (struct nilfs_dir_entry *)kaddr; |
| 380 | kaddr += nilfs_last_byte(dir, n) - reclen; |
| 381 | while ((char *) de <= kaddr) { |
| 382 | if (de->rec_len == 0) { |
| 383 | nilfs_error(dir->i_sb, __func__, |
| 384 | "zero-length directory entry"); |
| 385 | nilfs_put_page(page); |
| 386 | goto out; |
| 387 | } |
| 388 | if (nilfs_match(namelen, name, de)) |
| 389 | goto found; |
| 390 | de = nilfs_next_entry(de); |
| 391 | } |
| 392 | nilfs_put_page(page); |
| 393 | } |
| 394 | if (++n >= npages) |
| 395 | n = 0; |
| 396 | /* next page is past the blocks we've got */ |
| 397 | if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) { |
| 398 | nilfs_error(dir->i_sb, __func__, |
Ryusuke Konishi | 1621562 | 2010-03-14 03:17:45 +0900 | [diff] [blame] | 399 | "dir %lu size %lld exceeds block count %llu", |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 400 | dir->i_ino, dir->i_size, |
| 401 | (unsigned long long)dir->i_blocks); |
| 402 | goto out; |
| 403 | } |
| 404 | } while (n != start); |
| 405 | out: |
| 406 | return NULL; |
| 407 | |
| 408 | found: |
| 409 | *res_page = page; |
| 410 | ei->i_dir_start_lookup = n; |
| 411 | return de; |
| 412 | } |
| 413 | |
| 414 | struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p) |
| 415 | { |
| 416 | struct page *page = nilfs_get_page(dir, 0); |
| 417 | struct nilfs_dir_entry *de = NULL; |
| 418 | |
| 419 | if (!IS_ERR(page)) { |
| 420 | de = nilfs_next_entry( |
| 421 | (struct nilfs_dir_entry *)page_address(page)); |
| 422 | *p = page; |
| 423 | } |
| 424 | return de; |
| 425 | } |
| 426 | |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 427 | ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr) |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 428 | { |
| 429 | ino_t res = 0; |
| 430 | struct nilfs_dir_entry *de; |
| 431 | struct page *page; |
| 432 | |
Al Viro | 0319003 | 2010-01-31 21:02:09 -0500 | [diff] [blame] | 433 | de = nilfs_find_entry(dir, qstr, &page); |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 434 | if (de) { |
| 435 | res = le64_to_cpu(de->inode); |
| 436 | kunmap(page); |
| 437 | page_cache_release(page); |
| 438 | } |
| 439 | return res; |
| 440 | } |
| 441 | |
| 442 | /* Releases the page */ |
| 443 | void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de, |
| 444 | struct page *page, struct inode *inode) |
| 445 | { |
| 446 | unsigned from = (char *) de - (char *) page_address(page); |
| 447 | unsigned to = from + le16_to_cpu(de->rec_len); |
| 448 | struct address_space *mapping = page->mapping; |
| 449 | int err; |
| 450 | |
| 451 | lock_page(page); |
| 452 | err = nilfs_prepare_chunk_uninterruptible(page, mapping, from, to); |
| 453 | BUG_ON(err); |
| 454 | de->inode = cpu_to_le64(inode->i_ino); |
| 455 | nilfs_set_de_type(de, inode); |
Jiro SEKIBA | 2093abf | 2009-11-27 19:41:11 +0900 | [diff] [blame] | 456 | nilfs_commit_chunk(page, mapping, from, to); |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 457 | nilfs_put_page(page); |
| 458 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 459 | /* NILFS_I(dir)->i_flags &= ~NILFS_BTREE_FL; */ |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Parent is locked. |
| 464 | */ |
| 465 | int nilfs_add_link(struct dentry *dentry, struct inode *inode) |
| 466 | { |
| 467 | struct inode *dir = dentry->d_parent->d_inode; |
Al Viro | 072f98b | 2010-01-31 21:03:58 -0500 | [diff] [blame] | 468 | const unsigned char *name = dentry->d_name.name; |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 469 | int namelen = dentry->d_name.len; |
| 470 | unsigned chunk_size = nilfs_chunk_size(dir); |
| 471 | unsigned reclen = NILFS_DIR_REC_LEN(namelen); |
| 472 | unsigned short rec_len, name_len; |
| 473 | struct page *page = NULL; |
| 474 | struct nilfs_dir_entry *de; |
| 475 | unsigned long npages = dir_pages(dir); |
| 476 | unsigned long n; |
| 477 | char *kaddr; |
| 478 | unsigned from, to; |
| 479 | int err; |
| 480 | |
| 481 | /* |
| 482 | * We take care of directory expansion in the same loop. |
| 483 | * This code plays outside i_size, so it locks the page |
| 484 | * to protect that region. |
| 485 | */ |
| 486 | for (n = 0; n <= npages; n++) { |
| 487 | char *dir_end; |
| 488 | |
| 489 | page = nilfs_get_page(dir, n); |
| 490 | err = PTR_ERR(page); |
| 491 | if (IS_ERR(page)) |
| 492 | goto out; |
| 493 | lock_page(page); |
| 494 | kaddr = page_address(page); |
| 495 | dir_end = kaddr + nilfs_last_byte(dir, n); |
| 496 | de = (struct nilfs_dir_entry *)kaddr; |
| 497 | kaddr += PAGE_CACHE_SIZE - reclen; |
| 498 | while ((char *)de <= kaddr) { |
| 499 | if ((char *)de == dir_end) { |
| 500 | /* We hit i_size */ |
| 501 | name_len = 0; |
| 502 | rec_len = chunk_size; |
| 503 | de->rec_len = cpu_to_le16(chunk_size); |
| 504 | de->inode = 0; |
| 505 | goto got_it; |
| 506 | } |
| 507 | if (de->rec_len == 0) { |
| 508 | nilfs_error(dir->i_sb, __func__, |
| 509 | "zero-length directory entry"); |
| 510 | err = -EIO; |
| 511 | goto out_unlock; |
| 512 | } |
| 513 | err = -EEXIST; |
| 514 | if (nilfs_match(namelen, name, de)) |
| 515 | goto out_unlock; |
| 516 | name_len = NILFS_DIR_REC_LEN(de->name_len); |
| 517 | rec_len = le16_to_cpu(de->rec_len); |
| 518 | if (!de->inode && rec_len >= reclen) |
| 519 | goto got_it; |
| 520 | if (rec_len >= name_len + reclen) |
| 521 | goto got_it; |
| 522 | de = (struct nilfs_dir_entry *)((char *)de + rec_len); |
| 523 | } |
| 524 | unlock_page(page); |
| 525 | nilfs_put_page(page); |
| 526 | } |
| 527 | BUG(); |
| 528 | return -EINVAL; |
| 529 | |
| 530 | got_it: |
| 531 | from = (char *)de - (char *)page_address(page); |
| 532 | to = from + rec_len; |
| 533 | err = nilfs_prepare_chunk(page, page->mapping, from, to); |
| 534 | if (err) |
| 535 | goto out_unlock; |
| 536 | if (de->inode) { |
| 537 | struct nilfs_dir_entry *de1; |
| 538 | |
| 539 | de1 = (struct nilfs_dir_entry *)((char *)de + name_len); |
| 540 | de1->rec_len = cpu_to_le16(rec_len - name_len); |
| 541 | de->rec_len = cpu_to_le16(name_len); |
| 542 | de = de1; |
| 543 | } |
| 544 | de->name_len = namelen; |
| 545 | memcpy(de->name, name, namelen); |
| 546 | de->inode = cpu_to_le64(inode->i_ino); |
| 547 | nilfs_set_de_type(de, inode); |
Jiro SEKIBA | 2093abf | 2009-11-27 19:41:11 +0900 | [diff] [blame] | 548 | nilfs_commit_chunk(page, page->mapping, from, to); |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 549 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 550 | /* NILFS_I(dir)->i_flags &= ~NILFS_BTREE_FL; */ |
Jiro SEKIBA | abdb318 | 2009-11-27 19:41:14 +0900 | [diff] [blame] | 551 | nilfs_mark_inode_dirty(dir); |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 552 | /* OFFSET_CACHE */ |
| 553 | out_put: |
| 554 | nilfs_put_page(page); |
| 555 | out: |
| 556 | return err; |
| 557 | out_unlock: |
| 558 | unlock_page(page); |
| 559 | goto out_put; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * nilfs_delete_entry deletes a directory entry by merging it with the |
| 564 | * previous entry. Page is up-to-date. Releases the page. |
| 565 | */ |
| 566 | int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page) |
| 567 | { |
| 568 | struct address_space *mapping = page->mapping; |
| 569 | struct inode *inode = mapping->host; |
| 570 | char *kaddr = page_address(page); |
| 571 | unsigned from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1); |
| 572 | unsigned to = ((char *)dir - kaddr) + le16_to_cpu(dir->rec_len); |
| 573 | struct nilfs_dir_entry *pde = NULL; |
| 574 | struct nilfs_dir_entry *de = (struct nilfs_dir_entry *)(kaddr + from); |
| 575 | int err; |
| 576 | |
| 577 | while ((char *)de < (char *)dir) { |
| 578 | if (de->rec_len == 0) { |
| 579 | nilfs_error(inode->i_sb, __func__, |
| 580 | "zero-length directory entry"); |
| 581 | err = -EIO; |
| 582 | goto out; |
| 583 | } |
| 584 | pde = de; |
| 585 | de = nilfs_next_entry(de); |
| 586 | } |
| 587 | if (pde) |
| 588 | from = (char *)pde - (char *)page_address(page); |
| 589 | lock_page(page); |
| 590 | err = nilfs_prepare_chunk(page, mapping, from, to); |
| 591 | BUG_ON(err); |
| 592 | if (pde) |
| 593 | pde->rec_len = cpu_to_le16(to - from); |
| 594 | dir->inode = 0; |
Jiro SEKIBA | 2093abf | 2009-11-27 19:41:11 +0900 | [diff] [blame] | 595 | nilfs_commit_chunk(page, mapping, from, to); |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 596 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 597 | /* NILFS_I(inode)->i_flags &= ~NILFS_BTREE_FL; */ |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 598 | out: |
| 599 | nilfs_put_page(page); |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Set the first fragment of directory. |
| 605 | */ |
| 606 | int nilfs_make_empty(struct inode *inode, struct inode *parent) |
| 607 | { |
| 608 | struct address_space *mapping = inode->i_mapping; |
| 609 | struct page *page = grab_cache_page(mapping, 0); |
| 610 | unsigned chunk_size = nilfs_chunk_size(inode); |
| 611 | struct nilfs_dir_entry *de; |
| 612 | int err; |
| 613 | void *kaddr; |
| 614 | |
| 615 | if (!page) |
| 616 | return -ENOMEM; |
| 617 | |
| 618 | err = nilfs_prepare_chunk(page, mapping, 0, chunk_size); |
| 619 | if (unlikely(err)) { |
| 620 | unlock_page(page); |
| 621 | goto fail; |
| 622 | } |
| 623 | kaddr = kmap_atomic(page, KM_USER0); |
| 624 | memset(kaddr, 0, chunk_size); |
| 625 | de = (struct nilfs_dir_entry *)kaddr; |
| 626 | de->name_len = 1; |
| 627 | de->rec_len = cpu_to_le16(NILFS_DIR_REC_LEN(1)); |
| 628 | memcpy(de->name, ".\0\0", 4); |
| 629 | de->inode = cpu_to_le64(inode->i_ino); |
| 630 | nilfs_set_de_type(de, inode); |
| 631 | |
| 632 | de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1)); |
| 633 | de->name_len = 2; |
| 634 | de->rec_len = cpu_to_le16(chunk_size - NILFS_DIR_REC_LEN(1)); |
| 635 | de->inode = cpu_to_le64(parent->i_ino); |
| 636 | memcpy(de->name, "..\0", 4); |
| 637 | nilfs_set_de_type(de, inode); |
| 638 | kunmap_atomic(kaddr, KM_USER0); |
Jiro SEKIBA | 2093abf | 2009-11-27 19:41:11 +0900 | [diff] [blame] | 639 | nilfs_commit_chunk(page, mapping, 0, chunk_size); |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 640 | fail: |
| 641 | page_cache_release(page); |
| 642 | return err; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * routine to check that the specified directory is empty (for rmdir) |
| 647 | */ |
| 648 | int nilfs_empty_dir(struct inode *inode) |
| 649 | { |
| 650 | struct page *page = NULL; |
| 651 | unsigned long i, npages = dir_pages(inode); |
| 652 | |
| 653 | for (i = 0; i < npages; i++) { |
| 654 | char *kaddr; |
| 655 | struct nilfs_dir_entry *de; |
| 656 | |
| 657 | page = nilfs_get_page(inode, i); |
| 658 | if (IS_ERR(page)) |
| 659 | continue; |
| 660 | |
| 661 | kaddr = page_address(page); |
| 662 | de = (struct nilfs_dir_entry *)kaddr; |
| 663 | kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1); |
| 664 | |
| 665 | while ((char *)de <= kaddr) { |
| 666 | if (de->rec_len == 0) { |
| 667 | nilfs_error(inode->i_sb, __func__, |
| 668 | "zero-length directory entry " |
| 669 | "(kaddr=%p, de=%p)\n", kaddr, de); |
| 670 | goto not_empty; |
| 671 | } |
| 672 | if (de->inode != 0) { |
| 673 | /* check for . and .. */ |
| 674 | if (de->name[0] != '.') |
| 675 | goto not_empty; |
| 676 | if (de->name_len > 2) |
| 677 | goto not_empty; |
| 678 | if (de->name_len < 2) { |
| 679 | if (de->inode != |
| 680 | cpu_to_le64(inode->i_ino)) |
| 681 | goto not_empty; |
| 682 | } else if (de->name[1] != '.') |
| 683 | goto not_empty; |
| 684 | } |
| 685 | de = nilfs_next_entry(de); |
| 686 | } |
| 687 | nilfs_put_page(page); |
| 688 | } |
| 689 | return 1; |
| 690 | |
| 691 | not_empty: |
| 692 | nilfs_put_page(page); |
| 693 | return 0; |
| 694 | } |
| 695 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 696 | const struct file_operations nilfs_dir_operations = { |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 697 | .llseek = generic_file_llseek, |
| 698 | .read = generic_read_dir, |
| 699 | .readdir = nilfs_readdir, |
Ryusuke Konishi | 7a94619 | 2009-04-06 19:01:53 -0700 | [diff] [blame] | 700 | .unlocked_ioctl = nilfs_ioctl, |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 701 | #ifdef CONFIG_COMPAT |
Ryusuke Konishi | 8082d36 | 2009-04-06 19:01:53 -0700 | [diff] [blame] | 702 | .compat_ioctl = nilfs_ioctl, |
Yoshiji Amagai | 2ba466d | 2009-04-06 19:01:34 -0700 | [diff] [blame] | 703 | #endif /* CONFIG_COMPAT */ |
| 704 | .fsync = nilfs_sync_file, |
| 705 | |
| 706 | }; |