p54: disable PS by default
[linux-2.6/mini2440.git] / fs / nilfs2 / dir.c
blob1a4fa04cf071bd8a82ae9f21d079612e963b419b
1 /*
2 * dir.c - NILFS directory entry operations
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net>
23 * linux/fs/ext2/dir.c
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)
30 * from
32 * linux/fs/minix/dir.c
34 * Copyright (C) 1991, 1992 Linus Torvalds
36 * ext2 directory handling functions
38 * Big-endian to little-endian byte-swapping/bitmaps by
39 * David S. Miller (davem@caip.rutgers.edu), 1995
41 * All code that works with directory layout had been switched to pagecache
42 * and moved here. AV
45 #include <linux/pagemap.h>
46 #include "nilfs.h"
47 #include "page.h"
50 * nilfs uses block-sized chunks. Arguably, sector-sized ones would be
51 * more robust, but we have what we have
53 static inline unsigned nilfs_chunk_size(struct inode *inode)
55 return inode->i_sb->s_blocksize;
58 static inline void nilfs_put_page(struct page *page)
60 kunmap(page);
61 page_cache_release(page);
64 static inline unsigned long dir_pages(struct inode *inode)
66 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
70 * Return the offset into page `page_nr' of the last valid
71 * byte in that page, plus one.
73 static unsigned nilfs_last_byte(struct inode *inode, unsigned long page_nr)
75 unsigned last_byte = inode->i_size;
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;
83 static int nilfs_prepare_chunk_uninterruptible(struct page *page,
84 struct address_space *mapping,
85 unsigned from, unsigned to)
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);
93 static int nilfs_prepare_chunk(struct page *page,
94 struct address_space *mapping,
95 unsigned from, unsigned to)
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);
102 static int nilfs_commit_chunk(struct page *page,
103 struct address_space *mapping,
104 unsigned from, unsigned to)
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;
113 nr_dirty = nilfs_page_count_clean_buffers(page, from, to);
114 copied = block_write_end(NULL, mapping, pos, len, len, page, NULL);
115 if (pos + copied > dir->i_size) {
116 i_size_write(dir, pos + copied);
117 mark_inode_dirty(dir);
119 if (IS_DIRSYNC(dir))
120 nilfs_set_transaction_flag(NILFS_TI_SYNC);
121 err = nilfs_set_file_dirty(sbi, dir, nr_dirty);
122 unlock_page(page);
123 return err;
126 static void nilfs_check_page(struct page *page)
128 struct inode *dir = page->mapping->host;
129 struct super_block *sb = dir->i_sb;
130 unsigned chunk_size = nilfs_chunk_size(dir);
131 char *kaddr = page_address(page);
132 unsigned offs, rec_len;
133 unsigned limit = PAGE_CACHE_SIZE;
134 struct nilfs_dir_entry *p;
135 char *error;
137 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
138 limit = dir->i_size & ~PAGE_CACHE_MASK;
139 if (limit & (chunk_size - 1))
140 goto Ebadsize;
141 if (!limit)
142 goto out;
144 for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) {
145 p = (struct nilfs_dir_entry *)(kaddr + offs);
146 rec_len = le16_to_cpu(p->rec_len);
148 if (rec_len < NILFS_DIR_REC_LEN(1))
149 goto Eshort;
150 if (rec_len & 3)
151 goto Ealign;
152 if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
153 goto Enamelen;
154 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
155 goto Espan;
157 if (offs != limit)
158 goto Eend;
159 out:
160 SetPageChecked(page);
161 return;
163 /* Too bad, we had an error */
165 Ebadsize:
166 nilfs_error(sb, "nilfs_check_page",
167 "size of directory #%lu is not a multiple of chunk size",
168 dir->i_ino
170 goto fail;
171 Eshort:
172 error = "rec_len is smaller than minimal";
173 goto bad_entry;
174 Ealign:
175 error = "unaligned directory entry";
176 goto bad_entry;
177 Enamelen:
178 error = "rec_len is too small for name_len";
179 goto bad_entry;
180 Espan:
181 error = "directory entry across blocks";
182 bad_entry:
183 nilfs_error(sb, "nilfs_check_page", "bad entry in directory #%lu: %s - "
184 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
185 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
186 (unsigned long) le64_to_cpu(p->inode),
187 rec_len, p->name_len);
188 goto fail;
189 Eend:
190 p = (struct nilfs_dir_entry *)(kaddr + offs);
191 nilfs_error(sb, "nilfs_check_page",
192 "entry in directory #%lu spans the page boundary"
193 "offset=%lu, inode=%lu",
194 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
195 (unsigned long) le64_to_cpu(p->inode));
196 fail:
197 SetPageChecked(page);
198 SetPageError(page);
201 static struct page *nilfs_get_page(struct inode *dir, unsigned long n)
203 struct address_space *mapping = dir->i_mapping;
204 struct page *page = read_cache_page(mapping, n,
205 (filler_t *)mapping->a_ops->readpage, NULL);
206 if (!IS_ERR(page)) {
207 wait_on_page_locked(page);
208 kmap(page);
209 if (!PageUptodate(page))
210 goto fail;
211 if (!PageChecked(page))
212 nilfs_check_page(page);
213 if (PageError(page))
214 goto fail;
216 return page;
218 fail:
219 nilfs_put_page(page);
220 return ERR_PTR(-EIO);
224 * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
226 * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
228 static int
229 nilfs_match(int len, const char * const name, struct nilfs_dir_entry *de)
231 if (len != de->name_len)
232 return 0;
233 if (!de->inode)
234 return 0;
235 return !memcmp(name, de->name, len);
239 * p is at least 6 bytes before the end of page
241 static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
243 return (struct nilfs_dir_entry *)((char *)p + le16_to_cpu(p->rec_len));
246 static unsigned char
247 nilfs_filetype_table[NILFS_FT_MAX] = {
248 [NILFS_FT_UNKNOWN] = DT_UNKNOWN,
249 [NILFS_FT_REG_FILE] = DT_REG,
250 [NILFS_FT_DIR] = DT_DIR,
251 [NILFS_FT_CHRDEV] = DT_CHR,
252 [NILFS_FT_BLKDEV] = DT_BLK,
253 [NILFS_FT_FIFO] = DT_FIFO,
254 [NILFS_FT_SOCK] = DT_SOCK,
255 [NILFS_FT_SYMLINK] = DT_LNK,
258 #define S_SHIFT 12
259 static unsigned char
260 nilfs_type_by_mode[S_IFMT >> S_SHIFT] = {
261 [S_IFREG >> S_SHIFT] = NILFS_FT_REG_FILE,
262 [S_IFDIR >> S_SHIFT] = NILFS_FT_DIR,
263 [S_IFCHR >> S_SHIFT] = NILFS_FT_CHRDEV,
264 [S_IFBLK >> S_SHIFT] = NILFS_FT_BLKDEV,
265 [S_IFIFO >> S_SHIFT] = NILFS_FT_FIFO,
266 [S_IFSOCK >> S_SHIFT] = NILFS_FT_SOCK,
267 [S_IFLNK >> S_SHIFT] = NILFS_FT_SYMLINK,
270 static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
272 mode_t mode = inode->i_mode;
274 de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
277 static int nilfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
279 loff_t pos = filp->f_pos;
280 struct inode *inode = filp->f_dentry->d_inode;
281 struct super_block *sb = inode->i_sb;
282 unsigned int offset = pos & ~PAGE_CACHE_MASK;
283 unsigned long n = pos >> PAGE_CACHE_SHIFT;
284 unsigned long npages = dir_pages(inode);
285 /* unsigned chunk_mask = ~(nilfs_chunk_size(inode)-1); */
286 unsigned char *types = NULL;
287 int ret;
289 if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
290 goto success;
292 types = nilfs_filetype_table;
294 for ( ; n < npages; n++, offset = 0) {
295 char *kaddr, *limit;
296 struct nilfs_dir_entry *de;
297 struct page *page = nilfs_get_page(inode, n);
299 if (IS_ERR(page)) {
300 nilfs_error(sb, __func__, "bad page in #%lu",
301 inode->i_ino);
302 filp->f_pos += PAGE_CACHE_SIZE - offset;
303 ret = -EIO;
304 goto done;
306 kaddr = page_address(page);
307 de = (struct nilfs_dir_entry *)(kaddr + offset);
308 limit = kaddr + nilfs_last_byte(inode, n) -
309 NILFS_DIR_REC_LEN(1);
310 for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
311 if (de->rec_len == 0) {
312 nilfs_error(sb, __func__,
313 "zero-length directory entry");
314 ret = -EIO;
315 nilfs_put_page(page);
316 goto done;
318 if (de->inode) {
319 int over;
320 unsigned char d_type = DT_UNKNOWN;
322 if (types && de->file_type < NILFS_FT_MAX)
323 d_type = types[de->file_type];
325 offset = (char *)de - kaddr;
326 over = filldir(dirent, de->name, de->name_len,
327 (n<<PAGE_CACHE_SHIFT) | offset,
328 le64_to_cpu(de->inode), d_type);
329 if (over) {
330 nilfs_put_page(page);
331 goto success;
334 filp->f_pos += le16_to_cpu(de->rec_len);
336 nilfs_put_page(page);
339 success:
340 ret = 0;
341 done:
342 return ret;
346 * nilfs_find_entry()
348 * finds an entry in the specified directory with the wanted name. It
349 * returns the page in which the entry was found, and the entry itself
350 * (as a parameter - res_dir). Page is returned mapped and unlocked.
351 * Entry is guaranteed to be valid.
353 struct nilfs_dir_entry *
354 nilfs_find_entry(struct inode *dir, struct dentry *dentry,
355 struct page **res_page)
357 const char *name = dentry->d_name.name;
358 int namelen = dentry->d_name.len;
359 unsigned reclen = NILFS_DIR_REC_LEN(namelen);
360 unsigned long start, n;
361 unsigned long npages = dir_pages(dir);
362 struct page *page = NULL;
363 struct nilfs_inode_info *ei = NILFS_I(dir);
364 struct nilfs_dir_entry *de;
366 if (npages == 0)
367 goto out;
369 /* OFFSET_CACHE */
370 *res_page = NULL;
372 start = ei->i_dir_start_lookup;
373 if (start >= npages)
374 start = 0;
375 n = start;
376 do {
377 char *kaddr;
378 page = nilfs_get_page(dir, n);
379 if (!IS_ERR(page)) {
380 kaddr = page_address(page);
381 de = (struct nilfs_dir_entry *)kaddr;
382 kaddr += nilfs_last_byte(dir, n) - reclen;
383 while ((char *) de <= kaddr) {
384 if (de->rec_len == 0) {
385 nilfs_error(dir->i_sb, __func__,
386 "zero-length directory entry");
387 nilfs_put_page(page);
388 goto out;
390 if (nilfs_match(namelen, name, de))
391 goto found;
392 de = nilfs_next_entry(de);
394 nilfs_put_page(page);
396 if (++n >= npages)
397 n = 0;
398 /* next page is past the blocks we've got */
399 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
400 nilfs_error(dir->i_sb, __func__,
401 "dir %lu size %lld exceeds block cout %llu",
402 dir->i_ino, dir->i_size,
403 (unsigned long long)dir->i_blocks);
404 goto out;
406 } while (n != start);
407 out:
408 return NULL;
410 found:
411 *res_page = page;
412 ei->i_dir_start_lookup = n;
413 return de;
416 struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p)
418 struct page *page = nilfs_get_page(dir, 0);
419 struct nilfs_dir_entry *de = NULL;
421 if (!IS_ERR(page)) {
422 de = nilfs_next_entry(
423 (struct nilfs_dir_entry *)page_address(page));
424 *p = page;
426 return de;
429 ino_t nilfs_inode_by_name(struct inode *dir, struct dentry *dentry)
431 ino_t res = 0;
432 struct nilfs_dir_entry *de;
433 struct page *page;
435 de = nilfs_find_entry(dir, dentry, &page);
436 if (de) {
437 res = le64_to_cpu(de->inode);
438 kunmap(page);
439 page_cache_release(page);
441 return res;
444 /* Releases the page */
445 void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
446 struct page *page, struct inode *inode)
448 unsigned from = (char *) de - (char *) page_address(page);
449 unsigned to = from + le16_to_cpu(de->rec_len);
450 struct address_space *mapping = page->mapping;
451 int err;
453 lock_page(page);
454 err = nilfs_prepare_chunk_uninterruptible(page, mapping, from, to);
455 BUG_ON(err);
456 de->inode = cpu_to_le64(inode->i_ino);
457 nilfs_set_de_type(de, inode);
458 err = nilfs_commit_chunk(page, mapping, from, to);
459 nilfs_put_page(page);
460 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
461 /* NILFS_I(dir)->i_flags &= ~NILFS_BTREE_FL; */
462 mark_inode_dirty(dir);
466 * Parent is locked.
468 int nilfs_add_link(struct dentry *dentry, struct inode *inode)
470 struct inode *dir = dentry->d_parent->d_inode;
471 const char *name = dentry->d_name.name;
472 int namelen = dentry->d_name.len;
473 unsigned chunk_size = nilfs_chunk_size(dir);
474 unsigned reclen = NILFS_DIR_REC_LEN(namelen);
475 unsigned short rec_len, name_len;
476 struct page *page = NULL;
477 struct nilfs_dir_entry *de;
478 unsigned long npages = dir_pages(dir);
479 unsigned long n;
480 char *kaddr;
481 unsigned from, to;
482 int err;
485 * We take care of directory expansion in the same loop.
486 * This code plays outside i_size, so it locks the page
487 * to protect that region.
489 for (n = 0; n <= npages; n++) {
490 char *dir_end;
492 page = nilfs_get_page(dir, n);
493 err = PTR_ERR(page);
494 if (IS_ERR(page))
495 goto out;
496 lock_page(page);
497 kaddr = page_address(page);
498 dir_end = kaddr + nilfs_last_byte(dir, n);
499 de = (struct nilfs_dir_entry *)kaddr;
500 kaddr += PAGE_CACHE_SIZE - reclen;
501 while ((char *)de <= kaddr) {
502 if ((char *)de == dir_end) {
503 /* We hit i_size */
504 name_len = 0;
505 rec_len = chunk_size;
506 de->rec_len = cpu_to_le16(chunk_size);
507 de->inode = 0;
508 goto got_it;
510 if (de->rec_len == 0) {
511 nilfs_error(dir->i_sb, __func__,
512 "zero-length directory entry");
513 err = -EIO;
514 goto out_unlock;
516 err = -EEXIST;
517 if (nilfs_match(namelen, name, de))
518 goto out_unlock;
519 name_len = NILFS_DIR_REC_LEN(de->name_len);
520 rec_len = le16_to_cpu(de->rec_len);
521 if (!de->inode && rec_len >= reclen)
522 goto got_it;
523 if (rec_len >= name_len + reclen)
524 goto got_it;
525 de = (struct nilfs_dir_entry *)((char *)de + rec_len);
527 unlock_page(page);
528 nilfs_put_page(page);
530 BUG();
531 return -EINVAL;
533 got_it:
534 from = (char *)de - (char *)page_address(page);
535 to = from + rec_len;
536 err = nilfs_prepare_chunk(page, page->mapping, from, to);
537 if (err)
538 goto out_unlock;
539 if (de->inode) {
540 struct nilfs_dir_entry *de1;
542 de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
543 de1->rec_len = cpu_to_le16(rec_len - name_len);
544 de->rec_len = cpu_to_le16(name_len);
545 de = de1;
547 de->name_len = namelen;
548 memcpy(de->name, name, namelen);
549 de->inode = cpu_to_le64(inode->i_ino);
550 nilfs_set_de_type(de, inode);
551 err = nilfs_commit_chunk(page, page->mapping, from, to);
552 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
553 /* NILFS_I(dir)->i_flags &= ~NILFS_BTREE_FL; */
554 mark_inode_dirty(dir);
555 /* OFFSET_CACHE */
556 out_put:
557 nilfs_put_page(page);
558 out:
559 return err;
560 out_unlock:
561 unlock_page(page);
562 goto out_put;
566 * nilfs_delete_entry deletes a directory entry by merging it with the
567 * previous entry. Page is up-to-date. Releases the page.
569 int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page)
571 struct address_space *mapping = page->mapping;
572 struct inode *inode = mapping->host;
573 char *kaddr = page_address(page);
574 unsigned from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
575 unsigned to = ((char *)dir - kaddr) + le16_to_cpu(dir->rec_len);
576 struct nilfs_dir_entry *pde = NULL;
577 struct nilfs_dir_entry *de = (struct nilfs_dir_entry *)(kaddr + from);
578 int err;
580 while ((char *)de < (char *)dir) {
581 if (de->rec_len == 0) {
582 nilfs_error(inode->i_sb, __func__,
583 "zero-length directory entry");
584 err = -EIO;
585 goto out;
587 pde = de;
588 de = nilfs_next_entry(de);
590 if (pde)
591 from = (char *)pde - (char *)page_address(page);
592 lock_page(page);
593 err = nilfs_prepare_chunk(page, mapping, from, to);
594 BUG_ON(err);
595 if (pde)
596 pde->rec_len = cpu_to_le16(to - from);
597 dir->inode = 0;
598 err = nilfs_commit_chunk(page, mapping, from, to);
599 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
600 /* NILFS_I(inode)->i_flags &= ~NILFS_BTREE_FL; */
601 mark_inode_dirty(inode);
602 out:
603 nilfs_put_page(page);
604 return err;
608 * Set the first fragment of directory.
610 int nilfs_make_empty(struct inode *inode, struct inode *parent)
612 struct address_space *mapping = inode->i_mapping;
613 struct page *page = grab_cache_page(mapping, 0);
614 unsigned chunk_size = nilfs_chunk_size(inode);
615 struct nilfs_dir_entry *de;
616 int err;
617 void *kaddr;
619 if (!page)
620 return -ENOMEM;
622 err = nilfs_prepare_chunk(page, mapping, 0, chunk_size);
623 if (unlikely(err)) {
624 unlock_page(page);
625 goto fail;
627 kaddr = kmap_atomic(page, KM_USER0);
628 memset(kaddr, 0, chunk_size);
629 de = (struct nilfs_dir_entry *)kaddr;
630 de->name_len = 1;
631 de->rec_len = cpu_to_le16(NILFS_DIR_REC_LEN(1));
632 memcpy(de->name, ".\0\0", 4);
633 de->inode = cpu_to_le64(inode->i_ino);
634 nilfs_set_de_type(de, inode);
636 de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
637 de->name_len = 2;
638 de->rec_len = cpu_to_le16(chunk_size - NILFS_DIR_REC_LEN(1));
639 de->inode = cpu_to_le64(parent->i_ino);
640 memcpy(de->name, "..\0", 4);
641 nilfs_set_de_type(de, inode);
642 kunmap_atomic(kaddr, KM_USER0);
643 err = nilfs_commit_chunk(page, mapping, 0, chunk_size);
644 fail:
645 page_cache_release(page);
646 return err;
650 * routine to check that the specified directory is empty (for rmdir)
652 int nilfs_empty_dir(struct inode *inode)
654 struct page *page = NULL;
655 unsigned long i, npages = dir_pages(inode);
657 for (i = 0; i < npages; i++) {
658 char *kaddr;
659 struct nilfs_dir_entry *de;
661 page = nilfs_get_page(inode, i);
662 if (IS_ERR(page))
663 continue;
665 kaddr = page_address(page);
666 de = (struct nilfs_dir_entry *)kaddr;
667 kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
669 while ((char *)de <= kaddr) {
670 if (de->rec_len == 0) {
671 nilfs_error(inode->i_sb, __func__,
672 "zero-length directory entry "
673 "(kaddr=%p, de=%p)\n", kaddr, de);
674 goto not_empty;
676 if (de->inode != 0) {
677 /* check for . and .. */
678 if (de->name[0] != '.')
679 goto not_empty;
680 if (de->name_len > 2)
681 goto not_empty;
682 if (de->name_len < 2) {
683 if (de->inode !=
684 cpu_to_le64(inode->i_ino))
685 goto not_empty;
686 } else if (de->name[1] != '.')
687 goto not_empty;
689 de = nilfs_next_entry(de);
691 nilfs_put_page(page);
693 return 1;
695 not_empty:
696 nilfs_put_page(page);
697 return 0;
700 struct file_operations nilfs_dir_operations = {
701 .llseek = generic_file_llseek,
702 .read = generic_read_dir,
703 .readdir = nilfs_readdir,
704 .unlocked_ioctl = nilfs_ioctl,
705 #ifdef CONFIG_COMPAT
706 .compat_ioctl = nilfs_ioctl,
707 #endif /* CONFIG_COMPAT */
708 .fsync = nilfs_sync_file,