2 * Copyright (C) 2005, 2006
3 * Avishay Traeger (avishay@gmail.com) (avishay@il.ibm.com)
4 * Copyright (C) 2005, 2006
5 * International Business Machines
6 * Copyright (C) 2008, 2009
7 * Boaz Harrosh <bharrosh@panasas.com>
9 * Copyrights for code taken from ext2:
10 * Copyright (C) 1992, 1993, 1994, 1995
11 * Remy Card (card@masi.ibp.fr)
12 * Laboratoire MASI - Institut Blaise Pascal
13 * Universite Pierre et Marie Curie (Paris VI)
15 * linux/fs/minix/inode.c
16 * Copyright (C) 1991, 1992 Linus Torvalds
18 * This file is part of exofs.
20 * exofs is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation. Since it is based on ext2, and the only
23 * valid version of GPL for the Linux kernel is version 2, the only valid
24 * version of GPL for exofs is version 2.
26 * exofs is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with exofs; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 static inline unsigned exofs_chunk_size(struct inode *inode)
40 return inode->i_sb->s_blocksize;
43 static inline void exofs_put_page(struct page *page)
46 page_cache_release(page);
49 /* Accesses dir's inode->i_size must be called under inode lock */
50 static inline unsigned long dir_pages(struct inode *inode)
52 return (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
55 static unsigned exofs_last_byte(struct inode *inode, unsigned long page_nr)
57 loff_t last_byte = inode->i_size;
59 last_byte -= page_nr << PAGE_CACHE_SHIFT;
60 if (last_byte > PAGE_CACHE_SIZE)
61 last_byte = PAGE_CACHE_SIZE;
65 static int exofs_commit_chunk(struct page *page, loff_t pos, unsigned len)
67 struct address_space *mapping = page->mapping;
68 struct inode *dir = mapping->host;
73 if (!PageUptodate(page))
74 SetPageUptodate(page);
76 if (pos+len > dir->i_size) {
77 i_size_write(dir, pos+len);
78 mark_inode_dirty(dir);
83 err = write_one_page(page, 1);
90 static void exofs_check_page(struct page *page)
92 struct inode *dir = page->mapping->host;
93 unsigned chunk_size = exofs_chunk_size(dir);
94 char *kaddr = page_address(page);
95 unsigned offs, rec_len;
96 unsigned limit = PAGE_CACHE_SIZE;
97 struct exofs_dir_entry *p;
100 /* if the page is the last one in the directory */
101 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
102 limit = dir->i_size & ~PAGE_CACHE_MASK;
103 if (limit & (chunk_size - 1))
108 for (offs = 0; offs <= limit - EXOFS_DIR_REC_LEN(1); offs += rec_len) {
109 p = (struct exofs_dir_entry *)(kaddr + offs);
110 rec_len = le16_to_cpu(p->rec_len);
112 if (rec_len < EXOFS_DIR_REC_LEN(1))
116 if (rec_len < EXOFS_DIR_REC_LEN(p->name_len))
118 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
124 SetPageChecked(page);
128 EXOFS_ERR("ERROR [exofs_check_page]: "
129 "size of directory #%lu is not a multiple of chunk size",
134 error = "rec_len is smaller than minimal";
137 error = "unaligned directory entry";
140 error = "rec_len is too small for name_len";
143 error = "directory entry across blocks";
147 "ERROR [exofs_check_page]: bad entry in directory #%lu: %s - "
148 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d",
149 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
150 _LLU(le64_to_cpu(p->inode_no)),
151 rec_len, p->name_len);
154 p = (struct exofs_dir_entry *)(kaddr + offs);
155 EXOFS_ERR("ERROR [exofs_check_page]: "
156 "entry in directory #%lu spans the page boundary"
157 "offset=%lu, inode=%llu",
158 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
159 _LLU(le64_to_cpu(p->inode_no)));
161 SetPageChecked(page);
165 static struct page *exofs_get_page(struct inode *dir, unsigned long n)
167 struct address_space *mapping = dir->i_mapping;
168 struct page *page = read_mapping_page(mapping, n, NULL);
172 if (!PageChecked(page))
173 exofs_check_page(page);
180 exofs_put_page(page);
181 return ERR_PTR(-EIO);
184 static inline int exofs_match(int len, const unsigned char *name,
185 struct exofs_dir_entry *de)
187 if (len != de->name_len)
191 return !memcmp(name, de->name, len);
195 struct exofs_dir_entry *exofs_next_entry(struct exofs_dir_entry *p)
197 return (struct exofs_dir_entry *)((char *)p + le16_to_cpu(p->rec_len));
200 static inline unsigned
201 exofs_validate_entry(char *base, unsigned offset, unsigned mask)
203 struct exofs_dir_entry *de = (struct exofs_dir_entry *)(base + offset);
204 struct exofs_dir_entry *p =
205 (struct exofs_dir_entry *)(base + (offset&mask));
206 while ((char *)p < (char *)de) {
209 p = exofs_next_entry(p);
211 return (char *)p - base;
214 static unsigned char exofs_filetype_table[EXOFS_FT_MAX] = {
215 [EXOFS_FT_UNKNOWN] = DT_UNKNOWN,
216 [EXOFS_FT_REG_FILE] = DT_REG,
217 [EXOFS_FT_DIR] = DT_DIR,
218 [EXOFS_FT_CHRDEV] = DT_CHR,
219 [EXOFS_FT_BLKDEV] = DT_BLK,
220 [EXOFS_FT_FIFO] = DT_FIFO,
221 [EXOFS_FT_SOCK] = DT_SOCK,
222 [EXOFS_FT_SYMLINK] = DT_LNK,
226 static unsigned char exofs_type_by_mode[S_IFMT >> S_SHIFT] = {
227 [S_IFREG >> S_SHIFT] = EXOFS_FT_REG_FILE,
228 [S_IFDIR >> S_SHIFT] = EXOFS_FT_DIR,
229 [S_IFCHR >> S_SHIFT] = EXOFS_FT_CHRDEV,
230 [S_IFBLK >> S_SHIFT] = EXOFS_FT_BLKDEV,
231 [S_IFIFO >> S_SHIFT] = EXOFS_FT_FIFO,
232 [S_IFSOCK >> S_SHIFT] = EXOFS_FT_SOCK,
233 [S_IFLNK >> S_SHIFT] = EXOFS_FT_SYMLINK,
237 void exofs_set_de_type(struct exofs_dir_entry *de, struct inode *inode)
239 mode_t mode = inode->i_mode;
240 de->file_type = exofs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
244 exofs_readdir(struct file *filp, void *dirent, filldir_t filldir)
246 loff_t pos = filp->f_pos;
247 struct inode *inode = filp->f_path.dentry->d_inode;
248 unsigned int offset = pos & ~PAGE_CACHE_MASK;
249 unsigned long n = pos >> PAGE_CACHE_SHIFT;
250 unsigned long npages = dir_pages(inode);
251 unsigned chunk_mask = ~(exofs_chunk_size(inode)-1);
252 unsigned char *types = NULL;
253 int need_revalidate = (filp->f_version != inode->i_version);
255 if (pos > inode->i_size - EXOFS_DIR_REC_LEN(1))
258 types = exofs_filetype_table;
260 for ( ; n < npages; n++, offset = 0) {
262 struct exofs_dir_entry *de;
263 struct page *page = exofs_get_page(inode, n);
269 filp->f_pos += PAGE_CACHE_SIZE - offset;
270 return PTR_ERR(page);
272 kaddr = page_address(page);
273 if (unlikely(need_revalidate)) {
275 offset = exofs_validate_entry(kaddr, offset,
277 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
279 filp->f_version = inode->i_version;
282 de = (struct exofs_dir_entry *)(kaddr + offset);
283 limit = kaddr + exofs_last_byte(inode, n) -
284 EXOFS_DIR_REC_LEN(1);
285 for (; (char *)de <= limit; de = exofs_next_entry(de)) {
286 if (de->rec_len == 0) {
288 "zero-length directory entry");
289 exofs_put_page(page);
294 unsigned char d_type = DT_UNKNOWN;
296 if (types && de->file_type < EXOFS_FT_MAX)
297 d_type = types[de->file_type];
299 offset = (char *)de - kaddr;
300 over = filldir(dirent, de->name, de->name_len,
301 (n<<PAGE_CACHE_SHIFT) | offset,
302 le64_to_cpu(de->inode_no),
305 exofs_put_page(page);
309 filp->f_pos += le16_to_cpu(de->rec_len);
311 exofs_put_page(page);
317 struct exofs_dir_entry *exofs_find_entry(struct inode *dir,
318 struct dentry *dentry, struct page **res_page)
320 const unsigned char *name = dentry->d_name.name;
321 int namelen = dentry->d_name.len;
322 unsigned reclen = EXOFS_DIR_REC_LEN(namelen);
323 unsigned long start, n;
324 unsigned long npages = dir_pages(dir);
325 struct page *page = NULL;
326 struct exofs_i_info *oi = exofs_i(dir);
327 struct exofs_dir_entry *de;
334 start = oi->i_dir_start_lookup;
340 page = exofs_get_page(dir, n);
342 kaddr = page_address(page);
343 de = (struct exofs_dir_entry *) kaddr;
344 kaddr += exofs_last_byte(dir, n) - reclen;
345 while ((char *) de <= kaddr) {
346 if (de->rec_len == 0) {
348 "ERROR: exofs_find_entry: "
349 "zero-length directory entry");
350 exofs_put_page(page);
353 if (exofs_match(namelen, name, de))
355 de = exofs_next_entry(de);
357 exofs_put_page(page);
361 } while (n != start);
367 oi->i_dir_start_lookup = n;
371 struct exofs_dir_entry *exofs_dotdot(struct inode *dir, struct page **p)
373 struct page *page = exofs_get_page(dir, 0);
374 struct exofs_dir_entry *de = NULL;
377 de = exofs_next_entry(
378 (struct exofs_dir_entry *)page_address(page));
384 ino_t exofs_parent_ino(struct dentry *child)
387 struct exofs_dir_entry *de;
390 de = exofs_dotdot(child->d_inode, &page);
394 ino = le64_to_cpu(de->inode_no);
395 exofs_put_page(page);
399 ino_t exofs_inode_by_name(struct inode *dir, struct dentry *dentry)
402 struct exofs_dir_entry *de;
405 de = exofs_find_entry(dir, dentry, &page);
407 res = le64_to_cpu(de->inode_no);
408 exofs_put_page(page);
413 int exofs_set_link(struct inode *dir, struct exofs_dir_entry *de,
414 struct page *page, struct inode *inode)
416 loff_t pos = page_offset(page) +
417 (char *) de - (char *) page_address(page);
418 unsigned len = le16_to_cpu(de->rec_len);
422 err = exofs_write_begin(NULL, page->mapping, pos, len,
423 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
425 EXOFS_ERR("exofs_set_link: exofs_write_begin FAILD => %d\n",
428 de->inode_no = cpu_to_le64(inode->i_ino);
429 exofs_set_de_type(de, inode);
431 err = exofs_commit_chunk(page, pos, len);
432 exofs_put_page(page);
433 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
434 mark_inode_dirty(dir);
438 int exofs_add_link(struct dentry *dentry, struct inode *inode)
440 struct inode *dir = dentry->d_parent->d_inode;
441 const unsigned char *name = dentry->d_name.name;
442 int namelen = dentry->d_name.len;
443 unsigned chunk_size = exofs_chunk_size(dir);
444 unsigned reclen = EXOFS_DIR_REC_LEN(namelen);
445 unsigned short rec_len, name_len;
446 struct page *page = NULL;
447 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
448 struct exofs_dir_entry *de;
449 unsigned long npages = dir_pages(dir);
455 for (n = 0; n <= npages; n++) {
458 page = exofs_get_page(dir, n);
463 kaddr = page_address(page);
464 dir_end = kaddr + exofs_last_byte(dir, n);
465 de = (struct exofs_dir_entry *)kaddr;
466 kaddr += PAGE_CACHE_SIZE - reclen;
467 while ((char *)de <= kaddr) {
468 if ((char *)de == dir_end) {
470 rec_len = chunk_size;
471 de->rec_len = cpu_to_le16(chunk_size);
475 if (de->rec_len == 0) {
476 EXOFS_ERR("ERROR: exofs_add_link: "
477 "zero-length directory entry");
482 if (exofs_match(namelen, name, de))
484 name_len = EXOFS_DIR_REC_LEN(de->name_len);
485 rec_len = le16_to_cpu(de->rec_len);
486 if (!de->inode_no && rec_len >= reclen)
488 if (rec_len >= name_len + reclen)
490 de = (struct exofs_dir_entry *) ((char *) de + rec_len);
493 exofs_put_page(page);
496 EXOFS_ERR("exofs_add_link: BAD dentry=%p or inode=%p", dentry, inode);
500 pos = page_offset(page) +
501 (char *)de - (char *)page_address(page);
502 err = exofs_write_begin(NULL, page->mapping, pos, rec_len, 0,
507 struct exofs_dir_entry *de1 =
508 (struct exofs_dir_entry *)((char *)de + name_len);
509 de1->rec_len = cpu_to_le16(rec_len - name_len);
510 de->rec_len = cpu_to_le16(name_len);
513 de->name_len = namelen;
514 memcpy(de->name, name, namelen);
515 de->inode_no = cpu_to_le64(inode->i_ino);
516 exofs_set_de_type(de, inode);
517 err = exofs_commit_chunk(page, pos, rec_len);
518 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
519 mark_inode_dirty(dir);
523 exofs_put_page(page);
531 int exofs_delete_entry(struct exofs_dir_entry *dir, struct page *page)
533 struct address_space *mapping = page->mapping;
534 struct inode *inode = mapping->host;
535 struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
536 char *kaddr = page_address(page);
537 unsigned from = ((char *)dir - kaddr) & ~(exofs_chunk_size(inode)-1);
538 unsigned to = ((char *)dir - kaddr) + le16_to_cpu(dir->rec_len);
540 struct exofs_dir_entry *pde = NULL;
541 struct exofs_dir_entry *de = (struct exofs_dir_entry *) (kaddr + from);
545 if (de->rec_len == 0) {
546 EXOFS_ERR("ERROR: exofs_delete_entry:"
547 "zero-length directory entry");
552 de = exofs_next_entry(de);
555 from = (char *)pde - (char *)page_address(page);
556 pos = page_offset(page) + from;
558 err = exofs_write_begin(NULL, page->mapping, pos, to - from, 0,
561 EXOFS_ERR("exofs_delete_entry: exofs_write_begin FAILD => %d\n",
564 pde->rec_len = cpu_to_le16(to - from);
567 err = exofs_commit_chunk(page, pos, to - from);
568 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
569 mark_inode_dirty(inode);
572 exofs_put_page(page);
576 /* kept aligned on 4 bytes */
577 #define THIS_DIR ".\0\0"
578 #define PARENT_DIR "..\0"
580 int exofs_make_empty(struct inode *inode, struct inode *parent)
582 struct address_space *mapping = inode->i_mapping;
583 struct page *page = grab_cache_page(mapping, 0);
584 unsigned chunk_size = exofs_chunk_size(inode);
585 struct exofs_dir_entry *de;
592 err = exofs_write_begin(NULL, page->mapping, 0, chunk_size, 0,
599 kaddr = kmap_atomic(page, KM_USER0);
600 de = (struct exofs_dir_entry *)kaddr;
602 de->rec_len = cpu_to_le16(EXOFS_DIR_REC_LEN(1));
603 memcpy(de->name, THIS_DIR, sizeof(THIS_DIR));
604 de->inode_no = cpu_to_le64(inode->i_ino);
605 exofs_set_de_type(de, inode);
607 de = (struct exofs_dir_entry *)(kaddr + EXOFS_DIR_REC_LEN(1));
609 de->rec_len = cpu_to_le16(chunk_size - EXOFS_DIR_REC_LEN(1));
610 de->inode_no = cpu_to_le64(parent->i_ino);
611 memcpy(de->name, PARENT_DIR, sizeof(PARENT_DIR));
612 exofs_set_de_type(de, inode);
613 kunmap_atomic(page, KM_USER0);
614 err = exofs_commit_chunk(page, 0, chunk_size);
616 page_cache_release(page);
620 int exofs_empty_dir(struct inode *inode)
622 struct page *page = NULL;
623 unsigned long i, npages = dir_pages(inode);
625 for (i = 0; i < npages; i++) {
627 struct exofs_dir_entry *de;
628 page = exofs_get_page(inode, i);
633 kaddr = page_address(page);
634 de = (struct exofs_dir_entry *)kaddr;
635 kaddr += exofs_last_byte(inode, i) - EXOFS_DIR_REC_LEN(1);
637 while ((char *)de <= kaddr) {
638 if (de->rec_len == 0) {
639 EXOFS_ERR("ERROR: exofs_empty_dir: "
640 "zero-length directory entry"
641 "kaddr=%p, de=%p\n", kaddr, de);
644 if (de->inode_no != 0) {
645 /* check for . and .. */
646 if (de->name[0] != '.')
648 if (de->name_len > 2)
650 if (de->name_len < 2) {
651 if (le64_to_cpu(de->inode_no) !=
654 } else if (de->name[1] != '.')
657 de = exofs_next_entry(de);
659 exofs_put_page(page);
664 exofs_put_page(page);
668 const struct file_operations exofs_dir_operations = {
669 .llseek = generic_file_llseek,
670 .read = generic_read_dir,
671 .readdir = exofs_readdir,