blob: 11a49ce84392a1e39bc1c335273c4f1ab2ee0d8f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext2/dir.c
3 *
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)
8 *
9 * from
10 *
11 * linux/fs/minix/dir.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * ext2 directory handling functions
16 *
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
19 *
20 * All code that works with directory layout had been switched to pagecache
21 * and moved here. AV
22 */
23
24#include "ext2.h"
Nick Pigginf34fb6e2007-10-16 01:25:04 -070025#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/pagemap.h>
Nick Pigginf34fb6e2007-10-16 01:25:04 -070027#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29typedef struct ext2_dir_entry_2 ext2_dirent;
30
Jan Kara89910cc2007-10-21 16:41:40 -070031static inline unsigned ext2_rec_len_from_disk(__le16 dlen)
32{
33 unsigned len = le16_to_cpu(dlen);
34
35 if (len == EXT2_MAX_REC_LEN)
36 return 1 << 16;
37 return len;
38}
39
40static inline __le16 ext2_rec_len_to_disk(unsigned len)
41{
42 if (len == (1 << 16))
43 return cpu_to_le16(EXT2_MAX_REC_LEN);
Julia Lawall2c116192008-04-28 02:16:02 -070044 else
45 BUG_ON(len > (1 << 16));
Jan Kara89910cc2007-10-21 16:41:40 -070046 return cpu_to_le16(len);
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
51 * more robust, but we have what we have
52 */
53static inline unsigned ext2_chunk_size(struct inode *inode)
54{
55 return inode->i_sb->s_blocksize;
56}
57
58static inline void ext2_put_page(struct page *page)
59{
60 kunmap(page);
61 page_cache_release(page);
62}
63
64static 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 */
73static unsigned
74ext2_last_byte(struct inode *inode, unsigned long page_nr)
75{
76 unsigned last_byte = inode->i_size;
77
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;
82}
83
Nick Pigginf34fb6e2007-10-16 01:25:04 -070084static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Nick Pigginf34fb6e2007-10-16 01:25:04 -070086 struct address_space *mapping = page->mapping;
87 struct inode *dir = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int err = 0;
Nick Pigginf34fb6e2007-10-16 01:25:04 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 dir->i_version++;
Nick Pigginf34fb6e2007-10-16 01:25:04 -070091 block_write_end(NULL, mapping, pos, len, len, page, NULL);
92
93 if (pos+len > dir->i_size) {
94 i_size_write(dir, pos+len);
95 mark_inode_dirty(dir);
96 }
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (IS_DIRSYNC(dir))
99 err = write_one_page(page, 1);
100 else
101 unlock_page(page);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return err;
104}
105
Eric Sandeenbd395972008-10-15 22:04:02 -0700106static void ext2_check_page(struct page *page, int quiet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct inode *dir = page->mapping->host;
109 struct super_block *sb = dir->i_sb;
110 unsigned chunk_size = ext2_chunk_size(dir);
111 char *kaddr = page_address(page);
112 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
113 unsigned offs, rec_len;
114 unsigned limit = PAGE_CACHE_SIZE;
115 ext2_dirent *p;
116 char *error;
117
118 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
119 limit = dir->i_size & ~PAGE_CACHE_MASK;
120 if (limit & (chunk_size - 1))
121 goto Ebadsize;
122 if (!limit)
123 goto out;
124 }
125 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
126 p = (ext2_dirent *)(kaddr + offs);
Jan Kara89910cc2007-10-21 16:41:40 -0700127 rec_len = ext2_rec_len_from_disk(p->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 if (rec_len < EXT2_DIR_REC_LEN(1))
130 goto Eshort;
131 if (rec_len & 3)
132 goto Ealign;
133 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
134 goto Enamelen;
135 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
136 goto Espan;
137 if (le32_to_cpu(p->inode) > max_inumber)
138 goto Einumber;
139 }
140 if (offs != limit)
141 goto Eend;
142out:
143 SetPageChecked(page);
144 return;
145
146 /* Too bad, we had an error */
147
148Ebadsize:
Eric Sandeenbd395972008-10-15 22:04:02 -0700149 if (!quiet)
150 ext2_error(sb, __func__,
151 "size of directory #%lu is not a multiple "
152 "of chunk size", dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 goto fail;
154Eshort:
155 error = "rec_len is smaller than minimal";
156 goto bad_entry;
157Ealign:
158 error = "unaligned directory entry";
159 goto bad_entry;
160Enamelen:
161 error = "rec_len is too small for name_len";
162 goto bad_entry;
163Espan:
164 error = "directory entry across blocks";
165 goto bad_entry;
166Einumber:
167 error = "inode out of bounds";
168bad_entry:
Eric Sandeenbd395972008-10-15 22:04:02 -0700169 if (!quiet)
170 ext2_error(sb, __func__, "bad entry in directory #%lu: : %s - "
171 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
172 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
173 (unsigned long) le32_to_cpu(p->inode),
174 rec_len, p->name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto fail;
176Eend:
Eric Sandeenbd395972008-10-15 22:04:02 -0700177 if (!quiet) {
178 p = (ext2_dirent *)(kaddr + offs);
179 ext2_error(sb, "ext2_check_page",
180 "entry in directory #%lu spans the page boundary"
181 "offset=%lu, inode=%lu",
182 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
183 (unsigned long) le32_to_cpu(p->inode));
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185fail:
186 SetPageChecked(page);
187 SetPageError(page);
188}
189
Eric Sandeenbd395972008-10-15 22:04:02 -0700190static struct page * ext2_get_page(struct inode *dir, unsigned long n,
191 int quiet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct address_space *mapping = dir->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -0700194 struct page *page = read_mapping_page(mapping, n, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!IS_ERR(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!PageChecked(page))
Eric Sandeenbd395972008-10-15 22:04:02 -0700198 ext2_check_page(page, quiet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (PageError(page))
200 goto fail;
201 }
202 return page;
203
204fail:
205 ext2_put_page(page);
206 return ERR_PTR(-EIO);
207}
208
209/*
210 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
211 *
212 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
213 */
214static inline int ext2_match (int len, const char * const name,
215 struct ext2_dir_entry_2 * de)
216{
217 if (len != de->name_len)
218 return 0;
219 if (!de->inode)
220 return 0;
221 return !memcmp(name, de->name, len);
222}
223
224/*
225 * p is at least 6 bytes before the end of page
226 */
227static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
228{
Jan Kara89910cc2007-10-21 16:41:40 -0700229 return (ext2_dirent *)((char *)p +
230 ext2_rec_len_from_disk(p->rec_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233static inline unsigned
234ext2_validate_entry(char *base, unsigned offset, unsigned mask)
235{
236 ext2_dirent *de = (ext2_dirent*)(base + offset);
237 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
238 while ((char*)p < (char*)de) {
239 if (p->rec_len == 0)
240 break;
241 p = ext2_next_entry(p);
242 }
243 return (char *)p - base;
244}
245
246static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
247 [EXT2_FT_UNKNOWN] = DT_UNKNOWN,
248 [EXT2_FT_REG_FILE] = DT_REG,
249 [EXT2_FT_DIR] = DT_DIR,
250 [EXT2_FT_CHRDEV] = DT_CHR,
251 [EXT2_FT_BLKDEV] = DT_BLK,
252 [EXT2_FT_FIFO] = DT_FIFO,
253 [EXT2_FT_SOCK] = DT_SOCK,
254 [EXT2_FT_SYMLINK] = DT_LNK,
255};
256
257#define S_SHIFT 12
258static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
259 [S_IFREG >> S_SHIFT] = EXT2_FT_REG_FILE,
260 [S_IFDIR >> S_SHIFT] = EXT2_FT_DIR,
261 [S_IFCHR >> S_SHIFT] = EXT2_FT_CHRDEV,
262 [S_IFBLK >> S_SHIFT] = EXT2_FT_BLKDEV,
263 [S_IFIFO >> S_SHIFT] = EXT2_FT_FIFO,
264 [S_IFSOCK >> S_SHIFT] = EXT2_FT_SOCK,
265 [S_IFLNK >> S_SHIFT] = EXT2_FT_SYMLINK,
266};
267
268static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
269{
270 mode_t mode = inode->i_mode;
271 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
272 de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
273 else
274 de->file_type = 0;
275}
276
277static int
278ext2_readdir (struct file * filp, void * dirent, filldir_t filldir)
279{
280 loff_t pos = filp->f_pos;
Josef "Jeff" Sipekc29c6932006-12-08 02:36:37 -0800281 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct super_block *sb = inode->i_sb;
283 unsigned int offset = pos & ~PAGE_CACHE_MASK;
284 unsigned long n = pos >> PAGE_CACHE_SHIFT;
285 unsigned long npages = dir_pages(inode);
286 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
287 unsigned char *types = NULL;
Al Viro2d7f2ea2006-03-15 21:41:59 +0000288 int need_revalidate = filp->f_version != inode->i_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
Al Viro2d7f2ea2006-03-15 21:41:59 +0000291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
294 types = ext2_filetype_table;
295
296 for ( ; n < npages; n++, offset = 0) {
297 char *kaddr, *limit;
298 ext2_dirent *de;
Eric Sandeenbd395972008-10-15 22:04:02 -0700299 struct page *page = ext2_get_page(inode, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (IS_ERR(page)) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700302 ext2_error(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 "bad page in #%lu",
304 inode->i_ino);
305 filp->f_pos += PAGE_CACHE_SIZE - offset;
Akinobu Mitabbff2862008-04-28 02:16:00 -0700306 return PTR_ERR(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 kaddr = page_address(page);
Al Viro2d7f2ea2006-03-15 21:41:59 +0000309 if (unlikely(need_revalidate)) {
310 if (offset) {
311 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
312 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
313 }
314 filp->f_version = inode->i_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 need_revalidate = 0;
316 }
317 de = (ext2_dirent *)(kaddr+offset);
318 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
319 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
320 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700321 ext2_error(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 "zero-length directory entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ext2_put_page(page);
Al Viro2d7f2ea2006-03-15 21:41:59 +0000324 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 if (de->inode) {
327 int over;
328 unsigned char d_type = DT_UNKNOWN;
329
330 if (types && de->file_type < EXT2_FT_MAX)
331 d_type = types[de->file_type];
332
333 offset = (char *)de - kaddr;
334 over = filldir(dirent, de->name, de->name_len,
335 (n<<PAGE_CACHE_SHIFT) | offset,
336 le32_to_cpu(de->inode), d_type);
337 if (over) {
338 ext2_put_page(page);
Al Viro2d7f2ea2006-03-15 21:41:59 +0000339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 }
Jan Kara89910cc2007-10-21 16:41:40 -0700342 filp->f_pos += ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 ext2_put_page(page);
345 }
Al Viro2d7f2ea2006-03-15 21:41:59 +0000346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/*
350 * ext2_find_entry()
351 *
352 * finds an entry in the specified directory with the wanted name. It
353 * returns the page in which the entry was found, and the entry itself
354 * (as a parameter - res_dir). Page is returned mapped and unlocked.
355 * Entry is guaranteed to be valid.
356 */
357struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
358 struct dentry *dentry, struct page ** res_page)
359{
360 const char *name = dentry->d_name.name;
361 int namelen = dentry->d_name.len;
362 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
363 unsigned long start, n;
364 unsigned long npages = dir_pages(dir);
365 struct page *page = NULL;
366 struct ext2_inode_info *ei = EXT2_I(dir);
367 ext2_dirent * de;
Eric Sandeenbd395972008-10-15 22:04:02 -0700368 int dir_has_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (npages == 0)
371 goto out;
372
373 /* OFFSET_CACHE */
374 *res_page = NULL;
375
376 start = ei->i_dir_start_lookup;
377 if (start >= npages)
378 start = 0;
379 n = start;
380 do {
381 char *kaddr;
Eric Sandeenbd395972008-10-15 22:04:02 -0700382 page = ext2_get_page(dir, n, dir_has_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!IS_ERR(page)) {
384 kaddr = page_address(page);
385 de = (ext2_dirent *) kaddr;
386 kaddr += ext2_last_byte(dir, n) - reclen;
387 while ((char *) de <= kaddr) {
388 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700389 ext2_error(dir->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 "zero-length directory entry");
391 ext2_put_page(page);
392 goto out;
393 }
394 if (ext2_match (namelen, name, de))
395 goto found;
396 de = ext2_next_entry(de);
397 }
398 ext2_put_page(page);
Eric Sandeenbd395972008-10-15 22:04:02 -0700399 } else
400 dir_has_error = 1;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (++n >= npages)
403 n = 0;
Eric Sandeend8adb9c2007-02-10 01:45:06 -0800404 /* next page is past the blocks we've got */
405 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700406 ext2_error(dir->i_sb, __func__,
Eric Sandeend8adb9c2007-02-10 01:45:06 -0800407 "dir %lu size %lld exceeds block count %llu",
408 dir->i_ino, dir->i_size,
409 (unsigned long long)dir->i_blocks);
410 goto out;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 } while (n != start);
413out:
414 return NULL;
415
416found:
417 *res_page = page;
418 ei->i_dir_start_lookup = n;
419 return de;
420}
421
422struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
423{
Eric Sandeenbd395972008-10-15 22:04:02 -0700424 struct page *page = ext2_get_page(dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ext2_dirent *de = NULL;
426
427 if (!IS_ERR(page)) {
428 de = ext2_next_entry((ext2_dirent *) page_address(page));
429 *p = page;
430 }
431 return de;
432}
433
434ino_t ext2_inode_by_name(struct inode * dir, struct dentry *dentry)
435{
436 ino_t res = 0;
437 struct ext2_dir_entry_2 * de;
438 struct page *page;
439
440 de = ext2_find_entry (dir, dentry, &page);
441 if (de) {
442 res = le32_to_cpu(de->inode);
Evgeniy Dushistov7d93a1a532006-06-25 05:49:27 -0700443 ext2_put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 return res;
446}
447
448/* Releases the page */
449void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
450 struct page *page, struct inode *inode)
451{
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700452 loff_t pos = page_offset(page) +
453 (char *) de - (char *) page_address(page);
Jan Kara89910cc2007-10-21 16:41:40 -0700454 unsigned len = ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int err;
456
457 lock_page(page);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700458 err = __ext2_write_begin(NULL, page->mapping, pos, len,
459 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
Eric Sesterhenn309be532006-03-26 18:27:41 +0200460 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 de->inode = cpu_to_le32(inode->i_ino);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700462 ext2_set_de_type(de, inode);
463 err = ext2_commit_chunk(page, pos, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ext2_put_page(page);
465 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
466 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
467 mark_inode_dirty(dir);
468}
469
470/*
471 * Parent is locked.
472 */
473int ext2_add_link (struct dentry *dentry, struct inode *inode)
474{
475 struct inode *dir = dentry->d_parent->d_inode;
476 const char *name = dentry->d_name.name;
477 int namelen = dentry->d_name.len;
478 unsigned chunk_size = ext2_chunk_size(dir);
479 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
480 unsigned short rec_len, name_len;
481 struct page *page = NULL;
482 ext2_dirent * de;
483 unsigned long npages = dir_pages(dir);
484 unsigned long n;
485 char *kaddr;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700486 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int err;
488
489 /*
490 * We take care of directory expansion in the same loop.
491 * This code plays outside i_size, so it locks the page
492 * to protect that region.
493 */
494 for (n = 0; n <= npages; n++) {
495 char *dir_end;
496
Eric Sandeenbd395972008-10-15 22:04:02 -0700497 page = ext2_get_page(dir, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 err = PTR_ERR(page);
499 if (IS_ERR(page))
500 goto out;
501 lock_page(page);
502 kaddr = page_address(page);
503 dir_end = kaddr + ext2_last_byte(dir, n);
504 de = (ext2_dirent *)kaddr;
505 kaddr += PAGE_CACHE_SIZE - reclen;
506 while ((char *)de <= kaddr) {
507 if ((char *)de == dir_end) {
508 /* We hit i_size */
509 name_len = 0;
510 rec_len = chunk_size;
Jan Kara89910cc2007-10-21 16:41:40 -0700511 de->rec_len = ext2_rec_len_to_disk(chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 de->inode = 0;
513 goto got_it;
514 }
515 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700516 ext2_error(dir->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 "zero-length directory entry");
518 err = -EIO;
519 goto out_unlock;
520 }
521 err = -EEXIST;
522 if (ext2_match (namelen, name, de))
523 goto out_unlock;
524 name_len = EXT2_DIR_REC_LEN(de->name_len);
Jan Kara89910cc2007-10-21 16:41:40 -0700525 rec_len = ext2_rec_len_from_disk(de->rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (!de->inode && rec_len >= reclen)
527 goto got_it;
528 if (rec_len >= name_len + reclen)
529 goto got_it;
530 de = (ext2_dirent *) ((char *) de + rec_len);
531 }
532 unlock_page(page);
533 ext2_put_page(page);
534 }
535 BUG();
536 return -EINVAL;
537
538got_it:
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700539 pos = page_offset(page) +
540 (char*)de - (char*)page_address(page);
541 err = __ext2_write_begin(NULL, page->mapping, pos, rec_len, 0,
542 &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (err)
544 goto out_unlock;
545 if (de->inode) {
546 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
Jan Kara89910cc2007-10-21 16:41:40 -0700547 de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
548 de->rec_len = ext2_rec_len_to_disk(name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 de = de1;
550 }
551 de->name_len = namelen;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700552 memcpy(de->name, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 de->inode = cpu_to_le32(inode->i_ino);
554 ext2_set_de_type (de, inode);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700555 err = ext2_commit_chunk(page, pos, rec_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
557 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
558 mark_inode_dirty(dir);
559 /* OFFSET_CACHE */
560out_put:
561 ext2_put_page(page);
562out:
563 return err;
564out_unlock:
565 unlock_page(page);
566 goto out_put;
567}
568
569/*
570 * ext2_delete_entry deletes a directory entry by merging it with the
571 * previous entry. Page is up-to-date. Releases the page.
572 */
573int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
574{
575 struct address_space *mapping = page->mapping;
576 struct inode *inode = mapping->host;
577 char *kaddr = page_address(page);
578 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
Jan Kara89910cc2007-10-21 16:41:40 -0700579 unsigned to = ((char *)dir - kaddr) +
580 ext2_rec_len_from_disk(dir->rec_len);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700581 loff_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ext2_dirent * pde = NULL;
583 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
584 int err;
585
586 while ((char*)de < (char*)dir) {
587 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700588 ext2_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 "zero-length directory entry");
590 err = -EIO;
591 goto out;
592 }
593 pde = de;
594 de = ext2_next_entry(de);
595 }
596 if (pde)
597 from = (char*)pde - (char*)page_address(page);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700598 pos = page_offset(page) + from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 lock_page(page);
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700600 err = __ext2_write_begin(NULL, page->mapping, pos, to - from, 0,
601 &page, NULL);
Eric Sesterhenn309be532006-03-26 18:27:41 +0200602 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (pde)
Jan Kara89910cc2007-10-21 16:41:40 -0700604 pde->rec_len = ext2_rec_len_to_disk(to - from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 dir->inode = 0;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700606 err = ext2_commit_chunk(page, pos, to - from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
608 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
609 mark_inode_dirty(inode);
610out:
611 ext2_put_page(page);
612 return err;
613}
614
615/*
616 * Set the first fragment of directory.
617 */
618int ext2_make_empty(struct inode *inode, struct inode *parent)
619{
620 struct address_space *mapping = inode->i_mapping;
621 struct page *page = grab_cache_page(mapping, 0);
622 unsigned chunk_size = ext2_chunk_size(inode);
623 struct ext2_dir_entry_2 * de;
624 int err;
625 void *kaddr;
626
627 if (!page)
628 return -ENOMEM;
Nick Pigginf34fb6e2007-10-16 01:25:04 -0700629
630 err = __ext2_write_begin(NULL, page->mapping, 0, chunk_size, 0,
631 &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (err) {
633 unlock_page(page);
634 goto fail;
635 }
636 kaddr = kmap_atomic(page, KM_USER0);
Luiz Fernando Capitulino7823c7c2006-01-11 01:38:27 +0100637 memset(kaddr, 0, chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 de = (struct ext2_dir_entry_2 *)kaddr;
639 de->name_len = 1;
Jan Kara89910cc2007-10-21 16:41:40 -0700640 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 memcpy (de->name, ".\0\0", 4);
642 de->inode = cpu_to_le32(inode->i_ino);
643 ext2_set_de_type (de, inode);
644
645 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
646 de->name_len = 2;
Jan Kara89910cc2007-10-21 16:41:40 -0700647 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 de->inode = cpu_to_le32(parent->i_ino);
649 memcpy (de->name, "..\0", 4);
650 ext2_set_de_type (de, inode);
651 kunmap_atomic(kaddr, KM_USER0);
652 err = ext2_commit_chunk(page, 0, chunk_size);
653fail:
654 page_cache_release(page);
655 return err;
656}
657
658/*
659 * routine to check that the specified directory is empty (for rmdir)
660 */
661int ext2_empty_dir (struct inode * inode)
662{
663 struct page *page = NULL;
664 unsigned long i, npages = dir_pages(inode);
Eric Sandeenbd395972008-10-15 22:04:02 -0700665 int dir_has_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 for (i = 0; i < npages; i++) {
668 char *kaddr;
669 ext2_dirent * de;
Eric Sandeenbd395972008-10-15 22:04:02 -0700670 page = ext2_get_page(inode, i, dir_has_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Eric Sandeenbd395972008-10-15 22:04:02 -0700672 if (IS_ERR(page)) {
673 dir_has_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
Eric Sandeenbd395972008-10-15 22:04:02 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 kaddr = page_address(page);
678 de = (ext2_dirent *)kaddr;
679 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
680
681 while ((char *)de <= kaddr) {
682 if (de->rec_len == 0) {
Harvey Harrison605afd62008-04-28 02:16:03 -0700683 ext2_error(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 "zero-length directory entry");
685 printk("kaddr=%p, de=%p\n", kaddr, de);
686 goto not_empty;
687 }
688 if (de->inode != 0) {
689 /* check for . and .. */
690 if (de->name[0] != '.')
691 goto not_empty;
692 if (de->name_len > 2)
693 goto not_empty;
694 if (de->name_len < 2) {
695 if (de->inode !=
696 cpu_to_le32(inode->i_ino))
697 goto not_empty;
698 } else if (de->name[1] != '.')
699 goto not_empty;
700 }
701 de = ext2_next_entry(de);
702 }
703 ext2_put_page(page);
704 }
705 return 1;
706
707not_empty:
708 ext2_put_page(page);
709 return 0;
710}
711
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800712const struct file_operations ext2_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 .llseek = generic_file_llseek,
714 .read = generic_read_dir,
715 .readdir = ext2_readdir,
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800716 .unlocked_ioctl = ext2_ioctl,
David Howellse322ff02006-08-29 19:06:20 +0100717#ifdef CONFIG_COMPAT
718 .compat_ioctl = ext2_compat_ioctl,
719#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 .fsync = ext2_sync_file,
721};