3 * Library for filesystems writers.
6 #include <linux/pagemap.h>
7 #include <linux/smp_lock.h>
9 int simple_statfs(struct super_block
*sb
, struct statfs
*buf
)
11 buf
->f_type
= sb
->s_magic
;
12 buf
->f_bsize
= PAGE_CACHE_SIZE
;
13 buf
->f_namelen
= NAME_MAX
;
18 * Lookup the data. This is trivial - if the dentry didn't already
19 * exist, we know it is negative.
22 struct dentry
*simple_lookup(struct inode
*dir
, struct dentry
*dentry
)
28 int simple_sync_file(struct file
* file
, struct dentry
*dentry
, int datasync
)
33 int dcache_dir_open(struct inode
*inode
, struct file
*file
)
35 static struct qstr cursor_name
= {.len
= 1, .name
= "."};
37 file
->private_data
= d_alloc(file
->f_dentry
, &cursor_name
);
39 return file
->private_data
? 0 : -ENOMEM
;
42 int dcache_dir_close(struct inode
*inode
, struct file
*file
)
44 dput(file
->private_data
);
48 loff_t
dcache_dir_lseek(struct file
*file
, loff_t offset
, int origin
)
50 down(&file
->f_dentry
->d_inode
->i_sem
);
53 offset
+= file
->f_pos
;
58 up(&file
->f_dentry
->d_inode
->i_sem
);
61 if (offset
!= file
->f_pos
) {
63 if (file
->f_pos
>= 2) {
65 struct dentry
*cursor
= file
->private_data
;
66 loff_t n
= file
->f_pos
- 2;
68 spin_lock(&dcache_lock
);
69 p
= file
->f_dentry
->d_subdirs
.next
;
70 while (n
&& p
!= &file
->f_dentry
->d_subdirs
) {
72 next
= list_entry(p
, struct dentry
, d_child
);
73 if (!list_empty(&next
->d_hash
) && next
->d_inode
)
77 list_del(&cursor
->d_child
);
78 list_add_tail(&cursor
->d_child
, p
);
79 spin_unlock(&dcache_lock
);
82 up(&file
->f_dentry
->d_inode
->i_sem
);
86 /* Relationship between i_mode and the DT_xxx types */
87 static inline unsigned char dt_type(struct inode
*inode
)
89 return (inode
->i_mode
>> 12) & 15;
93 * Directory is locked and all positive dentries in it are safe, since
94 * for ramfs-type trees they can't go away without unlink() or rmdir(),
95 * both impossible due to the lock on directory.
98 int dcache_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
100 struct dentry
*dentry
= filp
->f_dentry
;
101 struct dentry
*cursor
= filp
->private_data
;
102 struct list_head
*p
, *q
= &cursor
->d_child
;
108 ino
= dentry
->d_inode
->i_ino
;
109 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
115 ino
= parent_ino(dentry
);
116 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
122 spin_lock(&dcache_lock
);
123 if (filp
->f_pos
== 2) {
125 list_add(q
, &dentry
->d_subdirs
);
127 for (p
=q
->next
; p
!= &dentry
->d_subdirs
; p
=p
->next
) {
129 next
= list_entry(p
, struct dentry
, d_child
);
130 if (list_empty(&next
->d_hash
) || !next
->d_inode
)
133 spin_unlock(&dcache_lock
);
134 if (filldir(dirent
, next
->d_name
.name
, next
->d_name
.len
, filp
->f_pos
, next
->d_inode
->i_ino
, dt_type(next
->d_inode
)) < 0)
136 spin_lock(&dcache_lock
);
137 /* next is still alive */
143 spin_unlock(&dcache_lock
);
148 ssize_t
generic_read_dir(struct file
*filp
, char *buf
, size_t siz
, loff_t
*ppos
)
153 struct file_operations simple_dir_operations
= {
154 .open
= dcache_dir_open
,
155 .release
= dcache_dir_close
,
156 .llseek
= dcache_dir_lseek
,
157 .read
= generic_read_dir
,
158 .readdir
= dcache_readdir
,
161 struct inode_operations simple_dir_inode_operations
= {
162 .lookup
= simple_lookup
,
166 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
167 * will never be mountable)
170 get_sb_pseudo(struct file_system_type
*fs_type
, char *name
,
171 struct super_operations
*ops
, unsigned long magic
)
173 struct super_block
*s
= sget(fs_type
, NULL
, set_anon_super
, NULL
);
174 static struct super_operations default_ops
= {.statfs
= simple_statfs
};
175 struct dentry
*dentry
;
177 struct qstr d_name
= {.name
= name
, .len
= strlen(name
)};
182 s
->s_flags
= MS_NOUSER
;
183 s
->s_maxbytes
= ~0ULL;
184 s
->s_blocksize
= 1024;
185 s
->s_blocksize_bits
= 10;
187 s
->s_op
= ops
? ops
: &default_ops
;
191 root
->i_mode
= S_IFDIR
| S_IRUSR
| S_IWUSR
;
192 root
->i_uid
= root
->i_gid
= 0;
193 root
->i_atime
= root
->i_mtime
= root
->i_ctime
= CURRENT_TIME
;
194 dentry
= d_alloc(NULL
, &d_name
);
200 dentry
->d_parent
= dentry
;
201 d_instantiate(dentry
, root
);
203 s
->s_flags
|= MS_ACTIVE
;
207 up_write(&s
->s_umount
);
209 return ERR_PTR(-ENOMEM
);
212 int simple_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
214 struct inode
*inode
= old_dentry
->d_inode
;
217 atomic_inc(&inode
->i_count
);
219 d_instantiate(dentry
, inode
);
223 static inline int simple_positive(struct dentry
*dentry
)
225 return dentry
->d_inode
&& !d_unhashed(dentry
);
228 int simple_empty(struct dentry
*dentry
)
230 struct dentry
*child
;
233 spin_lock(&dcache_lock
);
234 list_for_each_entry(child
, &dentry
->d_subdirs
, d_child
)
235 if (simple_positive(child
))
239 spin_unlock(&dcache_lock
);
243 int simple_unlink(struct inode
*dir
, struct dentry
*dentry
)
245 struct inode
*inode
= dentry
->d_inode
;
252 int simple_rmdir(struct inode
*dir
, struct dentry
*dentry
)
254 if (!simple_empty(dentry
))
257 dentry
->d_inode
->i_nlink
--;
258 simple_unlink(dir
, dentry
);
263 int simple_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
264 struct inode
*new_dir
, struct dentry
*new_dentry
)
266 int they_are_dirs
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
268 if (!simple_empty(new_dentry
))
271 if (new_dentry
->d_inode
) {
272 simple_unlink(new_dir
, new_dentry
);
275 } else if (they_are_dirs
) {
282 int simple_readpage(struct file
*file
, struct page
*page
)
286 if (PageUptodate(page
))
289 kaddr
= kmap_atomic(page
, KM_USER0
);
290 memset(kaddr
, 0, PAGE_CACHE_SIZE
);
291 kunmap_atomic(kaddr
, KM_USER0
);
292 flush_dcache_page(page
);
293 SetPageUptodate(page
);
299 int simple_prepare_write(struct file
*file
, struct page
*page
,
300 unsigned from
, unsigned to
)
302 if (!PageUptodate(page
)) {
303 if (to
- from
!= PAGE_CACHE_SIZE
) {
304 void *kaddr
= kmap_atomic(page
, KM_USER0
);
305 memset(kaddr
, 0, from
);
306 memset(kaddr
+ to
, 0, PAGE_CACHE_SIZE
- to
);
307 flush_dcache_page(page
);
308 kunmap_atomic(kaddr
, KM_USER0
);
310 SetPageUptodate(page
);
315 int simple_commit_write(struct file
*file
, struct page
*page
,
316 unsigned offset
, unsigned to
)
318 struct inode
*inode
= page
->mapping
->host
;
319 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
321 if (pos
> inode
->i_size
)
323 set_page_dirty(page
);