[PATCH] Add a driver for the Technisat Skystar2 DVB card
[linux-2.6/history.git] / fs / libfs.c
blobbefcf41b0e30cab774959c27554debffc1b774bf
1 /*
2 * fs/libfs.c
3 * Library for filesystems writers.
4 */
6 #include <linux/pagemap.h>
7 #include <linux/mount.h>
8 #include <linux/vfs.h>
10 int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
11 struct kstat *stat)
13 struct inode *inode = dentry->d_inode;
14 generic_fillattr(inode, stat);
15 stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
16 return 0;
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;
24 return 0;
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)
34 d_add(dentry, NULL);
35 return NULL;
38 int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
40 return 0;
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);
55 return 0;
58 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
60 down(&file->f_dentry->d_inode->i_sem);
61 switch (origin) {
62 case 1:
63 offset += file->f_pos;
64 case 0:
65 if (offset >= 0)
66 break;
67 default:
68 up(&file->f_dentry->d_inode->i_sem);
69 return -EINVAL;
71 if (offset != file->f_pos) {
72 file->f_pos = offset;
73 if (file->f_pos >= 2) {
74 struct list_head *p;
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) {
81 struct dentry *next;
82 next = list_entry(p, struct dentry, d_child);
83 if (!d_unhashed(next) && next->d_inode)
84 n--;
85 p = p->next;
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);
93 return offset;
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;
113 ino_t ino;
114 int i = filp->f_pos;
116 switch (i) {
117 case 0:
118 ino = dentry->d_inode->i_ino;
119 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
120 break;
121 filp->f_pos++;
122 i++;
123 /* fallthrough */
124 case 1:
125 ino = parent_ino(dentry);
126 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
127 break;
128 filp->f_pos++;
129 i++;
130 /* fallthrough */
131 default:
132 spin_lock(&dcache_lock);
133 if (filp->f_pos == 2) {
134 list_del(q);
135 list_add(q, &dentry->d_subdirs);
137 for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
138 struct dentry *next;
139 next = list_entry(p, struct dentry, d_child);
140 if (d_unhashed(next) || !next->d_inode)
141 continue;
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)
145 return 0;
146 spin_lock(&dcache_lock);
147 /* next is still alive */
148 list_del(q);
149 list_add(q, p);
150 p = q;
151 filp->f_pos++;
153 spin_unlock(&dcache_lock);
155 return 0;
158 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
160 return -EISDIR;
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)
179 struct super_block *
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;
186 struct inode *root;
187 struct qstr d_name = {.name = name, .len = strlen(name)};
189 if (IS_ERR(s))
190 return s;
192 s->s_flags = MS_NOUSER;
193 s->s_maxbytes = ~0ULL;
194 s->s_blocksize = 1024;
195 s->s_blocksize_bits = 10;
196 s->s_magic = magic;
197 s->s_op = ops ? ops : &default_ops;
198 root = new_inode(s);
199 if (!root)
200 goto Enomem;
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);
205 if (!dentry) {
206 iput(root);
207 goto Enomem;
209 dentry->d_sb = s;
210 dentry->d_parent = dentry;
211 d_instantiate(dentry, root);
212 s->s_root = dentry;
213 s->s_flags |= MS_ACTIVE;
214 return s;
216 Enomem:
217 up_write(&s->s_umount);
218 deactivate_super(s);
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;
226 inode->i_nlink++;
227 atomic_inc(&inode->i_count);
228 dget(dentry);
229 d_instantiate(dentry, inode);
230 return 0;
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;
241 int ret = 0;
243 spin_lock(&dcache_lock);
244 list_for_each_entry(child, &dentry->d_subdirs, d_child)
245 if (simple_positive(child))
246 goto out;
247 ret = 1;
248 out:
249 spin_unlock(&dcache_lock);
250 return ret;
253 int simple_unlink(struct inode *dir, struct dentry *dentry)
255 struct inode *inode = dentry->d_inode;
257 inode->i_nlink--;
258 dput(dentry);
259 return 0;
262 int simple_rmdir(struct inode *dir, struct dentry *dentry)
264 if (!simple_empty(dentry))
265 return -ENOTEMPTY;
267 dentry->d_inode->i_nlink--;
268 simple_unlink(dir, dentry);
269 dir->i_nlink--;
270 return 0;
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))
279 return -ENOTEMPTY;
281 if (new_dentry->d_inode) {
282 simple_unlink(new_dir, new_dentry);
283 if (they_are_dirs)
284 old_dir->i_nlink--;
285 } else if (they_are_dirs) {
286 old_dir->i_nlink--;
287 new_dir->i_nlink++;
289 return 0;
292 int simple_readpage(struct file *file, struct page *page)
294 void *kaddr;
296 if (PageUptodate(page))
297 goto out;
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);
304 out:
305 unlock_page(page);
306 return 0;
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);
322 return 0;
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);
338 return 0;
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};
344 struct inode *inode;
345 struct dentry *root;
346 struct dentry *dentry;
347 int i;
349 s->s_blocksize = PAGE_CACHE_SIZE;
350 s->s_blocksize_bits = PAGE_CACHE_SHIFT;
351 s->s_magic = magic;
352 s->s_op = &s_ops;
354 inode = new_inode(s);
355 if (!inode)
356 return -ENOMEM;
357 inode->i_mode = S_IFDIR | 0755;
358 inode->i_uid = inode->i_gid = 0;
359 inode->i_blksize = PAGE_CACHE_SIZE;
360 inode->i_blocks = 0;
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);
365 if (!root) {
366 iput(inode);
367 return -ENOMEM;
369 for (i = 0; !files->name || files->name[0]; i++, files++) {
370 struct qstr name;
371 if (!files->name)
372 continue;
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);
377 if (!dentry)
378 goto out;
379 inode = new_inode(s);
380 if (!inode)
381 goto out;
382 inode->i_mode = S_IFREG | files->mode;
383 inode->i_uid = inode->i_gid = 0;
384 inode->i_blksize = PAGE_CACHE_SIZE;
385 inode->i_blocks = 0;
386 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
387 inode->i_fop = files->ops;
388 inode->i_ino = i;
389 d_add(dentry, inode);
391 s->s_root = root;
392 return 0;
393 out:
394 d_genocide(root);
395 dput(root);
396 return -ENOMEM;
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);
408 if (IS_ERR(mnt))
409 return PTR_ERR(mnt);
410 spin_lock(&pin_fs_lock);
411 if (!*mount)
412 *mount = mnt;
414 mntget(*mount);
415 ++*count;
416 spin_unlock(&pin_fs_lock);
417 mntput(mnt);
418 return 0;
421 void simple_release_fs(struct vfsmount **mount, int *count)
423 struct vfsmount *mnt;
424 spin_lock(&pin_fs_lock);
425 mnt = *mount;
426 if (!--*count)
427 *mount = NULL;
428 spin_unlock(&pin_fs_lock);
429 mntput(mnt);