2 * Copyright (C) 2005, 2006
3 * Avishay Traeger (avishay@gmail.com)
4 * Copyright (C) 2008, 2009
5 * Boaz Harrosh <ooo@electrozaur.com>
7 * Copyrights for code taken from ext2:
8 * Copyright (C) 1992, 1993, 1994, 1995
9 * Remy Card (card@masi.ibp.fr)
10 * Laboratoire MASI - Institut Blaise Pascal
11 * Universite Pierre et Marie Curie (Paris VI)
13 * linux/fs/minix/inode.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * This file is part of exofs.
18 * exofs is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation. Since it is based on ext2, and the only
21 * valid version of GPL for the Linux kernel is version 2, the only valid
22 * version of GPL for exofs is version 2.
24 * exofs is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with exofs; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 static inline unsigned exofs_chunk_size(struct inode
*inode
)
38 return inode
->i_sb
->s_blocksize
;
41 static inline void exofs_put_page(struct page
*page
)
47 static unsigned exofs_last_byte(struct inode
*inode
, unsigned long page_nr
)
49 loff_t last_byte
= inode
->i_size
;
51 last_byte
-= page_nr
<< PAGE_SHIFT
;
52 if (last_byte
> PAGE_SIZE
)
53 last_byte
= PAGE_SIZE
;
57 static int exofs_commit_chunk(struct page
*page
, loff_t pos
, unsigned len
)
59 struct address_space
*mapping
= page
->mapping
;
60 struct inode
*dir
= mapping
->host
;
65 if (!PageUptodate(page
))
66 SetPageUptodate(page
);
68 if (pos
+len
> dir
->i_size
) {
69 i_size_write(dir
, pos
+len
);
70 mark_inode_dirty(dir
);
75 err
= write_one_page(page
);
82 static bool exofs_check_page(struct page
*page
)
84 struct inode
*dir
= page
->mapping
->host
;
85 unsigned chunk_size
= exofs_chunk_size(dir
);
86 char *kaddr
= page_address(page
);
87 unsigned offs
, rec_len
;
88 unsigned limit
= PAGE_SIZE
;
89 struct exofs_dir_entry
*p
;
92 /* if the page is the last one in the directory */
93 if ((dir
->i_size
>> PAGE_SHIFT
) == page
->index
) {
94 limit
= dir
->i_size
& ~PAGE_MASK
;
95 if (limit
& (chunk_size
- 1))
100 for (offs
= 0; offs
<= limit
- EXOFS_DIR_REC_LEN(1); offs
+= rec_len
) {
101 p
= (struct exofs_dir_entry
*)(kaddr
+ offs
);
102 rec_len
= le16_to_cpu(p
->rec_len
);
104 if (rec_len
< EXOFS_DIR_REC_LEN(1))
108 if (rec_len
< EXOFS_DIR_REC_LEN(p
->name_len
))
110 if (((offs
+ rec_len
- 1) ^ offs
) & ~(chunk_size
-1))
116 SetPageChecked(page
);
120 EXOFS_ERR("ERROR [exofs_check_page]: "
121 "size of directory(0x%lx) is not a multiple of chunk size\n",
126 error
= "rec_len is smaller than minimal";
129 error
= "unaligned directory entry";
132 error
= "rec_len is too small for name_len";
135 error
= "directory entry across blocks";
139 "ERROR [exofs_check_page]: bad entry in directory(0x%lx): %s - "
140 "offset=%lu, inode=0x%llx, rec_len=%d, name_len=%d\n",
141 dir
->i_ino
, error
, (page
->index
<<PAGE_SHIFT
)+offs
,
142 _LLU(le64_to_cpu(p
->inode_no
)),
143 rec_len
, p
->name_len
);
146 p
= (struct exofs_dir_entry
*)(kaddr
+ offs
);
147 EXOFS_ERR("ERROR [exofs_check_page]: "
148 "entry in directory(0x%lx) spans the page boundary"
149 "offset=%lu, inode=0x%llx\n",
150 dir
->i_ino
, (page
->index
<<PAGE_SHIFT
)+offs
,
151 _LLU(le64_to_cpu(p
->inode_no
)));
157 static struct page
*exofs_get_page(struct inode
*dir
, unsigned long n
)
159 struct address_space
*mapping
= dir
->i_mapping
;
160 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
164 if (unlikely(!PageChecked(page
))) {
165 if (PageError(page
) || !exofs_check_page(page
))
172 exofs_put_page(page
);
173 return ERR_PTR(-EIO
);
176 static inline int exofs_match(int len
, const unsigned char *name
,
177 struct exofs_dir_entry
*de
)
179 if (len
!= de
->name_len
)
183 return !memcmp(name
, de
->name
, len
);
187 struct exofs_dir_entry
*exofs_next_entry(struct exofs_dir_entry
*p
)
189 return (struct exofs_dir_entry
*)((char *)p
+ le16_to_cpu(p
->rec_len
));
192 static inline unsigned
193 exofs_validate_entry(char *base
, unsigned offset
, unsigned mask
)
195 struct exofs_dir_entry
*de
= (struct exofs_dir_entry
*)(base
+ offset
);
196 struct exofs_dir_entry
*p
=
197 (struct exofs_dir_entry
*)(base
+ (offset
&mask
));
198 while ((char *)p
< (char *)de
) {
201 p
= exofs_next_entry(p
);
203 return (char *)p
- base
;
206 static unsigned char exofs_filetype_table
[EXOFS_FT_MAX
] = {
207 [EXOFS_FT_UNKNOWN
] = DT_UNKNOWN
,
208 [EXOFS_FT_REG_FILE
] = DT_REG
,
209 [EXOFS_FT_DIR
] = DT_DIR
,
210 [EXOFS_FT_CHRDEV
] = DT_CHR
,
211 [EXOFS_FT_BLKDEV
] = DT_BLK
,
212 [EXOFS_FT_FIFO
] = DT_FIFO
,
213 [EXOFS_FT_SOCK
] = DT_SOCK
,
214 [EXOFS_FT_SYMLINK
] = DT_LNK
,
218 static unsigned char exofs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
219 [S_IFREG
>> S_SHIFT
] = EXOFS_FT_REG_FILE
,
220 [S_IFDIR
>> S_SHIFT
] = EXOFS_FT_DIR
,
221 [S_IFCHR
>> S_SHIFT
] = EXOFS_FT_CHRDEV
,
222 [S_IFBLK
>> S_SHIFT
] = EXOFS_FT_BLKDEV
,
223 [S_IFIFO
>> S_SHIFT
] = EXOFS_FT_FIFO
,
224 [S_IFSOCK
>> S_SHIFT
] = EXOFS_FT_SOCK
,
225 [S_IFLNK
>> S_SHIFT
] = EXOFS_FT_SYMLINK
,
229 void exofs_set_de_type(struct exofs_dir_entry
*de
, struct inode
*inode
)
231 umode_t mode
= inode
->i_mode
;
232 de
->file_type
= exofs_type_by_mode
[(mode
& S_IFMT
) >> S_SHIFT
];
236 exofs_readdir(struct file
*file
, struct dir_context
*ctx
)
238 loff_t pos
= ctx
->pos
;
239 struct inode
*inode
= file_inode(file
);
240 unsigned int offset
= pos
& ~PAGE_MASK
;
241 unsigned long n
= pos
>> PAGE_SHIFT
;
242 unsigned long npages
= dir_pages(inode
);
243 unsigned chunk_mask
= ~(exofs_chunk_size(inode
)-1);
244 int need_revalidate
= (file
->f_version
!= inode
->i_version
);
246 if (pos
> inode
->i_size
- EXOFS_DIR_REC_LEN(1))
249 for ( ; n
< npages
; n
++, offset
= 0) {
251 struct exofs_dir_entry
*de
;
252 struct page
*page
= exofs_get_page(inode
, n
);
255 EXOFS_ERR("ERROR: bad page in directory(0x%lx)\n",
257 ctx
->pos
+= PAGE_SIZE
- offset
;
258 return PTR_ERR(page
);
260 kaddr
= page_address(page
);
261 if (unlikely(need_revalidate
)) {
263 offset
= exofs_validate_entry(kaddr
, offset
,
265 ctx
->pos
= (n
<<PAGE_SHIFT
) + offset
;
267 file
->f_version
= inode
->i_version
;
270 de
= (struct exofs_dir_entry
*)(kaddr
+ offset
);
271 limit
= kaddr
+ exofs_last_byte(inode
, n
) -
272 EXOFS_DIR_REC_LEN(1);
273 for (; (char *)de
<= limit
; de
= exofs_next_entry(de
)) {
274 if (de
->rec_len
== 0) {
276 "zero-length entry in directory(0x%lx)\n",
278 exofs_put_page(page
);
284 if (de
->file_type
< EXOFS_FT_MAX
)
285 t
= exofs_filetype_table
[de
->file_type
];
289 if (!dir_emit(ctx
, de
->name
, de
->name_len
,
290 le64_to_cpu(de
->inode_no
),
292 exofs_put_page(page
);
296 ctx
->pos
+= le16_to_cpu(de
->rec_len
);
298 exofs_put_page(page
);
303 struct exofs_dir_entry
*exofs_find_entry(struct inode
*dir
,
304 struct dentry
*dentry
, struct page
**res_page
)
306 const unsigned char *name
= dentry
->d_name
.name
;
307 int namelen
= dentry
->d_name
.len
;
308 unsigned reclen
= EXOFS_DIR_REC_LEN(namelen
);
309 unsigned long start
, n
;
310 unsigned long npages
= dir_pages(dir
);
311 struct page
*page
= NULL
;
312 struct exofs_i_info
*oi
= exofs_i(dir
);
313 struct exofs_dir_entry
*de
;
320 start
= oi
->i_dir_start_lookup
;
326 page
= exofs_get_page(dir
, n
);
328 kaddr
= page_address(page
);
329 de
= (struct exofs_dir_entry
*) kaddr
;
330 kaddr
+= exofs_last_byte(dir
, n
) - reclen
;
331 while ((char *) de
<= kaddr
) {
332 if (de
->rec_len
== 0) {
333 EXOFS_ERR("ERROR: zero-length entry in "
334 "directory(0x%lx)\n",
336 exofs_put_page(page
);
339 if (exofs_match(namelen
, name
, de
))
341 de
= exofs_next_entry(de
);
343 exofs_put_page(page
);
347 } while (n
!= start
);
353 oi
->i_dir_start_lookup
= n
;
357 struct exofs_dir_entry
*exofs_dotdot(struct inode
*dir
, struct page
**p
)
359 struct page
*page
= exofs_get_page(dir
, 0);
360 struct exofs_dir_entry
*de
= NULL
;
363 de
= exofs_next_entry(
364 (struct exofs_dir_entry
*)page_address(page
));
370 ino_t
exofs_parent_ino(struct dentry
*child
)
373 struct exofs_dir_entry
*de
;
376 de
= exofs_dotdot(d_inode(child
), &page
);
380 ino
= le64_to_cpu(de
->inode_no
);
381 exofs_put_page(page
);
385 ino_t
exofs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
)
388 struct exofs_dir_entry
*de
;
391 de
= exofs_find_entry(dir
, dentry
, &page
);
393 res
= le64_to_cpu(de
->inode_no
);
394 exofs_put_page(page
);
399 int exofs_set_link(struct inode
*dir
, struct exofs_dir_entry
*de
,
400 struct page
*page
, struct inode
*inode
)
402 loff_t pos
= page_offset(page
) +
403 (char *) de
- (char *) page_address(page
);
404 unsigned len
= le16_to_cpu(de
->rec_len
);
408 err
= exofs_write_begin(NULL
, page
->mapping
, pos
, len
, 0, &page
, NULL
);
410 EXOFS_ERR("exofs_set_link: exofs_write_begin FAILED => %d\n",
413 de
->inode_no
= cpu_to_le64(inode
->i_ino
);
414 exofs_set_de_type(de
, inode
);
416 err
= exofs_commit_chunk(page
, pos
, len
);
417 exofs_put_page(page
);
418 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
419 mark_inode_dirty(dir
);
423 int exofs_add_link(struct dentry
*dentry
, struct inode
*inode
)
425 struct inode
*dir
= d_inode(dentry
->d_parent
);
426 const unsigned char *name
= dentry
->d_name
.name
;
427 int namelen
= dentry
->d_name
.len
;
428 unsigned chunk_size
= exofs_chunk_size(dir
);
429 unsigned reclen
= EXOFS_DIR_REC_LEN(namelen
);
430 unsigned short rec_len
, name_len
;
431 struct page
*page
= NULL
;
432 struct exofs_sb_info
*sbi
= inode
->i_sb
->s_fs_info
;
433 struct exofs_dir_entry
*de
;
434 unsigned long npages
= dir_pages(dir
);
440 for (n
= 0; n
<= npages
; n
++) {
443 page
= exofs_get_page(dir
, n
);
448 kaddr
= page_address(page
);
449 dir_end
= kaddr
+ exofs_last_byte(dir
, n
);
450 de
= (struct exofs_dir_entry
*)kaddr
;
451 kaddr
+= PAGE_SIZE
- reclen
;
452 while ((char *)de
<= kaddr
) {
453 if ((char *)de
== dir_end
) {
455 rec_len
= chunk_size
;
456 de
->rec_len
= cpu_to_le16(chunk_size
);
460 if (de
->rec_len
== 0) {
461 EXOFS_ERR("ERROR: exofs_add_link: "
462 "zero-length entry in directory(0x%lx)\n",
468 if (exofs_match(namelen
, name
, de
))
470 name_len
= EXOFS_DIR_REC_LEN(de
->name_len
);
471 rec_len
= le16_to_cpu(de
->rec_len
);
472 if (!de
->inode_no
&& rec_len
>= reclen
)
474 if (rec_len
>= name_len
+ reclen
)
476 de
= (struct exofs_dir_entry
*) ((char *) de
+ rec_len
);
479 exofs_put_page(page
);
482 EXOFS_ERR("exofs_add_link: BAD dentry=%p or inode=0x%lx\n",
483 dentry
, inode
->i_ino
);
487 pos
= page_offset(page
) +
488 (char *)de
- (char *)page_address(page
);
489 err
= exofs_write_begin(NULL
, page
->mapping
, pos
, rec_len
, 0,
494 struct exofs_dir_entry
*de1
=
495 (struct exofs_dir_entry
*)((char *)de
+ name_len
);
496 de1
->rec_len
= cpu_to_le16(rec_len
- name_len
);
497 de
->rec_len
= cpu_to_le16(name_len
);
500 de
->name_len
= namelen
;
501 memcpy(de
->name
, name
, namelen
);
502 de
->inode_no
= cpu_to_le64(inode
->i_ino
);
503 exofs_set_de_type(de
, inode
);
504 err
= exofs_commit_chunk(page
, pos
, rec_len
);
505 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
506 mark_inode_dirty(dir
);
510 exofs_put_page(page
);
518 int exofs_delete_entry(struct exofs_dir_entry
*dir
, struct page
*page
)
520 struct address_space
*mapping
= page
->mapping
;
521 struct inode
*inode
= mapping
->host
;
522 struct exofs_sb_info
*sbi
= inode
->i_sb
->s_fs_info
;
523 char *kaddr
= page_address(page
);
524 unsigned from
= ((char *)dir
- kaddr
) & ~(exofs_chunk_size(inode
)-1);
525 unsigned to
= ((char *)dir
- kaddr
) + le16_to_cpu(dir
->rec_len
);
527 struct exofs_dir_entry
*pde
= NULL
;
528 struct exofs_dir_entry
*de
= (struct exofs_dir_entry
*) (kaddr
+ from
);
532 if (de
->rec_len
== 0) {
533 EXOFS_ERR("ERROR: exofs_delete_entry:"
534 "zero-length entry in directory(0x%lx)\n",
540 de
= exofs_next_entry(de
);
543 from
= (char *)pde
- (char *)page_address(page
);
544 pos
= page_offset(page
) + from
;
546 err
= exofs_write_begin(NULL
, page
->mapping
, pos
, to
- from
, 0,
549 EXOFS_ERR("exofs_delete_entry: exofs_write_begin FAILED => %d\n",
552 pde
->rec_len
= cpu_to_le16(to
- from
);
555 err
= exofs_commit_chunk(page
, pos
, to
- from
);
556 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
557 mark_inode_dirty(inode
);
560 exofs_put_page(page
);
564 /* kept aligned on 4 bytes */
565 #define THIS_DIR ".\0\0"
566 #define PARENT_DIR "..\0"
568 int exofs_make_empty(struct inode
*inode
, struct inode
*parent
)
570 struct address_space
*mapping
= inode
->i_mapping
;
571 struct page
*page
= grab_cache_page(mapping
, 0);
572 unsigned chunk_size
= exofs_chunk_size(inode
);
573 struct exofs_dir_entry
*de
;
580 err
= exofs_write_begin(NULL
, page
->mapping
, 0, chunk_size
, 0,
587 kaddr
= kmap_atomic(page
);
588 de
= (struct exofs_dir_entry
*)kaddr
;
590 de
->rec_len
= cpu_to_le16(EXOFS_DIR_REC_LEN(1));
591 memcpy(de
->name
, THIS_DIR
, sizeof(THIS_DIR
));
592 de
->inode_no
= cpu_to_le64(inode
->i_ino
);
593 exofs_set_de_type(de
, inode
);
595 de
= (struct exofs_dir_entry
*)(kaddr
+ EXOFS_DIR_REC_LEN(1));
597 de
->rec_len
= cpu_to_le16(chunk_size
- EXOFS_DIR_REC_LEN(1));
598 de
->inode_no
= cpu_to_le64(parent
->i_ino
);
599 memcpy(de
->name
, PARENT_DIR
, sizeof(PARENT_DIR
));
600 exofs_set_de_type(de
, inode
);
601 kunmap_atomic(kaddr
);
602 err
= exofs_commit_chunk(page
, 0, chunk_size
);
608 int exofs_empty_dir(struct inode
*inode
)
610 struct page
*page
= NULL
;
611 unsigned long i
, npages
= dir_pages(inode
);
613 for (i
= 0; i
< npages
; i
++) {
615 struct exofs_dir_entry
*de
;
616 page
= exofs_get_page(inode
, i
);
621 kaddr
= page_address(page
);
622 de
= (struct exofs_dir_entry
*)kaddr
;
623 kaddr
+= exofs_last_byte(inode
, i
) - EXOFS_DIR_REC_LEN(1);
625 while ((char *)de
<= kaddr
) {
626 if (de
->rec_len
== 0) {
627 EXOFS_ERR("ERROR: exofs_empty_dir: "
628 "zero-length directory entry"
629 "kaddr=%p, de=%p\n", kaddr
, de
);
632 if (de
->inode_no
!= 0) {
633 /* check for . and .. */
634 if (de
->name
[0] != '.')
636 if (de
->name_len
> 2)
638 if (de
->name_len
< 2) {
639 if (le64_to_cpu(de
->inode_no
) !=
642 } else if (de
->name
[1] != '.')
645 de
= exofs_next_entry(de
);
647 exofs_put_page(page
);
652 exofs_put_page(page
);
656 const struct file_operations exofs_dir_operations
= {
657 .llseek
= generic_file_llseek
,
658 .read
= generic_read_dir
,
659 .iterate_shared
= exofs_readdir
,