drm/i915: NOMEM->NOSPC
[linux-2.6/mini2440.git] / fs / ext2 / dir.c
blob2999d72153b7c19d91b86fe918089263f299b2b6
1 /*
2 * linux/fs/ext2/dir.c
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
9 * from
11 * linux/fs/minix/dir.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * ext2 directory handling functions
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
20 * All code that works with directory layout had been switched to pagecache
21 * and moved here. AV
24 #include "ext2.h"
25 #include <linux/buffer_head.h>
26 #include <linux/pagemap.h>
27 #include <linux/swap.h>
29 typedef struct ext2_dir_entry_2 ext2_dirent;
31 static inline unsigned ext2_rec_len_from_disk(__le16 dlen)
33 unsigned len = le16_to_cpu(dlen);
35 if (len == EXT2_MAX_REC_LEN)
36 return 1 << 16;
37 return len;
40 static inline __le16 ext2_rec_len_to_disk(unsigned len)
42 if (len == (1 << 16))
43 return cpu_to_le16(EXT2_MAX_REC_LEN);
44 else
45 BUG_ON(len > (1 << 16));
46 return cpu_to_le16(len);
50 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
51 * more robust, but we have what we have
53 static inline unsigned ext2_chunk_size(struct inode *inode)
55 return inode->i_sb->s_blocksize;
58 static inline void ext2_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
74 ext2_last_byte(struct inode *inode, unsigned long page_nr)
76 unsigned last_byte = inode->i_size;
78 last_byte -= page_nr << PAGE_CACHE_SHIFT;
79 if (last_byte > PAGE_CACHE_SIZE)
80 last_byte = PAGE_CACHE_SIZE;
81 return last_byte;
84 static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
86 struct address_space *mapping = page->mapping;
87 struct inode *dir = mapping->host;
88 int err = 0;
90 dir->i_version++;
91 block_write_end(NULL, mapping, pos, len, len, page, NULL);
93 if (pos+len > dir->i_size) {
94 i_size_write(dir, pos+len);
95 mark_inode_dirty(dir);
98 if (IS_DIRSYNC(dir)) {
99 err = write_one_page(page, 1);
100 if (!err)
101 err = ext2_sync_inode(dir);
102 } else {
103 unlock_page(page);
106 return err;
109 static void ext2_check_page(struct page *page, int quiet)
111 struct inode *dir = page->mapping->host;
112 struct super_block *sb = dir->i_sb;
113 unsigned chunk_size = ext2_chunk_size(dir);
114 char *kaddr = page_address(page);
115 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
116 unsigned offs, rec_len;
117 unsigned limit = PAGE_CACHE_SIZE;
118 ext2_dirent *p;
119 char *error;
121 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
122 limit = dir->i_size & ~PAGE_CACHE_MASK;
123 if (limit & (chunk_size - 1))
124 goto Ebadsize;
125 if (!limit)
126 goto out;
128 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
129 p = (ext2_dirent *)(kaddr + offs);
130 rec_len = ext2_rec_len_from_disk(p->rec_len);
132 if (rec_len < EXT2_DIR_REC_LEN(1))
133 goto Eshort;
134 if (rec_len & 3)
135 goto Ealign;
136 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
137 goto Enamelen;
138 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
139 goto Espan;
140 if (le32_to_cpu(p->inode) > max_inumber)
141 goto Einumber;
143 if (offs != limit)
144 goto Eend;
145 out:
146 SetPageChecked(page);
147 return;
149 /* Too bad, we had an error */
151 Ebadsize:
152 if (!quiet)
153 ext2_error(sb, __func__,
154 "size of directory #%lu is not a multiple "
155 "of chunk size", dir->i_ino);
156 goto fail;
157 Eshort:
158 error = "rec_len is smaller than minimal";
159 goto bad_entry;
160 Ealign:
161 error = "unaligned directory entry";
162 goto bad_entry;
163 Enamelen:
164 error = "rec_len is too small for name_len";
165 goto bad_entry;
166 Espan:
167 error = "directory entry across blocks";
168 goto bad_entry;
169 Einumber:
170 error = "inode out of bounds";
171 bad_entry:
172 if (!quiet)
173 ext2_error(sb, __func__, "bad entry in directory #%lu: : %s - "
174 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
175 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
176 (unsigned long) le32_to_cpu(p->inode),
177 rec_len, p->name_len);
178 goto fail;
179 Eend:
180 if (!quiet) {
181 p = (ext2_dirent *)(kaddr + offs);
182 ext2_error(sb, "ext2_check_page",
183 "entry in directory #%lu spans the page boundary"
184 "offset=%lu, inode=%lu",
185 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
186 (unsigned long) le32_to_cpu(p->inode));
188 fail:
189 SetPageChecked(page);
190 SetPageError(page);
193 static struct page * ext2_get_page(struct inode *dir, unsigned long n,
194 int quiet)
196 struct address_space *mapping = dir->i_mapping;
197 struct page *page = read_mapping_page(mapping, n, NULL);
198 if (!IS_ERR(page)) {
199 kmap(page);
200 if (!PageChecked(page))
201 ext2_check_page(page, quiet);
202 if (PageError(page))
203 goto fail;
205 return page;
207 fail:
208 ext2_put_page(page);
209 return ERR_PTR(-EIO);
213 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
215 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
217 static inline int ext2_match (int len, const char * const name,
218 struct ext2_dir_entry_2 * de)
220 if (len != de->name_len)
221 return 0;
222 if (!de->inode)
223 return 0;
224 return !memcmp(name, de->name, len);
228 * p is at least 6 bytes before the end of page
230 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
232 return (ext2_dirent *)((char *)p +
233 ext2_rec_len_from_disk(p->rec_len));
236 static inline unsigned
237 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
239 ext2_dirent *de = (ext2_dirent*)(base + offset);
240 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
241 while ((char*)p < (char*)de) {
242 if (p->rec_len == 0)
243 break;
244 p = ext2_next_entry(p);
246 return (char *)p - base;
249 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
250 [EXT2_FT_UNKNOWN] = DT_UNKNOWN,
251 [EXT2_FT_REG_FILE] = DT_REG,
252 [EXT2_FT_DIR] = DT_DIR,
253 [EXT2_FT_CHRDEV] = DT_CHR,
254 [EXT2_FT_BLKDEV] = DT_BLK,
255 [EXT2_FT_FIFO] = DT_FIFO,
256 [EXT2_FT_SOCK] = DT_SOCK,
257 [EXT2_FT_SYMLINK] = DT_LNK,
260 #define S_SHIFT 12
261 static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
262 [S_IFREG >> S_SHIFT] = EXT2_FT_REG_FILE,
263 [S_IFDIR >> S_SHIFT] = EXT2_FT_DIR,
264 [S_IFCHR >> S_SHIFT] = EXT2_FT_CHRDEV,
265 [S_IFBLK >> S_SHIFT] = EXT2_FT_BLKDEV,
266 [S_IFIFO >> S_SHIFT] = EXT2_FT_FIFO,
267 [S_IFSOCK >> S_SHIFT] = EXT2_FT_SOCK,
268 [S_IFLNK >> S_SHIFT] = EXT2_FT_SYMLINK,
271 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
273 mode_t mode = inode->i_mode;
274 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
275 de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
276 else
277 de->file_type = 0;
280 static int
281 ext2_readdir (struct file * filp, void * dirent, filldir_t filldir)
283 loff_t pos = filp->f_pos;
284 struct inode *inode = filp->f_path.dentry->d_inode;
285 struct super_block *sb = inode->i_sb;
286 unsigned int offset = pos & ~PAGE_CACHE_MASK;
287 unsigned long n = pos >> PAGE_CACHE_SHIFT;
288 unsigned long npages = dir_pages(inode);
289 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
290 unsigned char *types = NULL;
291 int need_revalidate = filp->f_version != inode->i_version;
293 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
294 return 0;
296 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
297 types = ext2_filetype_table;
299 for ( ; n < npages; n++, offset = 0) {
300 char *kaddr, *limit;
301 ext2_dirent *de;
302 struct page *page = ext2_get_page(inode, n, 0);
304 if (IS_ERR(page)) {
305 ext2_error(sb, __func__,
306 "bad page in #%lu",
307 inode->i_ino);
308 filp->f_pos += PAGE_CACHE_SIZE - offset;
309 return PTR_ERR(page);
311 kaddr = page_address(page);
312 if (unlikely(need_revalidate)) {
313 if (offset) {
314 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
315 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
317 filp->f_version = inode->i_version;
318 need_revalidate = 0;
320 de = (ext2_dirent *)(kaddr+offset);
321 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
322 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
323 if (de->rec_len == 0) {
324 ext2_error(sb, __func__,
325 "zero-length directory entry");
326 ext2_put_page(page);
327 return -EIO;
329 if (de->inode) {
330 int over;
331 unsigned char d_type = DT_UNKNOWN;
333 if (types && de->file_type < EXT2_FT_MAX)
334 d_type = types[de->file_type];
336 offset = (char *)de - kaddr;
337 over = filldir(dirent, de->name, de->name_len,
338 (n<<PAGE_CACHE_SHIFT) | offset,
339 le32_to_cpu(de->inode), d_type);
340 if (over) {
341 ext2_put_page(page);
342 return 0;
345 filp->f_pos += ext2_rec_len_from_disk(de->rec_len);
347 ext2_put_page(page);
349 return 0;
353 * ext2_find_entry()
355 * finds an entry in the specified directory with the wanted name. It
356 * returns the page in which the entry was found, and the entry itself
357 * (as a parameter - res_dir). Page is returned mapped and unlocked.
358 * Entry is guaranteed to be valid.
360 struct ext2_dir_entry_2 *ext2_find_entry (struct inode * dir,
361 struct qstr *child, struct page ** res_page)
363 const char *name = child->name;
364 int namelen = child->len;
365 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
366 unsigned long start, n;
367 unsigned long npages = dir_pages(dir);
368 struct page *page = NULL;
369 struct ext2_inode_info *ei = EXT2_I(dir);
370 ext2_dirent * de;
371 int dir_has_error = 0;
373 if (npages == 0)
374 goto out;
376 /* OFFSET_CACHE */
377 *res_page = NULL;
379 start = ei->i_dir_start_lookup;
380 if (start >= npages)
381 start = 0;
382 n = start;
383 do {
384 char *kaddr;
385 page = ext2_get_page(dir, n, dir_has_error);
386 if (!IS_ERR(page)) {
387 kaddr = page_address(page);
388 de = (ext2_dirent *) kaddr;
389 kaddr += ext2_last_byte(dir, n) - reclen;
390 while ((char *) de <= kaddr) {
391 if (de->rec_len == 0) {
392 ext2_error(dir->i_sb, __func__,
393 "zero-length directory entry");
394 ext2_put_page(page);
395 goto out;
397 if (ext2_match (namelen, name, de))
398 goto found;
399 de = ext2_next_entry(de);
401 ext2_put_page(page);
402 } else
403 dir_has_error = 1;
405 if (++n >= npages)
406 n = 0;
407 /* next page is past the blocks we've got */
408 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
409 ext2_error(dir->i_sb, __func__,
410 "dir %lu size %lld exceeds block count %llu",
411 dir->i_ino, dir->i_size,
412 (unsigned long long)dir->i_blocks);
413 goto out;
415 } while (n != start);
416 out:
417 return NULL;
419 found:
420 *res_page = page;
421 ei->i_dir_start_lookup = n;
422 return de;
425 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
427 struct page *page = ext2_get_page(dir, 0, 0);
428 ext2_dirent *de = NULL;
430 if (!IS_ERR(page)) {
431 de = ext2_next_entry((ext2_dirent *) page_address(page));
432 *p = page;
434 return de;
437 ino_t ext2_inode_by_name(struct inode *dir, struct qstr *child)
439 ino_t res = 0;
440 struct ext2_dir_entry_2 *de;
441 struct page *page;
443 de = ext2_find_entry (dir, child, &page);
444 if (de) {
445 res = le32_to_cpu(de->inode);
446 ext2_put_page(page);
448 return res;
451 /* Releases the page */
452 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
453 struct page *page, struct inode *inode)
455 loff_t pos = page_offset(page) +
456 (char *) de - (char *) page_address(page);
457 unsigned len = ext2_rec_len_from_disk(de->rec_len);
458 int err;
460 lock_page(page);
461 err = __ext2_write_begin(NULL, page->mapping, pos, len,
462 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
463 BUG_ON(err);
464 de->inode = cpu_to_le32(inode->i_ino);
465 ext2_set_de_type(de, inode);
466 err = ext2_commit_chunk(page, pos, len);
467 ext2_put_page(page);
468 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
469 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
470 mark_inode_dirty(dir);
474 * Parent is locked.
476 int ext2_add_link (struct dentry *dentry, struct inode *inode)
478 struct inode *dir = dentry->d_parent->d_inode;
479 const char *name = dentry->d_name.name;
480 int namelen = dentry->d_name.len;
481 unsigned chunk_size = ext2_chunk_size(dir);
482 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
483 unsigned short rec_len, name_len;
484 struct page *page = NULL;
485 ext2_dirent * de;
486 unsigned long npages = dir_pages(dir);
487 unsigned long n;
488 char *kaddr;
489 loff_t pos;
490 int err;
493 * We take care of directory expansion in the same loop.
494 * This code plays outside i_size, so it locks the page
495 * to protect that region.
497 for (n = 0; n <= npages; n++) {
498 char *dir_end;
500 page = ext2_get_page(dir, n, 0);
501 err = PTR_ERR(page);
502 if (IS_ERR(page))
503 goto out;
504 lock_page(page);
505 kaddr = page_address(page);
506 dir_end = kaddr + ext2_last_byte(dir, n);
507 de = (ext2_dirent *)kaddr;
508 kaddr += PAGE_CACHE_SIZE - reclen;
509 while ((char *)de <= kaddr) {
510 if ((char *)de == dir_end) {
511 /* We hit i_size */
512 name_len = 0;
513 rec_len = chunk_size;
514 de->rec_len = ext2_rec_len_to_disk(chunk_size);
515 de->inode = 0;
516 goto got_it;
518 if (de->rec_len == 0) {
519 ext2_error(dir->i_sb, __func__,
520 "zero-length directory entry");
521 err = -EIO;
522 goto out_unlock;
524 err = -EEXIST;
525 if (ext2_match (namelen, name, de))
526 goto out_unlock;
527 name_len = EXT2_DIR_REC_LEN(de->name_len);
528 rec_len = ext2_rec_len_from_disk(de->rec_len);
529 if (!de->inode && rec_len >= reclen)
530 goto got_it;
531 if (rec_len >= name_len + reclen)
532 goto got_it;
533 de = (ext2_dirent *) ((char *) de + rec_len);
535 unlock_page(page);
536 ext2_put_page(page);
538 BUG();
539 return -EINVAL;
541 got_it:
542 pos = page_offset(page) +
543 (char*)de - (char*)page_address(page);
544 err = __ext2_write_begin(NULL, page->mapping, pos, rec_len, 0,
545 &page, NULL);
546 if (err)
547 goto out_unlock;
548 if (de->inode) {
549 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
550 de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
551 de->rec_len = ext2_rec_len_to_disk(name_len);
552 de = de1;
554 de->name_len = namelen;
555 memcpy(de->name, name, namelen);
556 de->inode = cpu_to_le32(inode->i_ino);
557 ext2_set_de_type (de, inode);
558 err = ext2_commit_chunk(page, pos, rec_len);
559 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
560 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
561 mark_inode_dirty(dir);
562 /* OFFSET_CACHE */
563 out_put:
564 ext2_put_page(page);
565 out:
566 return err;
567 out_unlock:
568 unlock_page(page);
569 goto out_put;
573 * ext2_delete_entry deletes a directory entry by merging it with the
574 * previous entry. Page is up-to-date. Releases the page.
576 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
578 struct address_space *mapping = page->mapping;
579 struct inode *inode = mapping->host;
580 char *kaddr = page_address(page);
581 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
582 unsigned to = ((char *)dir - kaddr) +
583 ext2_rec_len_from_disk(dir->rec_len);
584 loff_t pos;
585 ext2_dirent * pde = NULL;
586 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
587 int err;
589 while ((char*)de < (char*)dir) {
590 if (de->rec_len == 0) {
591 ext2_error(inode->i_sb, __func__,
592 "zero-length directory entry");
593 err = -EIO;
594 goto out;
596 pde = de;
597 de = ext2_next_entry(de);
599 if (pde)
600 from = (char*)pde - (char*)page_address(page);
601 pos = page_offset(page) + from;
602 lock_page(page);
603 err = __ext2_write_begin(NULL, page->mapping, pos, to - from, 0,
604 &page, NULL);
605 BUG_ON(err);
606 if (pde)
607 pde->rec_len = ext2_rec_len_to_disk(to - from);
608 dir->inode = 0;
609 err = ext2_commit_chunk(page, pos, to - from);
610 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
611 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
612 mark_inode_dirty(inode);
613 out:
614 ext2_put_page(page);
615 return err;
619 * Set the first fragment of directory.
621 int ext2_make_empty(struct inode *inode, struct inode *parent)
623 struct address_space *mapping = inode->i_mapping;
624 struct page *page = grab_cache_page(mapping, 0);
625 unsigned chunk_size = ext2_chunk_size(inode);
626 struct ext2_dir_entry_2 * de;
627 int err;
628 void *kaddr;
630 if (!page)
631 return -ENOMEM;
633 err = __ext2_write_begin(NULL, page->mapping, 0, chunk_size, 0,
634 &page, NULL);
635 if (err) {
636 unlock_page(page);
637 goto fail;
639 kaddr = kmap_atomic(page, KM_USER0);
640 memset(kaddr, 0, chunk_size);
641 de = (struct ext2_dir_entry_2 *)kaddr;
642 de->name_len = 1;
643 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
644 memcpy (de->name, ".\0\0", 4);
645 de->inode = cpu_to_le32(inode->i_ino);
646 ext2_set_de_type (de, inode);
648 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
649 de->name_len = 2;
650 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
651 de->inode = cpu_to_le32(parent->i_ino);
652 memcpy (de->name, "..\0", 4);
653 ext2_set_de_type (de, inode);
654 kunmap_atomic(kaddr, KM_USER0);
655 err = ext2_commit_chunk(page, 0, chunk_size);
656 fail:
657 page_cache_release(page);
658 return err;
662 * routine to check that the specified directory is empty (for rmdir)
664 int ext2_empty_dir (struct inode * inode)
666 struct page *page = NULL;
667 unsigned long i, npages = dir_pages(inode);
668 int dir_has_error = 0;
670 for (i = 0; i < npages; i++) {
671 char *kaddr;
672 ext2_dirent * de;
673 page = ext2_get_page(inode, i, dir_has_error);
675 if (IS_ERR(page)) {
676 dir_has_error = 1;
677 continue;
680 kaddr = page_address(page);
681 de = (ext2_dirent *)kaddr;
682 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
684 while ((char *)de <= kaddr) {
685 if (de->rec_len == 0) {
686 ext2_error(inode->i_sb, __func__,
687 "zero-length directory entry");
688 printk("kaddr=%p, de=%p\n", kaddr, de);
689 goto not_empty;
691 if (de->inode != 0) {
692 /* check for . and .. */
693 if (de->name[0] != '.')
694 goto not_empty;
695 if (de->name_len > 2)
696 goto not_empty;
697 if (de->name_len < 2) {
698 if (de->inode !=
699 cpu_to_le32(inode->i_ino))
700 goto not_empty;
701 } else if (de->name[1] != '.')
702 goto not_empty;
704 de = ext2_next_entry(de);
706 ext2_put_page(page);
708 return 1;
710 not_empty:
711 ext2_put_page(page);
712 return 0;
715 const struct file_operations ext2_dir_operations = {
716 .llseek = generic_file_llseek,
717 .read = generic_read_dir,
718 .readdir = ext2_readdir,
719 .unlocked_ioctl = ext2_ioctl,
720 #ifdef CONFIG_COMPAT
721 .compat_ioctl = ext2_compat_ioctl,
722 #endif
723 .fsync = ext2_sync_file,