1 /*
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 <linux/fs.h>
25 #include <linux/ext2_fs.h>
26 #include <linux/pagemap.h>
27
28 typedef struct ext2_dir_entry_2 ext2_dirent;
29
30 /*
31 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
32 * more robust, but we have what we have
33 */
ext2_chunk_size(struct inode * inode)34 static inline unsigned ext2_chunk_size(struct inode *inode)
35 {
36 return inode->i_sb->s_blocksize;
37 }
38
ext2_put_page(struct page * page)39 static inline void ext2_put_page(struct page *page)
40 {
41 kunmap(page);
42 page_cache_release(page);
43 }
44
dir_pages(struct inode * inode)45 static inline unsigned long dir_pages(struct inode *inode)
46 {
47 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
48 }
49
ext2_commit_chunk(struct page * page,unsigned from,unsigned to)50 static int ext2_commit_chunk(struct page *page, unsigned from, unsigned to)
51 {
52 struct inode *dir = page->mapping->host;
53 int err = 0;
54 dir->i_version = ++event;
55 page->mapping->a_ops->commit_write(NULL, page, from, to);
56 if (IS_SYNC(dir)) {
57 int err2;
58 err = writeout_one_page(page);
59 err2 = waitfor_one_page(page);
60 if (err == 0)
61 err = err2;
62 }
63 return err;
64 }
65
ext2_check_page(struct page * page,int quiet)66 static void ext2_check_page(struct page *page, int quiet)
67 {
68 struct inode *dir = page->mapping->host;
69 struct super_block *sb = dir->i_sb;
70 unsigned chunk_size = ext2_chunk_size(dir);
71 char *kaddr = page_address(page);
72 u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
73 unsigned offs, rec_len;
74 unsigned limit = PAGE_CACHE_SIZE;
75 ext2_dirent *p;
76 char *error;
77
78 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
79 limit = dir->i_size & ~PAGE_CACHE_MASK;
80 if (limit & (chunk_size - 1))
81 goto Ebadsize;
82 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
83 ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
84 p->rec_len = cpu_to_le16(chunk_size);
85 }
86 if (!limit)
87 goto out;
88 }
89 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
90 p = (ext2_dirent *)(kaddr + offs);
91 rec_len = le16_to_cpu(p->rec_len);
92
93 if (rec_len < EXT2_DIR_REC_LEN(1))
94 goto Eshort;
95 if (rec_len & 3)
96 goto Ealign;
97 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
98 goto Enamelen;
99 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
100 goto Espan;
101 if (le32_to_cpu(p->inode) > max_inumber)
102 goto Einumber;
103 }
104 if (offs != limit)
105 goto Eend;
106 out:
107 SetPageChecked(page);
108 return;
109
110 /* Too bad, we had an error */
111
112 Ebadsize:
113 if (!quiet)
114 ext2_error(sb, __func__,
115 "size of directory #%lu is not a multiple "
116 "of chunk size", dir->i_ino);
117 goto fail;
118 Eshort:
119 error = "rec_len is smaller than minimal";
120 goto bad_entry;
121 Ealign:
122 error = "unaligned directory entry";
123 goto bad_entry;
124 Enamelen:
125 error = "rec_len is too small for name_len";
126 goto bad_entry;
127 Espan:
128 error = "directory entry across blocks";
129 goto bad_entry;
130 Einumber:
131 error = "inode out of bounds";
132 bad_entry:
133 if (!quiet)
134 ext2_error(sb, __func__, "bad entry in directory #%lu: : %s - "
135 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
136 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
137 (unsigned long) le32_to_cpu(p->inode),
138 rec_len, p->name_len);
139 goto fail;
140 Eend:
141 if (!quiet) {
142 p = (ext2_dirent *)(kaddr + offs);
143 ext2_error(sb, "ext2_check_page",
144 "entry in directory #%lu spans the page boundary"
145 "offset=%lu, inode=%lu",
146 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
147 (unsigned long) le32_to_cpu(p->inode));
148 }
149 fail:
150 SetPageChecked(page);
151 SetPageError(page);
152 }
153
ext2_get_page(struct inode * dir,unsigned long n,int quiet)154 static struct page * ext2_get_page(struct inode *dir, unsigned long n,
155 int quiet)
156 {
157 struct address_space *mapping = dir->i_mapping;
158 struct page *page = read_cache_page(mapping, n,
159 (filler_t*)mapping->a_ops->readpage, NULL);
160 if (!IS_ERR(page)) {
161 wait_on_page(page);
162 kmap(page);
163 if (!Page_Uptodate(page))
164 goto fail;
165 if (!PageChecked(page))
166 ext2_check_page(page, quiet);
167 if (PageError(page))
168 goto fail;
169 }
170 return page;
171
172 fail:
173 ext2_put_page(page);
174 return ERR_PTR(-EIO);
175 }
176
177 /*
178 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
179 *
180 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
181 */
ext2_match(int len,const char * const name,struct ext2_dir_entry_2 * de)182 static inline int ext2_match (int len, const char * const name,
183 struct ext2_dir_entry_2 * de)
184 {
185 if (len != de->name_len)
186 return 0;
187 if (!de->inode)
188 return 0;
189 return !memcmp(name, de->name, len);
190 }
191
192 /*
193 * p is at least 6 bytes before the end of page
194 */
ext2_next_entry(ext2_dirent * p)195 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
196 {
197 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
198 }
199
200 static inline unsigned
ext2_validate_entry(char * base,unsigned offset,unsigned mask)201 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
202 {
203 ext2_dirent *de = (ext2_dirent*)(base + offset);
204 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
205 while ((char*)p < (char*)de)
206 p = ext2_next_entry(p);
207 return (char *)p - base;
208 }
209
210 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
211 [EXT2_FT_UNKNOWN] DT_UNKNOWN,
212 [EXT2_FT_REG_FILE] DT_REG,
213 [EXT2_FT_DIR] DT_DIR,
214 [EXT2_FT_CHRDEV] DT_CHR,
215 [EXT2_FT_BLKDEV] DT_BLK,
216 [EXT2_FT_FIFO] DT_FIFO,
217 [EXT2_FT_SOCK] DT_SOCK,
218 [EXT2_FT_SYMLINK] DT_LNK,
219 };
220
221 #define S_SHIFT 12
222 static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
223 [S_IFREG >> S_SHIFT] EXT2_FT_REG_FILE,
224 [S_IFDIR >> S_SHIFT] EXT2_FT_DIR,
225 [S_IFCHR >> S_SHIFT] EXT2_FT_CHRDEV,
226 [S_IFBLK >> S_SHIFT] EXT2_FT_BLKDEV,
227 [S_IFIFO >> S_SHIFT] EXT2_FT_FIFO,
228 [S_IFSOCK >> S_SHIFT] EXT2_FT_SOCK,
229 [S_IFLNK >> S_SHIFT] EXT2_FT_SYMLINK,
230 };
231
ext2_set_de_type(ext2_dirent * de,struct inode * inode)232 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
233 {
234 mode_t mode = inode->i_mode;
235 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
236 de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
237 else
238 de->file_type = 0;
239 }
240
241 static int
ext2_readdir(struct file * filp,void * dirent,filldir_t filldir)242 ext2_readdir (struct file * filp, void * dirent, filldir_t filldir)
243 {
244 loff_t pos = filp->f_pos;
245 struct inode *inode = filp->f_dentry->d_inode;
246 struct super_block *sb = inode->i_sb;
247 unsigned int offset = pos & ~PAGE_CACHE_MASK;
248 unsigned long n = pos >> PAGE_CACHE_SHIFT;
249 unsigned long npages = dir_pages(inode);
250 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
251 unsigned char *types = NULL;
252 int need_revalidate = (filp->f_version != inode->i_version);
253 int ret = 0;
254
255 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
256 goto done;
257
258 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
259 types = ext2_filetype_table;
260
261 for ( ; n < npages; n++, offset = 0) {
262 char *kaddr, *limit;
263 ext2_dirent *de;
264 struct page *page = ext2_get_page(inode, n, 0);
265
266 if (IS_ERR(page)) {
267 ext2_error(sb, __FUNCTION__,
268 "bad page in #%lu",
269 inode->i_ino);
270 filp->f_pos += PAGE_CACHE_SIZE - offset;
271 ret = -EIO;
272 goto done;
273 }
274 kaddr = page_address(page);
275 if (need_revalidate) {
276 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
277 need_revalidate = 0;
278 }
279 de = (ext2_dirent *)(kaddr+offset);
280 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
281 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
282 if (de->inode) {
283 int over;
284 unsigned char d_type = DT_UNKNOWN;
285
286 if (types && de->file_type < EXT2_FT_MAX)
287 d_type = types[de->file_type];
288
289 offset = (char *)de - kaddr;
290 over = filldir(dirent, de->name, de->name_len,
291 (n<<PAGE_CACHE_SHIFT) | offset,
292 le32_to_cpu(de->inode), d_type);
293 if (over) {
294 ext2_put_page(page);
295 goto done;
296 }
297 }
298 filp->f_pos += le16_to_cpu(de->rec_len);
299 }
300 ext2_put_page(page);
301 }
302
303 done:
304 filp->f_version = inode->i_version;
305 UPDATE_ATIME(inode);
306 return ret;
307 }
308
309 /*
310 * ext2_find_entry()
311 *
312 * finds an entry in the specified directory with the wanted name. It
313 * returns the page in which the entry was found, and the entry itself
314 * (as a parameter - res_dir). Page is returned mapped and unlocked.
315 * Entry is guaranteed to be valid.
316 */
ext2_find_entry(struct inode * dir,struct dentry * dentry,struct page ** res_page)317 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
318 struct dentry *dentry, struct page ** res_page)
319 {
320 const char *name = dentry->d_name.name;
321 int namelen = dentry->d_name.len;
322 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
323 unsigned long start, n;
324 unsigned long npages = dir_pages(dir);
325 struct page *page = NULL;
326 ext2_dirent * de;
327 int dir_has_error = 0;
328
329 /* OFFSET_CACHE */
330 *res_page = NULL;
331
332 start = dir->u.ext2_i.i_dir_start_lookup;
333 if (start >= npages)
334 start = 0;
335 n = start;
336 do {
337 char *kaddr;
338 page = ext2_get_page(dir, n, dir_has_error);
339 if (!IS_ERR(page)) {
340 kaddr = page_address(page);
341 de = (ext2_dirent *) kaddr;
342 kaddr += PAGE_CACHE_SIZE - reclen;
343 while ((char *) de <= kaddr) {
344 if (ext2_match (namelen, name, de))
345 goto found;
346 de = ext2_next_entry(de);
347 }
348 ext2_put_page(page);
349 } else
350 dir_has_error = 1;
351
352 if (++n >= npages)
353 n = 0;
354 /* next page is past the blocks we've got */
355 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
356 ext2_error(dir->i_sb, __FUNCTION__,
357 "dir %lu size %lld exceeds block count %llu",
358 dir->i_ino, dir->i_size,
359 (unsigned long long)dir->i_blocks);
360 goto out;
361 }
362 } while (n != start);
363 out:
364 return NULL;
365
366 found:
367 *res_page = page;
368 dir->u.ext2_i.i_dir_start_lookup = n;
369 return de;
370 }
371
ext2_dotdot(struct inode * dir,struct page ** p)372 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
373 {
374 struct page *page = ext2_get_page(dir, 0, 0);
375 ext2_dirent *de = NULL;
376
377 if (!IS_ERR(page)) {
378 de = ext2_next_entry((ext2_dirent *) page_address(page));
379 *p = page;
380 }
381 return de;
382 }
383
ext2_inode_by_name(struct inode * dir,struct dentry * dentry)384 ino_t ext2_inode_by_name(struct inode * dir, struct dentry *dentry)
385 {
386 ino_t res = 0;
387 struct ext2_dir_entry_2 * de;
388 struct page *page;
389
390 de = ext2_find_entry (dir, dentry, &page);
391 if (de) {
392 res = le32_to_cpu(de->inode);
393 kunmap(page);
394 page_cache_release(page);
395 }
396 return res;
397 }
398
399 /* Releases the page */
ext2_set_link(struct inode * dir,struct ext2_dir_entry_2 * de,struct page * page,struct inode * inode)400 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
401 struct page *page, struct inode *inode)
402 {
403 unsigned from = (char *) de - (char *) page_address(page);
404 unsigned to = from + le16_to_cpu(de->rec_len);
405 int err;
406
407 lock_page(page);
408 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
409 if (err)
410 BUG();
411 de->inode = cpu_to_le32(inode->i_ino);
412 ext2_set_de_type (de, inode);
413 err = ext2_commit_chunk(page, from, to);
414 UnlockPage(page);
415 ext2_put_page(page);
416 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
417 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
418 mark_inode_dirty(dir);
419 }
420
421 /*
422 * Parent is locked.
423 */
ext2_add_link(struct dentry * dentry,struct inode * inode)424 int ext2_add_link (struct dentry *dentry, struct inode *inode)
425 {
426 struct inode *dir = dentry->d_parent->d_inode;
427 const char *name = dentry->d_name.name;
428 int namelen = dentry->d_name.len;
429 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
430 unsigned short rec_len, name_len;
431 struct page *page = NULL;
432 ext2_dirent * de;
433 unsigned long npages = dir_pages(dir);
434 unsigned long n;
435 char *kaddr;
436 unsigned from, to;
437 int err;
438
439 /* We take care of directory expansion in the same loop */
440 for (n = 0; n <= npages; n++) {
441 page = ext2_get_page(dir, n, 0);
442 err = PTR_ERR(page);
443 if (IS_ERR(page))
444 goto out;
445 kaddr = page_address(page);
446 de = (ext2_dirent *)kaddr;
447 kaddr += PAGE_CACHE_SIZE - reclen;
448 while ((char *)de <= kaddr) {
449 err = -EEXIST;
450 if (ext2_match (namelen, name, de))
451 goto out_page;
452 name_len = EXT2_DIR_REC_LEN(de->name_len);
453 rec_len = le16_to_cpu(de->rec_len);
454 if (!de->inode && rec_len >= reclen)
455 goto got_it;
456 if (rec_len >= name_len + reclen)
457 goto got_it;
458 de = (ext2_dirent *) ((char *) de + rec_len);
459 }
460 ext2_put_page(page);
461 }
462 BUG();
463 return -EINVAL;
464
465 got_it:
466 from = (char*)de - (char*)page_address(page);
467 to = from + rec_len;
468 lock_page(page);
469 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
470 if (err)
471 goto out_unlock;
472 if (de->inode) {
473 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
474 de1->rec_len = cpu_to_le16(rec_len - name_len);
475 de->rec_len = cpu_to_le16(name_len);
476 de = de1;
477 }
478 de->name_len = namelen;
479 memcpy (de->name, name, namelen);
480 de->inode = cpu_to_le32(inode->i_ino);
481 ext2_set_de_type (de, inode);
482 err = ext2_commit_chunk(page, from, to);
483 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
484 dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
485 mark_inode_dirty(dir);
486 /* OFFSET_CACHE */
487 out_unlock:
488 UnlockPage(page);
489 out_page:
490 ext2_put_page(page);
491 out:
492 return err;
493 }
494
495 /*
496 * ext2_delete_entry deletes a directory entry by merging it with the
497 * previous entry. Page is up-to-date. Releases the page.
498 */
ext2_delete_entry(struct ext2_dir_entry_2 * dir,struct page * page)499 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
500 {
501 struct address_space *mapping = page->mapping;
502 struct inode *inode = mapping->host;
503 char *kaddr = page_address(page);
504 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
505 unsigned to = ((char*)dir - kaddr) + le16_to_cpu(dir->rec_len);
506 ext2_dirent * pde = NULL;
507 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
508 int err;
509
510 while ((char*)de < (char*)dir) {
511 pde = de;
512 de = ext2_next_entry(de);
513 }
514 if (pde)
515 from = (char*)pde - (char*)page_address(page);
516 lock_page(page);
517 err = mapping->a_ops->prepare_write(NULL, page, from, to);
518 if (err)
519 BUG();
520 if (pde)
521 pde->rec_len = cpu_to_le16(to-from);
522 dir->inode = 0;
523 err = ext2_commit_chunk(page, from, to);
524 UnlockPage(page);
525 ext2_put_page(page);
526 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
527 inode->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
528 mark_inode_dirty(inode);
529 return err;
530 }
531
532 /*
533 * Set the first fragment of directory.
534 */
ext2_make_empty(struct inode * inode,struct inode * parent)535 int ext2_make_empty(struct inode *inode, struct inode *parent)
536 {
537 struct address_space *mapping = inode->i_mapping;
538 struct page *page = grab_cache_page(mapping, 0);
539 unsigned chunk_size = ext2_chunk_size(inode);
540 struct ext2_dir_entry_2 * de;
541 char *base;
542 int err;
543
544 if (!page)
545 return -ENOMEM;
546 err = mapping->a_ops->prepare_write(NULL, page, 0, chunk_size);
547 if (err)
548 goto fail;
549
550 base = page_address(page);
551
552 memset(base, 0, chunk_size);
553 de = (struct ext2_dir_entry_2 *) base;
554 de->name_len = 1;
555 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(1));
556 memcpy (de->name, ".\0\0", 4);
557 de->inode = cpu_to_le32(inode->i_ino);
558 ext2_set_de_type (de, inode);
559
560 de = (struct ext2_dir_entry_2 *) (base + EXT2_DIR_REC_LEN(1));
561 de->name_len = 2;
562 de->rec_len = cpu_to_le16(chunk_size - EXT2_DIR_REC_LEN(1));
563 de->inode = cpu_to_le32(parent->i_ino);
564 memcpy (de->name, "..\0", 4);
565 ext2_set_de_type (de, inode);
566
567 err = ext2_commit_chunk(page, 0, chunk_size);
568 fail:
569 UnlockPage(page);
570 page_cache_release(page);
571 return err;
572 }
573
574 /*
575 * routine to check that the specified directory is empty (for rmdir)
576 */
ext2_empty_dir(struct inode * inode)577 int ext2_empty_dir (struct inode * inode)
578 {
579 struct page *page = NULL;
580 unsigned long i, npages = dir_pages(inode);
581 int dir_has_error = 0;
582
583 for (i = 0; i < npages; i++) {
584 char *kaddr;
585 ext2_dirent * de;
586 page = ext2_get_page(inode, i, dir_has_error);
587
588 if (IS_ERR(page)) {
589 dir_has_error = 1;
590 continue;
591 }
592
593 kaddr = page_address(page);
594 de = (ext2_dirent *)kaddr;
595 kaddr += PAGE_CACHE_SIZE-EXT2_DIR_REC_LEN(1);
596
597 while ((char *)de <= kaddr) {
598 if (de->inode != 0) {
599 /* check for . and .. */
600 if (de->name[0] != '.')
601 goto not_empty;
602 if (de->name_len > 2)
603 goto not_empty;
604 if (de->name_len < 2) {
605 if (de->inode !=
606 cpu_to_le32(inode->i_ino))
607 goto not_empty;
608 } else if (de->name[1] != '.')
609 goto not_empty;
610 }
611 de = ext2_next_entry(de);
612 }
613 ext2_put_page(page);
614 }
615 return 1;
616
617 not_empty:
618 ext2_put_page(page);
619 return 0;
620 }
621
622 struct file_operations ext2_dir_operations = {
623 read: generic_read_dir,
624 readdir: ext2_readdir,
625 ioctl: ext2_ioctl,
626 fsync: ext2_sync_file,
627 };
628