1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
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)
11 * linux/fs/minix/dir.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * ext2 directory handling functions
18 * Big-endian to little-endian byte-swapping/bitmaps by
19 * David S. Miller (davem@caip.rutgers.edu), 1995
21 * All code that works with directory layout had been switched to pagecache
24 * Adapted for Lustre Light
25 * Copyright (C) 2002, Cluster File Systems, Inc.
30 #include <linux/ext2_fs.h>
31 #include <linux/pagemap.h>
33 #include <linux/locks.h>
34 #include <asm/uaccess.h>
36 #define DEBUG_SUBSYSTEM S_LLITE
38 #include <linux/obd_support.h>
39 #include <linux/obd_class.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_lite.h>
44 #include <linux/lustre_dlm.h>
46 typedef struct ext2_dir_entry_2 ext2_dirent;
48 #define PageChecked(page) test_bit(PG_checked, &(page)->flags)
49 #define SetPageChecked(page) set_bit(PG_checked, &(page)->flags)
52 static int ll_dir_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
57 /* returns the page unlocked, but with a reference */
58 static int ll_dir_readpage(struct file *file, struct page *page)
60 struct inode *inode = page->mapping->host;
61 struct ll_sb_info *sbi = ll_i2sbi(inode);
65 struct ptlrpc_request *request = NULL;
66 struct lustre_handle lockh;
67 struct lookup_intent it = {IT_READDIR };
71 if ((inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_SHIFT <= page->index){
72 memset(kmap(page), 0, PAGE_CACHE_SIZE);
78 rc = ll_lock(inode, NULL, &it, &lockh);
80 CERROR("lock enqueue: err: %d\n", rc);
81 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
83 if (Page_Uptodate(page)) {
84 CERROR("Explain this please?\n");
89 offset = page->index << PAGE_SHIFT;
91 rc = mdc_readpage(&sbi->ll_mdc_conn, inode->i_ino,
92 S_IFDIR, offset, buf, &request);
94 ptlrpc_free_req(request);
99 SetPageUptodate(page);
102 rc = ll_unlock(LCK_PR, &lockh);
104 CERROR("ll_unlock: err: %d\n", rc);
106 } /* ll_dir_readpage */
108 struct address_space_operations ll_dir_aops = {
109 readpage: ll_dir_readpage,
110 prepare_write: ll_dir_prepare_write
113 int waitfor_one_page(struct page *page)
116 struct buffer_head *bh, *head = page->buffers;
121 if (buffer_req(bh) && !buffer_uptodate(bh))
123 } while ((bh = bh->b_this_page) != head);
128 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
129 * more robust, but we have what we have
131 static inline unsigned ext2_chunk_size(struct inode *inode)
133 return inode->i_sb->s_blocksize;
136 static inline void ext2_put_page(struct page *page)
139 page_cache_release(page);
142 static inline unsigned long dir_pages(struct inode *inode)
144 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
147 extern void set_page_clean(struct page *page);
149 static int ext2_commit_chunk(struct page *page, unsigned from, unsigned to)
151 struct inode *dir = page->mapping->host;
152 loff_t new_size = (page->index << PAGE_CACHE_SHIFT) + to;
155 dir->i_version = ++event;
156 if (new_size > dir->i_size)
157 dir->i_size = new_size;
158 SetPageUptodate(page);
159 set_page_clean(page);
161 //page->mapping->a_ops->commit_write(NULL, page, from, to);
163 // err = waitfor_one_page(page);
167 static void ext2_check_page(struct page *page)
169 struct inode *dir = page->mapping->host;
170 unsigned chunk_size = ext2_chunk_size(dir);
171 char *kaddr = page_address(page);
172 // u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
173 unsigned offs, rec_len;
174 unsigned limit = PAGE_CACHE_SIZE;
178 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
179 limit = dir->i_size & ~PAGE_CACHE_MASK;
180 if (limit & (chunk_size - 1)) {
181 CERROR("limit %d dir size %lld index %ld\n",
182 limit, dir->i_size, page->index);
185 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
186 ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
187 p->rec_len = cpu_to_le16(chunk_size);
192 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
193 p = (ext2_dirent *)(kaddr + offs);
194 rec_len = le16_to_cpu(p->rec_len);
196 if (rec_len < EXT2_DIR_REC_LEN(1))
200 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
202 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
204 // if (le32_to_cpu(p->inode) > max_inumber)
210 SetPageChecked(page);
213 /* Too bad, we had an error */
216 CERROR("ext2_check_page"
217 "size of directory #%lu is not a multiple of chunk size\n",
222 error = "rec_len is smaller than minimal";
225 error = "unaligned directory entry";
228 error = "rec_len is too small for name_len";
231 error = "directory entry across blocks";
234 // error = "inode out of bounds";
236 CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
237 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
238 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
239 (unsigned long) le32_to_cpu(p->inode),
240 rec_len, p->name_len);
243 p = (ext2_dirent *)(kaddr + offs);
244 CERROR("ext2_check_page"
245 "entry in directory #%lu spans the page boundary"
246 "offset=%lu, inode=%lu",
247 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
248 (unsigned long) le32_to_cpu(p->inode));
250 SetPageChecked(page);
255 static struct page * ext2_get_page(struct inode *dir, unsigned long n)
257 struct address_space *mapping = dir->i_mapping;
258 struct page *page = read_cache_page(mapping, n,
259 (filler_t*)mapping->a_ops->readpage, NULL);
263 if (!Page_Uptodate(page))
265 if (!PageChecked(page))
266 ext2_check_page(page);
274 return ERR_PTR(-EIO);
278 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
280 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
282 static inline int ext2_match (int len, const char * const name,
283 struct ext2_dir_entry_2 * de)
285 if (len != de->name_len)
289 return !memcmp(name, de->name, len);
293 * p is at least 6 bytes before the end of page
295 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
297 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
300 static inline unsigned
301 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
303 ext2_dirent *de = (ext2_dirent*)(base + offset);
304 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
305 while ((char*)p < (char*)de)
306 p = ext2_next_entry(p);
307 return (char *)p - base;
310 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
311 [EXT2_FT_UNKNOWN] DT_UNKNOWN,
312 [EXT2_FT_REG_FILE] DT_REG,
313 [EXT2_FT_DIR] DT_DIR,
314 [EXT2_FT_CHRDEV] DT_CHR,
315 [EXT2_FT_BLKDEV] DT_BLK,
316 [EXT2_FT_FIFO] DT_FIFO,
317 [EXT2_FT_SOCK] DT_SOCK,
318 [EXT2_FT_SYMLINK] DT_LNK,
321 static unsigned int ll_dt2fmt[DT_WHT + 1] = {
323 [EXT2_FT_REG_FILE] S_IFREG,
324 [EXT2_FT_DIR] S_IFDIR,
325 [EXT2_FT_CHRDEV] S_IFCHR,
326 [EXT2_FT_BLKDEV] S_IFBLK,
327 [EXT2_FT_FIFO] S_IFIFO,
328 [EXT2_FT_SOCK] S_IFSOCK,
329 [EXT2_FT_SYMLINK] S_IFLNK
333 static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
334 [S_IFREG >> S_SHIFT] EXT2_FT_REG_FILE,
335 [S_IFDIR >> S_SHIFT] EXT2_FT_DIR,
336 [S_IFCHR >> S_SHIFT] EXT2_FT_CHRDEV,
337 [S_IFBLK >> S_SHIFT] EXT2_FT_BLKDEV,
338 [S_IFIFO >> S_SHIFT] EXT2_FT_FIFO,
339 [S_IFSOCK >> S_SHIFT] EXT2_FT_SOCK,
340 [S_IFLNK >> S_SHIFT] EXT2_FT_SYMLINK,
343 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
345 mode_t mode = inode->i_mode;
346 de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
350 ll_readdir (struct file * filp, void * dirent, filldir_t filldir)
352 loff_t pos = filp->f_pos;
353 struct inode *inode = filp->f_dentry->d_inode;
354 // XXX struct super_block *sb = inode->i_sb;
355 unsigned offset = pos & ~PAGE_CACHE_MASK;
356 unsigned long n = pos >> PAGE_CACHE_SHIFT;
357 unsigned long npages = dir_pages(inode);
358 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
359 unsigned char *types = NULL;
360 int need_revalidate = (filp->f_version != inode->i_version);
362 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
365 types = ext2_filetype_table;
367 for ( ; n < npages; n++, offset = 0) {
370 struct page *page = ext2_get_page(inode, n);
374 kaddr = page_address(page);
375 if (need_revalidate) {
376 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
379 de = (ext2_dirent *)(kaddr+offset);
380 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
381 for ( ;(char*)de <= limit; de = ext2_next_entry(de))
384 unsigned char d_type = DT_UNKNOWN;
386 if (types && de->file_type < EXT2_FT_MAX)
387 d_type = types[de->file_type];
389 offset = (char *)de - kaddr;
390 over = filldir(dirent, de->name, de->name_len,
391 (n<<PAGE_CACHE_SHIFT) | offset,
392 le32_to_cpu(de->inode), d_type);
402 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
403 filp->f_version = inode->i_version;
411 * finds an entry in the specified directory with the wanted name. It
412 * returns the page in which the entry was found, and the entry itself
413 * (as a parameter - res_dir). Page is returned mapped and unlocked.
414 * Entry is guaranteed to be valid.
416 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
417 struct dentry *dentry, struct page ** res_page)
419 const char *name = dentry->d_name.name;
420 int namelen = dentry->d_name.len;
421 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
422 unsigned long start, n;
423 unsigned long npages = dir_pages(dir);
424 struct page *page = NULL;
430 // start = dir->u.ext2_i.i_dir_start_lookup;
437 page = ext2_get_page(dir, n);
439 kaddr = page_address(page);
440 de = (ext2_dirent *) kaddr;
441 kaddr += PAGE_CACHE_SIZE - reclen;
442 while ((char *) de <= kaddr) {
443 if (ext2_match (namelen, name, de))
445 de = ext2_next_entry(de);
451 } while (n != start);
456 // dir->u.ext2_i.i_dir_start_lookup = n;
460 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
462 struct page *page = ext2_get_page(dir, 0);
463 ext2_dirent *de = NULL;
466 de = ext2_next_entry((ext2_dirent *) page_address(page));
472 ino_t ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *type)
475 struct ext2_dir_entry_2 * de;
478 de = ext2_find_entry (dir, dentry, &page);
480 res = le32_to_cpu(de->inode);
481 *type = ll_dt2fmt[de->file_type];
483 page_cache_release(page);
488 /* Releases the page */
489 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
490 struct page *page, struct inode *inode)
492 unsigned from = (char *) de - (char *) page_address(page);
493 unsigned to = from + le16_to_cpu(de->rec_len);
497 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
500 de->inode = cpu_to_le32(inode->i_ino);
501 ext2_set_de_type (de, inode);
502 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
503 err = ext2_commit_chunk(page, from, to);
511 int ll_add_link (struct dentry *dentry, struct inode *inode)
513 struct inode *dir = dentry->d_parent->d_inode;
514 const char *name = dentry->d_name.name;
515 int namelen = dentry->d_name.len;
516 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
517 unsigned short rec_len, name_len;
518 struct page *page = NULL;
520 unsigned long npages = dir_pages(dir);
526 /* We take care of directory expansion in the same loop */
527 for (n = 0; n <= npages; n++) {
528 page = ext2_get_page(dir, n);
532 kaddr = page_address(page);
533 de = (ext2_dirent *)kaddr;
534 kaddr += PAGE_CACHE_SIZE - reclen;
535 while ((char *)de <= kaddr) {
537 if (ext2_match (namelen, name, de))
539 name_len = EXT2_DIR_REC_LEN(de->name_len);
540 rec_len = le16_to_cpu(de->rec_len);
541 if ( n==npages && rec_len == 0) {
542 CERROR("Fatal dir behaviour\n");
545 if (!de->inode && rec_len >= reclen)
547 if (rec_len >= name_len + reclen)
549 de = (ext2_dirent *) ((char *) de + rec_len);
557 from = (char*)de - (char*)page_address(page);
560 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
564 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
565 de1->rec_len = cpu_to_le16(rec_len - name_len);
566 de->rec_len = cpu_to_le16(name_len);
569 de->name_len = namelen;
570 memcpy (de->name, name, namelen);
571 de->inode = cpu_to_le32(inode->i_ino);
572 ext2_set_de_type (de, inode);
573 CDEBUG(D_INODE, "type set to %o\n", de->file_type);
574 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
575 err = ext2_commit_chunk(page, from, to);
577 // change_inode happens with the commit_chunk
578 /* XXX OFFSET_CACHE */
589 * ext2_delete_entry deletes a directory entry by merging it with the
590 * previous entry. Page is up-to-date. Releases the page.
592 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
594 struct address_space *mapping = page->mapping;
595 struct inode *inode = mapping->host;
596 char *kaddr = page_address(page);
597 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
598 unsigned to = ((char*)dir - kaddr) + le16_to_cpu(dir->rec_len);
599 ext2_dirent * pde = NULL;
600 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
603 while ((char*)de < (char*)dir) {
605 de = ext2_next_entry(de);
608 from = (char*)pde - (char*)page_address(page);
610 err = mapping->a_ops->prepare_write(NULL, page, from, to);
614 pde->rec_len = cpu_to_le16(to-from);
616 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
617 err = ext2_commit_chunk(page, from, to);
624 * Set the first fragment of directory.
626 int ext2_make_empty(struct inode *inode, struct inode *parent)
628 struct address_space *mapping = inode->i_mapping;
629 struct page *page = grab_cache_page(mapping, 0);
630 unsigned chunk_size = ext2_chunk_size(inode);
631 struct ext2_dir_entry_2 * de;
642 err = mapping->a_ops->prepare_write(NULL, page, 0, chunk_size);
646 de = (struct ext2_dir_entry_2 *) base;
648 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(1));
649 memcpy (de->name, ".\0\0", 4);
650 de->inode = cpu_to_le32(inode->i_ino);
651 ext2_set_de_type (de, inode);
653 de = (struct ext2_dir_entry_2 *) (base + EXT2_DIR_REC_LEN(1));
655 de->rec_len = cpu_to_le16(chunk_size - EXT2_DIR_REC_LEN(1));
656 de->inode = cpu_to_le32(parent->i_ino);
657 memcpy (de->name, "..\0", 4);
658 ext2_set_de_type (de, inode);
660 err = ext2_commit_chunk(page, 0, chunk_size);
664 page_cache_release(page);
670 * routine to check that the specified directory is empty (for rmdir)
672 int ext2_empty_dir (struct inode * inode)
674 struct page *page = NULL;
675 unsigned long i, npages = dir_pages(inode);
677 for (i = 0; i < npages; i++) {
680 page = ext2_get_page(inode, i);
685 kaddr = page_address(page);
686 de = (ext2_dirent *)kaddr;
687 kaddr += PAGE_CACHE_SIZE-EXT2_DIR_REC_LEN(1);
689 while ((char *)de <= kaddr) {
690 if (de->inode != 0) {
691 /* check for . and .. */
692 if (de->name[0] != '.')
694 if (de->name_len > 2)
696 if (de->name_len < 2) {
698 cpu_to_le32(inode->i_ino))
700 } else if (de->name[1] != '.')
703 de = ext2_next_entry(de);
714 struct file_operations ll_dir_operations = {
715 read: generic_read_dir,