3 * Library for filesystems writers.
6 #include <linux/pagemap.h>
7 #include <linux/mount.h>
10 int simple_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
13 struct inode
*inode
= dentry
->d_inode
;
14 generic_fillattr(inode
, stat
);
15 stat
->blocks
= inode
->i_mapping
->nrpages
<< (PAGE_CACHE_SHIFT
- 9);
19 int simple_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
21 buf
->f_type
= sb
->s_magic
;
22 buf
->f_bsize
= PAGE_CACHE_SIZE
;
23 buf
->f_namelen
= NAME_MAX
;
28 * Lookup the data. This is trivial - if the dentry didn't already
29 * exist, we know it is negative.
32 struct dentry
*simple_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
38 int simple_sync_file(struct file
* file
, struct dentry
*dentry
, int datasync
)
43 int dcache_dir_open(struct inode
*inode
, struct file
*file
)
45 static struct qstr cursor_name
= {.len
= 1, .name
= "."};
47 file
->private_data
= d_alloc(file
->f_dentry
, &cursor_name
);
49 return file
->private_data
? 0 : -ENOMEM
;
52 int dcache_dir_close(struct inode
*inode
, struct file
*file
)
54 dput(file
->private_data
);
58 loff_t
dcache_dir_lseek(struct file
*file
, loff_t offset
, int origin
)
60 down(&file
->f_dentry
->d_inode
->i_sem
);
63 offset
+= file
->f_pos
;
68 up(&file
->f_dentry
->d_inode
->i_sem
);
71 if (offset
!= file
->f_pos
) {
73 if (file
->f_pos
>= 2) {
75 struct dentry
*cursor
= file
->private_data
;
76 loff_t n
= file
->f_pos
- 2;
78 spin_lock(&dcache_lock
);
79 p
= file
->f_dentry
->d_subdirs
.next
;
80 while (n
&& p
!= &file
->f_dentry
->d_subdirs
) {
82 next
= list_entry(p
, struct dentry
, d_child
);
83 if (!d_unhashed(next
) && next
->d_inode
)
87 list_del(&cursor
->d_child
);
88 list_add_tail(&cursor
->d_child
, p
);
89 spin_unlock(&dcache_lock
);
92 up(&file
->f_dentry
->d_inode
->i_sem
);
96 /* Relationship between i_mode and the DT_xxx types */
97 static inline unsigned char dt_type(struct inode
*inode
)
99 return (inode
->i_mode
>> 12) & 15;
103 * Directory is locked and all positive dentries in it are safe, since
104 * for ramfs-type trees they can't go away without unlink() or rmdir(),
105 * both impossible due to the lock on directory.
108 int dcache_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
110 struct dentry
*dentry
= filp
->f_dentry
;
111 struct dentry
*cursor
= filp
->private_data
;
112 struct list_head
*p
, *q
= &cursor
->d_child
;
118 ino
= dentry
->d_inode
->i_ino
;
119 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
125 ino
= parent_ino(dentry
);
126 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
132 spin_lock(&dcache_lock
);
133 if (filp
->f_pos
== 2) {
135 list_add(q
, &dentry
->d_subdirs
);
137 for (p
=q
->next
; p
!= &dentry
->d_subdirs
; p
=p
->next
) {
139 next
= list_entry(p
, struct dentry
, d_child
);
140 if (d_unhashed(next
) || !next
->d_inode
)
143 spin_unlock(&dcache_lock
);
144 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)
146 spin_lock(&dcache_lock
);
147 /* next is still alive */
153 spin_unlock(&dcache_lock
);
158 ssize_t
generic_read_dir(struct file
*filp
, char __user
*buf
, size_t siz
, loff_t
*ppos
)
163 struct file_operations simple_dir_operations
= {
164 .open
= dcache_dir_open
,
165 .release
= dcache_dir_close
,
166 .llseek
= dcache_dir_lseek
,
167 .read
= generic_read_dir
,
168 .readdir
= dcache_readdir
,
171 struct inode_operations simple_dir_inode_operations
= {
172 .lookup
= simple_lookup
,
176 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
177 * will never be mountable)
180 get_sb_pseudo(struct file_system_type
*fs_type
, char *name
,
181 struct super_operations
*ops
, unsigned long magic
)
183 struct super_block
*s
= sget(fs_type
, NULL
, set_anon_super
, NULL
);
184 static struct super_operations default_ops
= {.statfs
= simple_statfs
};
185 struct dentry
*dentry
;
187 struct qstr d_name
= {.name
= name
, .len
= strlen(name
)};
192 s
->s_flags
= MS_NOUSER
;
193 s
->s_maxbytes
= ~0ULL;
194 s
->s_blocksize
= 1024;
195 s
->s_blocksize_bits
= 10;
197 s
->s_op
= ops
? ops
: &default_ops
;
201 root
->i_mode
= S_IFDIR
| S_IRUSR
| S_IWUSR
;
202 root
->i_uid
= root
->i_gid
= 0;
203 root
->i_atime
= root
->i_mtime
= root
->i_ctime
= CURRENT_TIME
;
204 dentry
= d_alloc(NULL
, &d_name
);
210 dentry
->d_parent
= dentry
;
211 d_instantiate(dentry
, root
);
213 s
->s_flags
|= MS_ACTIVE
;
217 up_write(&s
->s_umount
);
219 return ERR_PTR(-ENOMEM
);
222 int simple_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
224 struct inode
*inode
= old_dentry
->d_inode
;
227 atomic_inc(&inode
->i_count
);
229 d_instantiate(dentry
, inode
);
233 static inline int simple_positive(struct dentry
*dentry
)
235 return dentry
->d_inode
&& !d_unhashed(dentry
);
238 int simple_empty(struct dentry
*dentry
)
240 struct dentry
*child
;
243 spin_lock(&dcache_lock
);
244 list_for_each_entry(child
, &dentry
->d_subdirs
, d_child
)
245 if (simple_positive(child
))
249 spin_unlock(&dcache_lock
);
253 int simple_unlink(struct inode
*dir
, struct dentry
*dentry
)
255 struct inode
*inode
= dentry
->d_inode
;
262 int simple_rmdir(struct inode
*dir
, struct dentry
*dentry
)
264 if (!simple_empty(dentry
))
267 dentry
->d_inode
->i_nlink
--;
268 simple_unlink(dir
, dentry
);
273 int simple_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
274 struct inode
*new_dir
, struct dentry
*new_dentry
)
276 int they_are_dirs
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
278 if (!simple_empty(new_dentry
))
281 if (new_dentry
->d_inode
) {
282 simple_unlink(new_dir
, new_dentry
);
285 } else if (they_are_dirs
) {
292 int simple_readpage(struct file
*file
, struct page
*page
)
296 if (PageUptodate(page
))
299 kaddr
= kmap_atomic(page
, KM_USER0
);
300 memset(kaddr
, 0, PAGE_CACHE_SIZE
);
301 kunmap_atomic(kaddr
, KM_USER0
);
302 flush_dcache_page(page
);
303 SetPageUptodate(page
);
309 int simple_prepare_write(struct file
*file
, struct page
*page
,
310 unsigned from
, unsigned to
)
312 if (!PageUptodate(page
)) {
313 if (to
- from
!= PAGE_CACHE_SIZE
) {
314 void *kaddr
= kmap_atomic(page
, KM_USER0
);
315 memset(kaddr
, 0, from
);
316 memset(kaddr
+ to
, 0, PAGE_CACHE_SIZE
- to
);
317 flush_dcache_page(page
);
318 kunmap_atomic(kaddr
, KM_USER0
);
320 SetPageUptodate(page
);
325 int simple_commit_write(struct file
*file
, struct page
*page
,
326 unsigned offset
, unsigned to
)
328 struct inode
*inode
= page
->mapping
->host
;
329 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
332 * No need to use i_size_read() here, the i_size
333 * cannot change under us because we hold the i_sem.
335 if (pos
> inode
->i_size
)
336 i_size_write(inode
, pos
);
337 set_page_dirty(page
);
341 int simple_fill_super(struct super_block
*s
, int magic
, struct tree_descr
*files
)
343 static struct super_operations s_ops
= {.statfs
= simple_statfs
};
346 struct dentry
*dentry
;
349 s
->s_blocksize
= PAGE_CACHE_SIZE
;
350 s
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
354 inode
= new_inode(s
);
357 inode
->i_mode
= S_IFDIR
| 0755;
358 inode
->i_uid
= inode
->i_gid
= 0;
359 inode
->i_blksize
= PAGE_CACHE_SIZE
;
361 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
362 inode
->i_op
= &simple_dir_inode_operations
;
363 inode
->i_fop
= &simple_dir_operations
;
364 root
= d_alloc_root(inode
);
369 for (i
= 0; !files
->name
|| files
->name
[0]; i
++, files
++) {
373 name
.name
= files
->name
;
374 name
.len
= strlen(name
.name
);
375 name
.hash
= full_name_hash(name
.name
, name
.len
);
376 dentry
= d_alloc(root
, &name
);
379 inode
= new_inode(s
);
382 inode
->i_mode
= S_IFREG
| files
->mode
;
383 inode
->i_uid
= inode
->i_gid
= 0;
384 inode
->i_blksize
= PAGE_CACHE_SIZE
;
386 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
387 inode
->i_fop
= files
->ops
;
389 d_add(dentry
, inode
);
399 static spinlock_t pin_fs_lock
= SPIN_LOCK_UNLOCKED
;
401 int simple_pin_fs(char *name
, struct vfsmount
**mount
, int *count
)
403 struct vfsmount
*mnt
= NULL
;
404 spin_lock(&pin_fs_lock
);
405 if (unlikely(!*mount
)) {
406 spin_unlock(&pin_fs_lock
);
407 mnt
= do_kern_mount(name
, 0, name
, NULL
);
410 spin_lock(&pin_fs_lock
);
416 spin_unlock(&pin_fs_lock
);
421 void simple_release_fs(struct vfsmount
**mount
, int *count
)
423 struct vfsmount
*mnt
;
424 spin_lock(&pin_fs_lock
);
428 spin_unlock(&pin_fs_lock
);