2 * linux/fs/ufs/ufs_dir.c
5 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
6 * Laboratory for Computer Science Research Computing Facility
7 * Rutgers, The State University of New Jersey
9 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
11 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
12 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
13 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
15 * Migration to usage of "page cache" on May 2006 by
16 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
19 #include <linux/time.h>
21 #include <linux/ufs_fs.h>
22 #include <linux/smp_lock.h>
23 #include <linux/sched.h>
29 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
31 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
33 static inline int ufs_match(struct super_block *sb, int len,
34 const char * const name, struct ufs_dir_entry * de)
36 if (len != ufs_get_de_namlen(sb, de))
40 return !memcmp(name, de->d_name, len);
43 static int ufs_commit_chunk(struct page *page, unsigned from, unsigned to)
45 struct inode *dir = page->mapping->host;
48 page->mapping->a_ops->commit_write(NULL, page, from, to);
50 err = write_one_page(page, 1);
56 static inline void ufs_put_page(struct page *page)
59 page_cache_release(page);
62 static inline unsigned long ufs_dir_pages(struct inode *inode)
64 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
67 ino_t ufs_inode_by_name(struct inode *dir, struct dentry *dentry)
70 struct ufs_dir_entry *de;
73 de = ufs_find_entry(dir, dentry, &page);
75 res = fs32_to_cpu(dir->i_sb, de->d_ino);
82 /* Releases the page */
83 void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
84 struct page *page, struct inode *inode)
86 unsigned from = (char *) de - (char *) page_address(page);
87 unsigned to = from + fs16_to_cpu(dir->i_sb, de->d_reclen);
91 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
93 de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
94 ufs_set_de_type(dir->i_sb, de, inode->i_mode);
95 err = ufs_commit_chunk(page, from, to);
97 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
98 mark_inode_dirty(dir);
102 static void ufs_check_page(struct page *page)
104 struct inode *dir = page->mapping->host;
105 struct super_block *sb = dir->i_sb;
106 char *kaddr = page_address(page);
107 unsigned offs, rec_len;
108 unsigned limit = PAGE_CACHE_SIZE;
109 const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
110 struct ufs_dir_entry *p;
113 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
114 limit = dir->i_size & ~PAGE_CACHE_MASK;
115 if (limit & chunk_mask)
120 for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
121 p = (struct ufs_dir_entry *)(kaddr + offs);
122 rec_len = fs16_to_cpu(sb, p->d_reclen);
124 if (rec_len < UFS_DIR_REC_LEN(1))
128 if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
130 if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
132 if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
133 UFS_SB(sb)->s_uspi->s_ncg))
139 SetPageChecked(page);
142 /* Too bad, we had an error */
145 ufs_error(sb, "ufs_check_page",
146 "size of directory #%lu is not a multiple of chunk size",
151 error = "rec_len is smaller than minimal";
154 error = "unaligned directory entry";
157 error = "rec_len is too small for name_len";
160 error = "directory entry across blocks";
163 error = "inode out of bounds";
165 ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
166 "offset=%lu, rec_len=%d, name_len=%d",
167 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
168 rec_len, ufs_get_de_namlen(sb, p));
171 p = (struct ufs_dir_entry *)(kaddr + offs);
172 ufs_error (sb, "ext2_check_page",
173 "entry in directory #%lu spans the page boundary"
175 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs);
177 SetPageChecked(page);
181 static struct page *ufs_get_page(struct inode *dir, unsigned long n)
183 struct address_space *mapping = dir->i_mapping;
184 struct page *page = read_cache_page(mapping, n,
185 (filler_t*)mapping->a_ops->readpage, NULL);
187 wait_on_page_locked(page);
189 if (!PageUptodate(page))
191 if (!PageChecked(page))
192 ufs_check_page(page);
200 return ERR_PTR(-EIO);
204 * Return the offset into page `page_nr' of the last valid
205 * byte in that page, plus one.
208 ufs_last_byte(struct inode *inode, unsigned long page_nr)
210 unsigned last_byte = inode->i_size;
212 last_byte -= page_nr << PAGE_CACHE_SHIFT;
213 if (last_byte > PAGE_CACHE_SIZE)
214 last_byte = PAGE_CACHE_SIZE;
218 static inline struct ufs_dir_entry *
219 ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
221 return (struct ufs_dir_entry *)((char *)p +
222 fs16_to_cpu(sb, p->d_reclen));
225 struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
227 struct page *page = ufs_get_page(dir, 0);
228 struct ufs_dir_entry *de = NULL;
231 de = ufs_next_entry(dir->i_sb,
232 (struct ufs_dir_entry *)page_address(page));
241 * finds an entry in the specified directory with the wanted name. It
242 * returns the page in which the entry was found, and the entry itself
243 * (as a parameter - res_dir). Page is returned mapped and unlocked.
244 * Entry is guaranteed to be valid.
246 struct ufs_dir_entry *ufs_find_entry(struct inode *dir, struct dentry *dentry,
247 struct page **res_page)
249 struct super_block *sb = dir->i_sb;
250 const char *name = dentry->d_name.name;
251 int namelen = dentry->d_name.len;
252 unsigned reclen = UFS_DIR_REC_LEN(namelen);
253 unsigned long start, n;
254 unsigned long npages = ufs_dir_pages(dir);
255 struct page *page = NULL;
256 struct ufs_inode_info *ui = UFS_I(dir);
257 struct ufs_dir_entry *de;
259 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
261 if (npages == 0 || namelen > UFS_MAXNAMLEN)
267 start = ui->i_dir_start_lookup;
274 page = ufs_get_page(dir, n);
276 kaddr = page_address(page);
277 de = (struct ufs_dir_entry *) kaddr;
278 kaddr += ufs_last_byte(dir, n) - reclen;
279 while ((char *) de <= kaddr) {
280 if (de->d_reclen == 0) {
281 ufs_error(dir->i_sb, __FUNCTION__,
282 "zero-length directory entry");
286 if (ufs_match(sb, namelen, name, de))
288 de = ufs_next_entry(sb, de);
294 } while (n != start);
300 ui->i_dir_start_lookup = n;
307 int ufs_add_link(struct dentry *dentry, struct inode *inode)
309 struct inode *dir = dentry->d_parent->d_inode;
310 const char *name = dentry->d_name.name;
311 int namelen = dentry->d_name.len;
312 struct super_block *sb = dir->i_sb;
313 unsigned reclen = UFS_DIR_REC_LEN(namelen);
314 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
315 unsigned short rec_len, name_len;
316 struct page *page = NULL;
317 struct ufs_dir_entry *de;
318 unsigned long npages = ufs_dir_pages(dir);
324 UFSD("ENTER, name %s, namelen %u\n", name, namelen);
327 * We take care of directory expansion in the same loop.
328 * This code plays outside i_size, so it locks the page
329 * to protect that region.
331 for (n = 0; n <= npages; n++) {
334 page = ufs_get_page(dir, n);
339 kaddr = page_address(page);
340 dir_end = kaddr + ufs_last_byte(dir, n);
341 de = (struct ufs_dir_entry *)kaddr;
342 kaddr += PAGE_CACHE_SIZE - reclen;
343 while ((char *)de <= kaddr) {
344 if ((char *)de == dir_end) {
347 rec_len = chunk_size;
348 de->d_reclen = cpu_to_fs16(sb, chunk_size);
352 if (de->d_reclen == 0) {
353 ufs_error(dir->i_sb, __FUNCTION__,
354 "zero-length directory entry");
359 if (ufs_match(sb, namelen, name, de))
361 name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
362 rec_len = fs16_to_cpu(sb, de->d_reclen);
363 if (!de->d_ino && rec_len >= reclen)
365 if (rec_len >= name_len + reclen)
367 de = (struct ufs_dir_entry *) ((char *) de + rec_len);
376 from = (char*)de - (char*)page_address(page);
378 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
382 struct ufs_dir_entry *de1 =
383 (struct ufs_dir_entry *) ((char *) de + name_len);
384 de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
385 de->d_reclen = cpu_to_fs16(sb, name_len);
390 ufs_set_de_namlen(sb, de, namelen);
391 memcpy(de->d_name, name, namelen + 1);
392 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
393 ufs_set_de_type(sb, de, inode->i_mode);
395 err = ufs_commit_chunk(page, from, to);
396 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
398 mark_inode_dirty(dir);
409 static inline unsigned
410 ufs_validate_entry(struct super_block *sb, char *base,
411 unsigned offset, unsigned mask)
413 struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
414 struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
415 while ((char*)p < (char*)de) {
416 if (p->d_reclen == 0)
418 p = ufs_next_entry(sb, p);
420 return (char *)p - base;
425 * This is blatantly stolen from ext2fs
428 ufs_readdir(struct file *filp, void *dirent, filldir_t filldir)
430 loff_t pos = filp->f_pos;
431 struct inode *inode = filp->f_path.dentry->d_inode;
432 struct super_block *sb = inode->i_sb;
433 unsigned int offset = pos & ~PAGE_CACHE_MASK;
434 unsigned long n = pos >> PAGE_CACHE_SHIFT;
435 unsigned long npages = ufs_dir_pages(inode);
436 unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
437 int need_revalidate = filp->f_version != inode->i_version;
438 unsigned flags = UFS_SB(sb)->s_flags;
442 if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
445 for ( ; n < npages; n++, offset = 0) {
447 struct ufs_dir_entry *de;
449 struct page *page = ufs_get_page(inode, n);
452 ufs_error(sb, __FUNCTION__,
455 filp->f_pos += PAGE_CACHE_SIZE - offset;
458 kaddr = page_address(page);
459 if (unlikely(need_revalidate)) {
461 offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
462 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
464 filp->f_version = inode->i_version;
467 de = (struct ufs_dir_entry *)(kaddr+offset);
468 limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
469 for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
470 if (de->d_reclen == 0) {
471 ufs_error(sb, __FUNCTION__,
472 "zero-length directory entry");
478 unsigned char d_type = DT_UNKNOWN;
480 offset = (char *)de - kaddr;
482 UFSD("filldir(%s,%u)\n", de->d_name,
483 fs32_to_cpu(sb, de->d_ino));
484 UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
486 if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
487 d_type = de->d_u.d_44.d_type;
489 over = filldir(dirent, de->d_name,
490 ufs_get_de_namlen(sb, de),
491 (n<<PAGE_CACHE_SHIFT) | offset,
492 fs32_to_cpu(sb, de->d_ino), d_type);
498 filp->f_pos += fs16_to_cpu(sb, de->d_reclen);
507 * ufs_delete_entry deletes a directory entry by merging it with the
510 int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
513 struct super_block *sb = inode->i_sb;
514 struct address_space *mapping = page->mapping;
515 char *kaddr = page_address(page);
516 unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
517 unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
518 struct ufs_dir_entry *pde = NULL;
519 struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
524 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
525 fs32_to_cpu(sb, de->d_ino),
526 fs16_to_cpu(sb, de->d_reclen),
527 ufs_get_de_namlen(sb, de), de->d_name);
529 while ((char*)de < (char*)dir) {
530 if (de->d_reclen == 0) {
531 ufs_error(inode->i_sb, __FUNCTION__,
532 "zero-length directory entry");
537 de = ufs_next_entry(sb, de);
540 from = (char*)pde - (char*)page_address(page);
542 err = mapping->a_ops->prepare_write(NULL, page, from, to);
545 pde->d_reclen = cpu_to_fs16(sb, to-from);
547 err = ufs_commit_chunk(page, from, to);
548 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
549 mark_inode_dirty(inode);
556 int ufs_make_empty(struct inode * inode, struct inode *dir)
558 struct super_block * sb = dir->i_sb;
559 struct address_space *mapping = inode->i_mapping;
560 struct page *page = grab_cache_page(mapping, 0);
561 const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
562 struct ufs_dir_entry * de;
569 err = mapping->a_ops->prepare_write(NULL, page, 0, chunk_size);
576 base = (char*)page_address(page);
577 memset(base, 0, PAGE_CACHE_SIZE);
579 de = (struct ufs_dir_entry *) base;
581 de->d_ino = cpu_to_fs32(sb, inode->i_ino);
582 ufs_set_de_type(sb, de, inode->i_mode);
583 ufs_set_de_namlen(sb, de, 1);
584 de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
585 strcpy (de->d_name, ".");
586 de = (struct ufs_dir_entry *)
587 ((char *)de + fs16_to_cpu(sb, de->d_reclen));
588 de->d_ino = cpu_to_fs32(sb, dir->i_ino);
589 ufs_set_de_type(sb, de, dir->i_mode);
590 de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
591 ufs_set_de_namlen(sb, de, 2);
592 strcpy (de->d_name, "..");
594 err = ufs_commit_chunk(page, 0, chunk_size);
597 page_cache_release(page);
602 * routine to check that the specified directory is empty (for rmdir)
604 int ufs_empty_dir(struct inode * inode)
606 struct super_block *sb = inode->i_sb;
607 struct page *page = NULL;
608 unsigned long i, npages = ufs_dir_pages(inode);
610 for (i = 0; i < npages; i++) {
612 struct ufs_dir_entry *de;
613 page = ufs_get_page(inode, i);
618 kaddr = page_address(page);
619 de = (struct ufs_dir_entry *)kaddr;
620 kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
622 while ((char *)de <= kaddr) {
623 if (de->d_reclen == 0) {
624 ufs_error(inode->i_sb, __FUNCTION__,
625 "zero-length directory entry: "
626 "kaddr=%p, de=%p\n", kaddr, de);
630 u16 namelen=ufs_get_de_namlen(sb, de);
631 /* check for . and .. */
632 if (de->d_name[0] != '.')
638 fs32_to_cpu(sb, de->d_ino))
640 } else if (de->d_name[1] != '.')
643 de = ufs_next_entry(sb, de);
654 const struct file_operations ufs_dir_operations = {
655 .read = generic_read_dir,
656 .readdir = ufs_readdir,