3 * Library for filesystems writers.
6 #include <linux/blkdev.h>
7 #include <linux/export.h>
8 #include <linux/pagemap.h>
9 #include <linux/slab.h>
10 #include <linux/mount.h>
11 #include <linux/vfs.h>
12 #include <linux/quotaops.h>
13 #include <linux/mutex.h>
14 #include <linux/namei.h>
15 #include <linux/exportfs.h>
16 #include <linux/writeback.h>
17 #include <linux/buffer_head.h> /* sync_mapping_buffers */
19 #include <asm/uaccess.h>
23 static inline int simple_positive(struct dentry
*dentry
)
25 return dentry
->d_inode
&& !d_unhashed(dentry
);
28 int simple_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
31 struct inode
*inode
= dentry
->d_inode
;
32 generic_fillattr(inode
, stat
);
33 stat
->blocks
= inode
->i_mapping
->nrpages
<< (PAGE_CACHE_SHIFT
- 9);
36 EXPORT_SYMBOL(simple_getattr
);
38 int simple_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
40 buf
->f_type
= dentry
->d_sb
->s_magic
;
41 buf
->f_bsize
= PAGE_CACHE_SIZE
;
42 buf
->f_namelen
= NAME_MAX
;
45 EXPORT_SYMBOL(simple_statfs
);
48 * Retaining negative dentries for an in-memory filesystem just wastes
49 * memory and lookup time: arrange for them to be deleted immediately.
51 int always_delete_dentry(const struct dentry
*dentry
)
55 EXPORT_SYMBOL(always_delete_dentry
);
57 const struct dentry_operations simple_dentry_operations
= {
58 .d_delete
= always_delete_dentry
,
60 EXPORT_SYMBOL(simple_dentry_operations
);
63 * Lookup the data. This is trivial - if the dentry didn't already
64 * exist, we know it is negative. Set d_op to delete negative dentries.
66 struct dentry
*simple_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
68 if (dentry
->d_name
.len
> NAME_MAX
)
69 return ERR_PTR(-ENAMETOOLONG
);
70 if (!dentry
->d_sb
->s_d_op
)
71 d_set_d_op(dentry
, &simple_dentry_operations
);
75 EXPORT_SYMBOL(simple_lookup
);
77 int dcache_dir_open(struct inode
*inode
, struct file
*file
)
79 static struct qstr cursor_name
= QSTR_INIT(".", 1);
81 file
->private_data
= d_alloc(file
->f_path
.dentry
, &cursor_name
);
83 return file
->private_data
? 0 : -ENOMEM
;
85 EXPORT_SYMBOL(dcache_dir_open
);
87 int dcache_dir_close(struct inode
*inode
, struct file
*file
)
89 dput(file
->private_data
);
92 EXPORT_SYMBOL(dcache_dir_close
);
94 loff_t
dcache_dir_lseek(struct file
*file
, loff_t offset
, int whence
)
96 struct dentry
*dentry
= file
->f_path
.dentry
;
97 mutex_lock(&dentry
->d_inode
->i_mutex
);
100 offset
+= file
->f_pos
;
105 mutex_unlock(&dentry
->d_inode
->i_mutex
);
108 if (offset
!= file
->f_pos
) {
109 file
->f_pos
= offset
;
110 if (file
->f_pos
>= 2) {
112 struct dentry
*cursor
= file
->private_data
;
113 loff_t n
= file
->f_pos
- 2;
115 spin_lock(&dentry
->d_lock
);
116 /* d_lock not required for cursor */
117 list_del(&cursor
->d_child
);
118 p
= dentry
->d_subdirs
.next
;
119 while (n
&& p
!= &dentry
->d_subdirs
) {
121 next
= list_entry(p
, struct dentry
, d_child
);
122 spin_lock_nested(&next
->d_lock
, DENTRY_D_LOCK_NESTED
);
123 if (simple_positive(next
))
125 spin_unlock(&next
->d_lock
);
128 list_add_tail(&cursor
->d_child
, p
);
129 spin_unlock(&dentry
->d_lock
);
132 mutex_unlock(&dentry
->d_inode
->i_mutex
);
135 EXPORT_SYMBOL(dcache_dir_lseek
);
137 /* Relationship between i_mode and the DT_xxx types */
138 static inline unsigned char dt_type(struct inode
*inode
)
140 return (inode
->i_mode
>> 12) & 15;
144 * Directory is locked and all positive dentries in it are safe, since
145 * for ramfs-type trees they can't go away without unlink() or rmdir(),
146 * both impossible due to the lock on directory.
149 int dcache_readdir(struct file
*file
, struct dir_context
*ctx
)
151 struct dentry
*dentry
= file
->f_path
.dentry
;
152 struct dentry
*cursor
= file
->private_data
;
153 struct list_head
*p
, *q
= &cursor
->d_child
;
155 if (!dir_emit_dots(file
, ctx
))
157 spin_lock(&dentry
->d_lock
);
159 list_move(q
, &dentry
->d_subdirs
);
161 for (p
= q
->next
; p
!= &dentry
->d_subdirs
; p
= p
->next
) {
162 struct dentry
*next
= list_entry(p
, struct dentry
, d_child
);
163 spin_lock_nested(&next
->d_lock
, DENTRY_D_LOCK_NESTED
);
164 if (!simple_positive(next
)) {
165 spin_unlock(&next
->d_lock
);
169 spin_unlock(&next
->d_lock
);
170 spin_unlock(&dentry
->d_lock
);
171 if (!dir_emit(ctx
, next
->d_name
.name
, next
->d_name
.len
,
172 next
->d_inode
->i_ino
, dt_type(next
->d_inode
)))
174 spin_lock(&dentry
->d_lock
);
175 spin_lock_nested(&next
->d_lock
, DENTRY_D_LOCK_NESTED
);
176 /* next is still alive */
178 spin_unlock(&next
->d_lock
);
182 spin_unlock(&dentry
->d_lock
);
185 EXPORT_SYMBOL(dcache_readdir
);
187 ssize_t
generic_read_dir(struct file
*filp
, char __user
*buf
, size_t siz
, loff_t
*ppos
)
191 EXPORT_SYMBOL(generic_read_dir
);
193 const struct file_operations simple_dir_operations
= {
194 .open
= dcache_dir_open
,
195 .release
= dcache_dir_close
,
196 .llseek
= dcache_dir_lseek
,
197 .read
= generic_read_dir
,
198 .iterate
= dcache_readdir
,
201 EXPORT_SYMBOL(simple_dir_operations
);
203 const struct inode_operations simple_dir_inode_operations
= {
204 .lookup
= simple_lookup
,
206 EXPORT_SYMBOL(simple_dir_inode_operations
);
208 static const struct super_operations simple_super_operations
= {
209 .statfs
= simple_statfs
,
213 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
214 * will never be mountable)
216 struct dentry
*mount_pseudo(struct file_system_type
*fs_type
, char *name
,
217 const struct super_operations
*ops
,
218 const struct dentry_operations
*dops
, unsigned long magic
)
220 struct super_block
*s
;
221 struct dentry
*dentry
;
223 struct qstr d_name
= QSTR_INIT(name
, strlen(name
));
225 s
= sget(fs_type
, NULL
, set_anon_super
, MS_NOUSER
, NULL
);
229 s
->s_maxbytes
= MAX_LFS_FILESIZE
;
230 s
->s_blocksize
= PAGE_SIZE
;
231 s
->s_blocksize_bits
= PAGE_SHIFT
;
233 s
->s_op
= ops
? ops
: &simple_super_operations
;
239 * since this is the first inode, make it number 1. New inodes created
240 * after this must take care not to collide with it (by passing
241 * max_reserved of 1 to iunique).
244 root
->i_mode
= S_IFDIR
| S_IRUSR
| S_IWUSR
;
245 root
->i_atime
= root
->i_mtime
= root
->i_ctime
= CURRENT_TIME
;
246 dentry
= __d_alloc(s
, &d_name
);
251 d_instantiate(dentry
, root
);
254 s
->s_flags
|= MS_ACTIVE
;
255 return dget(s
->s_root
);
258 deactivate_locked_super(s
);
259 return ERR_PTR(-ENOMEM
);
261 EXPORT_SYMBOL(mount_pseudo
);
263 int simple_open(struct inode
*inode
, struct file
*file
)
265 if (inode
->i_private
)
266 file
->private_data
= inode
->i_private
;
269 EXPORT_SYMBOL(simple_open
);
271 int simple_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
273 struct inode
*inode
= old_dentry
->d_inode
;
275 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
279 d_instantiate(dentry
, inode
);
282 EXPORT_SYMBOL(simple_link
);
284 int simple_empty(struct dentry
*dentry
)
286 struct dentry
*child
;
289 spin_lock(&dentry
->d_lock
);
290 list_for_each_entry(child
, &dentry
->d_subdirs
, d_child
) {
291 spin_lock_nested(&child
->d_lock
, DENTRY_D_LOCK_NESTED
);
292 if (simple_positive(child
)) {
293 spin_unlock(&child
->d_lock
);
296 spin_unlock(&child
->d_lock
);
300 spin_unlock(&dentry
->d_lock
);
303 EXPORT_SYMBOL(simple_empty
);
305 int simple_unlink(struct inode
*dir
, struct dentry
*dentry
)
307 struct inode
*inode
= dentry
->d_inode
;
309 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
314 EXPORT_SYMBOL(simple_unlink
);
316 int simple_rmdir(struct inode
*dir
, struct dentry
*dentry
)
318 if (!simple_empty(dentry
))
321 drop_nlink(dentry
->d_inode
);
322 simple_unlink(dir
, dentry
);
326 EXPORT_SYMBOL(simple_rmdir
);
328 int simple_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
329 struct inode
*new_dir
, struct dentry
*new_dentry
)
331 struct inode
*inode
= old_dentry
->d_inode
;
332 int they_are_dirs
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
334 if (!simple_empty(new_dentry
))
337 if (new_dentry
->d_inode
) {
338 simple_unlink(new_dir
, new_dentry
);
340 drop_nlink(new_dentry
->d_inode
);
343 } else if (they_are_dirs
) {
348 old_dir
->i_ctime
= old_dir
->i_mtime
= new_dir
->i_ctime
=
349 new_dir
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
353 EXPORT_SYMBOL(simple_rename
);
356 * simple_setattr - setattr for simple filesystem
358 * @iattr: iattr structure
360 * Returns 0 on success, -error on failure.
362 * simple_setattr is a simple ->setattr implementation without a proper
363 * implementation of size changes.
365 * It can either be used for in-memory filesystems or special files
366 * on simple regular filesystems. Anything that needs to change on-disk
367 * or wire state on size changes needs its own setattr method.
369 int simple_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
371 struct inode
*inode
= dentry
->d_inode
;
374 error
= inode_change_ok(inode
, iattr
);
378 if (iattr
->ia_valid
& ATTR_SIZE
)
379 truncate_setsize(inode
, iattr
->ia_size
);
380 setattr_copy(inode
, iattr
);
381 mark_inode_dirty(inode
);
384 EXPORT_SYMBOL(simple_setattr
);
386 int simple_readpage(struct file
*file
, struct page
*page
)
388 clear_highpage(page
);
389 flush_dcache_page(page
);
390 SetPageUptodate(page
);
394 EXPORT_SYMBOL(simple_readpage
);
396 int simple_write_begin(struct file
*file
, struct address_space
*mapping
,
397 loff_t pos
, unsigned len
, unsigned flags
,
398 struct page
**pagep
, void **fsdata
)
403 index
= pos
>> PAGE_CACHE_SHIFT
;
405 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
411 if (!PageUptodate(page
) && (len
!= PAGE_CACHE_SIZE
)) {
412 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
414 zero_user_segments(page
, 0, from
, from
+ len
, PAGE_CACHE_SIZE
);
418 EXPORT_SYMBOL(simple_write_begin
);
421 * simple_write_end - .write_end helper for non-block-device FSes
422 * @available: See .write_end of address_space_operations
431 * simple_write_end does the minimum needed for updating a page after writing is
432 * done. It has the same API signature as the .write_end of
433 * address_space_operations vector. So it can just be set onto .write_end for
434 * FSes that don't need any other processing. i_mutex is assumed to be held.
435 * Block based filesystems should use generic_write_end().
436 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
437 * is not called, so a filesystem that actually does store data in .write_inode
438 * should extend on what's done here with a call to mark_inode_dirty() in the
439 * case that i_size has changed.
441 int simple_write_end(struct file
*file
, struct address_space
*mapping
,
442 loff_t pos
, unsigned len
, unsigned copied
,
443 struct page
*page
, void *fsdata
)
445 struct inode
*inode
= page
->mapping
->host
;
446 loff_t last_pos
= pos
+ copied
;
448 /* zero the stale part of the page if we did a short copy */
450 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
452 zero_user(page
, from
+ copied
, len
- copied
);
455 if (!PageUptodate(page
))
456 SetPageUptodate(page
);
458 * No need to use i_size_read() here, the i_size
459 * cannot change under us because we hold the i_mutex.
461 if (last_pos
> inode
->i_size
)
462 i_size_write(inode
, last_pos
);
464 set_page_dirty(page
);
466 page_cache_release(page
);
470 EXPORT_SYMBOL(simple_write_end
);
473 * the inodes created here are not hashed. If you use iunique to generate
474 * unique inode values later for this filesystem, then you must take care
475 * to pass it an appropriate max_reserved value to avoid collisions.
477 int simple_fill_super(struct super_block
*s
, unsigned long magic
,
478 struct tree_descr
*files
)
482 struct dentry
*dentry
;
485 s
->s_blocksize
= PAGE_CACHE_SIZE
;
486 s
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
488 s
->s_op
= &simple_super_operations
;
491 inode
= new_inode(s
);
495 * because the root inode is 1, the files array must not contain an
499 inode
->i_mode
= S_IFDIR
| 0755;
500 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
501 inode
->i_op
= &simple_dir_inode_operations
;
502 inode
->i_fop
= &simple_dir_operations
;
504 root
= d_make_root(inode
);
507 for (i
= 0; !files
->name
|| files
->name
[0]; i
++, files
++) {
511 /* warn if it tries to conflict with the root inode */
512 if (unlikely(i
== 1))
513 printk(KERN_WARNING
"%s: %s passed in a files array"
514 "with an index of 1!\n", __func__
,
517 dentry
= d_alloc_name(root
, files
->name
);
520 inode
= new_inode(s
);
525 inode
->i_mode
= S_IFREG
| files
->mode
;
526 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
527 inode
->i_fop
= files
->ops
;
529 d_add(dentry
, inode
);
535 shrink_dcache_parent(root
);
539 EXPORT_SYMBOL(simple_fill_super
);
541 static DEFINE_SPINLOCK(pin_fs_lock
);
543 int simple_pin_fs(struct file_system_type
*type
, struct vfsmount
**mount
, int *count
)
545 struct vfsmount
*mnt
= NULL
;
546 spin_lock(&pin_fs_lock
);
547 if (unlikely(!*mount
)) {
548 spin_unlock(&pin_fs_lock
);
549 mnt
= vfs_kern_mount(type
, MS_KERNMOUNT
, type
->name
, NULL
);
552 spin_lock(&pin_fs_lock
);
558 spin_unlock(&pin_fs_lock
);
562 EXPORT_SYMBOL(simple_pin_fs
);
564 void simple_release_fs(struct vfsmount
**mount
, int *count
)
566 struct vfsmount
*mnt
;
567 spin_lock(&pin_fs_lock
);
571 spin_unlock(&pin_fs_lock
);
574 EXPORT_SYMBOL(simple_release_fs
);
577 * simple_read_from_buffer - copy data from the buffer to user space
578 * @to: the user space buffer to read to
579 * @count: the maximum number of bytes to read
580 * @ppos: the current position in the buffer
581 * @from: the buffer to read from
582 * @available: the size of the buffer
584 * The simple_read_from_buffer() function reads up to @count bytes from the
585 * buffer @from at offset @ppos into the user space address starting at @to.
587 * On success, the number of bytes read is returned and the offset @ppos is
588 * advanced by this number, or negative value is returned on error.
590 ssize_t
simple_read_from_buffer(void __user
*to
, size_t count
, loff_t
*ppos
,
591 const void *from
, size_t available
)
598 if (pos
>= available
|| !count
)
600 if (count
> available
- pos
)
601 count
= available
- pos
;
602 ret
= copy_to_user(to
, from
+ pos
, count
);
609 EXPORT_SYMBOL(simple_read_from_buffer
);
612 * simple_write_to_buffer - copy data from user space to the buffer
613 * @to: the buffer to write to
614 * @available: the size of the buffer
615 * @ppos: the current position in the buffer
616 * @from: the user space buffer to read from
617 * @count: the maximum number of bytes to read
619 * The simple_write_to_buffer() function reads up to @count bytes from the user
620 * space address starting at @from into the buffer @to at offset @ppos.
622 * On success, the number of bytes written is returned and the offset @ppos is
623 * advanced by this number, or negative value is returned on error.
625 ssize_t
simple_write_to_buffer(void *to
, size_t available
, loff_t
*ppos
,
626 const void __user
*from
, size_t count
)
633 if (pos
>= available
|| !count
)
635 if (count
> available
- pos
)
636 count
= available
- pos
;
637 res
= copy_from_user(to
+ pos
, from
, count
);
644 EXPORT_SYMBOL(simple_write_to_buffer
);
647 * memory_read_from_buffer - copy data from the buffer
648 * @to: the kernel space buffer to read to
649 * @count: the maximum number of bytes to read
650 * @ppos: the current position in the buffer
651 * @from: the buffer to read from
652 * @available: the size of the buffer
654 * The memory_read_from_buffer() function reads up to @count bytes from the
655 * buffer @from at offset @ppos into the kernel space address starting at @to.
657 * On success, the number of bytes read is returned and the offset @ppos is
658 * advanced by this number, or negative value is returned on error.
660 ssize_t
memory_read_from_buffer(void *to
, size_t count
, loff_t
*ppos
,
661 const void *from
, size_t available
)
667 if (pos
>= available
)
669 if (count
> available
- pos
)
670 count
= available
- pos
;
671 memcpy(to
, from
+ pos
, count
);
676 EXPORT_SYMBOL(memory_read_from_buffer
);
679 * Transaction based IO.
680 * The file expects a single write which triggers the transaction, and then
681 * possibly a read which collects the result - which is stored in a
685 void simple_transaction_set(struct file
*file
, size_t n
)
687 struct simple_transaction_argresp
*ar
= file
->private_data
;
689 BUG_ON(n
> SIMPLE_TRANSACTION_LIMIT
);
692 * The barrier ensures that ar->size will really remain zero until
693 * ar->data is ready for reading.
698 EXPORT_SYMBOL(simple_transaction_set
);
700 char *simple_transaction_get(struct file
*file
, const char __user
*buf
, size_t size
)
702 struct simple_transaction_argresp
*ar
;
703 static DEFINE_SPINLOCK(simple_transaction_lock
);
705 if (size
> SIMPLE_TRANSACTION_LIMIT
- 1)
706 return ERR_PTR(-EFBIG
);
708 ar
= (struct simple_transaction_argresp
*)get_zeroed_page(GFP_KERNEL
);
710 return ERR_PTR(-ENOMEM
);
712 spin_lock(&simple_transaction_lock
);
714 /* only one write allowed per open */
715 if (file
->private_data
) {
716 spin_unlock(&simple_transaction_lock
);
717 free_page((unsigned long)ar
);
718 return ERR_PTR(-EBUSY
);
721 file
->private_data
= ar
;
723 spin_unlock(&simple_transaction_lock
);
725 if (copy_from_user(ar
->data
, buf
, size
))
726 return ERR_PTR(-EFAULT
);
730 EXPORT_SYMBOL(simple_transaction_get
);
732 ssize_t
simple_transaction_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*pos
)
734 struct simple_transaction_argresp
*ar
= file
->private_data
;
738 return simple_read_from_buffer(buf
, size
, pos
, ar
->data
, ar
->size
);
740 EXPORT_SYMBOL(simple_transaction_read
);
742 int simple_transaction_release(struct inode
*inode
, struct file
*file
)
744 free_page((unsigned long)file
->private_data
);
747 EXPORT_SYMBOL(simple_transaction_release
);
749 /* Simple attribute files */
752 int (*get
)(void *, u64
*);
753 int (*set
)(void *, u64
);
754 char get_buf
[24]; /* enough to store a u64 and "\n\0" */
757 const char *fmt
; /* format for read operation */
758 struct mutex mutex
; /* protects access to these buffers */
761 /* simple_attr_open is called by an actual attribute open file operation
762 * to set the attribute specific access operations. */
763 int simple_attr_open(struct inode
*inode
, struct file
*file
,
764 int (*get
)(void *, u64
*), int (*set
)(void *, u64
),
767 struct simple_attr
*attr
;
769 attr
= kmalloc(sizeof(*attr
), GFP_KERNEL
);
775 attr
->data
= inode
->i_private
;
777 mutex_init(&attr
->mutex
);
779 file
->private_data
= attr
;
781 return nonseekable_open(inode
, file
);
783 EXPORT_SYMBOL_GPL(simple_attr_open
);
785 int simple_attr_release(struct inode
*inode
, struct file
*file
)
787 kfree(file
->private_data
);
790 EXPORT_SYMBOL_GPL(simple_attr_release
); /* GPL-only? This? Really? */
792 /* read from the buffer that is filled with the get function */
793 ssize_t
simple_attr_read(struct file
*file
, char __user
*buf
,
794 size_t len
, loff_t
*ppos
)
796 struct simple_attr
*attr
;
800 attr
= file
->private_data
;
805 ret
= mutex_lock_interruptible(&attr
->mutex
);
809 if (*ppos
) { /* continued read */
810 size
= strlen(attr
->get_buf
);
811 } else { /* first read */
813 ret
= attr
->get(attr
->data
, &val
);
817 size
= scnprintf(attr
->get_buf
, sizeof(attr
->get_buf
),
818 attr
->fmt
, (unsigned long long)val
);
821 ret
= simple_read_from_buffer(buf
, len
, ppos
, attr
->get_buf
, size
);
823 mutex_unlock(&attr
->mutex
);
826 EXPORT_SYMBOL_GPL(simple_attr_read
);
828 /* interpret the buffer as a number to call the set function with */
829 ssize_t
simple_attr_write(struct file
*file
, const char __user
*buf
,
830 size_t len
, loff_t
*ppos
)
832 struct simple_attr
*attr
;
837 attr
= file
->private_data
;
841 ret
= mutex_lock_interruptible(&attr
->mutex
);
846 size
= min(sizeof(attr
->set_buf
) - 1, len
);
847 if (copy_from_user(attr
->set_buf
, buf
, size
))
850 attr
->set_buf
[size
] = '\0';
851 val
= simple_strtoll(attr
->set_buf
, NULL
, 0);
852 ret
= attr
->set(attr
->data
, val
);
854 ret
= len
; /* on success, claim we got the whole input */
856 mutex_unlock(&attr
->mutex
);
859 EXPORT_SYMBOL_GPL(simple_attr_write
);
862 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
863 * @sb: filesystem to do the file handle conversion on
864 * @fid: file handle to convert
865 * @fh_len: length of the file handle in bytes
866 * @fh_type: type of file handle
867 * @get_inode: filesystem callback to retrieve inode
869 * This function decodes @fid as long as it has one of the well-known
870 * Linux filehandle types and calls @get_inode on it to retrieve the
871 * inode for the object specified in the file handle.
873 struct dentry
*generic_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
874 int fh_len
, int fh_type
, struct inode
*(*get_inode
)
875 (struct super_block
*sb
, u64 ino
, u32 gen
))
877 struct inode
*inode
= NULL
;
883 case FILEID_INO32_GEN
:
884 case FILEID_INO32_GEN_PARENT
:
885 inode
= get_inode(sb
, fid
->i32
.ino
, fid
->i32
.gen
);
889 return d_obtain_alias(inode
);
891 EXPORT_SYMBOL_GPL(generic_fh_to_dentry
);
894 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
895 * @sb: filesystem to do the file handle conversion on
896 * @fid: file handle to convert
897 * @fh_len: length of the file handle in bytes
898 * @fh_type: type of file handle
899 * @get_inode: filesystem callback to retrieve inode
901 * This function decodes @fid as long as it has one of the well-known
902 * Linux filehandle types and calls @get_inode on it to retrieve the
903 * inode for the _parent_ object specified in the file handle if it
904 * is specified in the file handle, or NULL otherwise.
906 struct dentry
*generic_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
907 int fh_len
, int fh_type
, struct inode
*(*get_inode
)
908 (struct super_block
*sb
, u64 ino
, u32 gen
))
910 struct inode
*inode
= NULL
;
916 case FILEID_INO32_GEN_PARENT
:
917 inode
= get_inode(sb
, fid
->i32
.parent_ino
,
918 (fh_len
> 3 ? fid
->i32
.parent_gen
: 0));
922 return d_obtain_alias(inode
);
924 EXPORT_SYMBOL_GPL(generic_fh_to_parent
);
927 * __generic_file_fsync - generic fsync implementation for simple filesystems
929 * @file: file to synchronize
930 * @start: start offset in bytes
931 * @end: end offset in bytes (inclusive)
932 * @datasync: only synchronize essential metadata if true
934 * This is a generic implementation of the fsync method for simple
935 * filesystems which track all non-inode metadata in the buffers list
936 * hanging off the address_space structure.
938 int __generic_file_fsync(struct file
*file
, loff_t start
, loff_t end
,
941 struct inode
*inode
= file
->f_mapping
->host
;
945 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
949 mutex_lock(&inode
->i_mutex
);
950 ret
= sync_mapping_buffers(inode
->i_mapping
);
951 if (!(inode
->i_state
& I_DIRTY
))
953 if (datasync
&& !(inode
->i_state
& I_DIRTY_DATASYNC
))
956 err
= sync_inode_metadata(inode
, 1);
961 mutex_unlock(&inode
->i_mutex
);
964 EXPORT_SYMBOL(__generic_file_fsync
);
967 * generic_file_fsync - generic fsync implementation for simple filesystems
969 * @file: file to synchronize
970 * @start: start offset in bytes
971 * @end: end offset in bytes (inclusive)
972 * @datasync: only synchronize essential metadata if true
976 int generic_file_fsync(struct file
*file
, loff_t start
, loff_t end
,
979 struct inode
*inode
= file
->f_mapping
->host
;
982 err
= __generic_file_fsync(file
, start
, end
, datasync
);
985 return blkdev_issue_flush(inode
->i_sb
->s_bdev
, GFP_KERNEL
, NULL
);
987 EXPORT_SYMBOL(generic_file_fsync
);
990 * generic_check_addressable - Check addressability of file system
991 * @blocksize_bits: log of file system block size
992 * @num_blocks: number of blocks in file system
994 * Determine whether a file system with @num_blocks blocks (and a
995 * block size of 2**@blocksize_bits) is addressable by the sector_t
996 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
998 int generic_check_addressable(unsigned blocksize_bits
, u64 num_blocks
)
1000 u64 last_fs_block
= num_blocks
- 1;
1002 last_fs_block
>> (PAGE_CACHE_SHIFT
- blocksize_bits
);
1004 if (unlikely(num_blocks
== 0))
1007 if ((blocksize_bits
< 9) || (blocksize_bits
> PAGE_CACHE_SHIFT
))
1010 if ((last_fs_block
> (sector_t
)(~0ULL) >> (blocksize_bits
- 9)) ||
1011 (last_fs_page
> (pgoff_t
)(~0ULL))) {
1016 EXPORT_SYMBOL(generic_check_addressable
);
1019 * No-op implementation of ->fsync for in-memory filesystems.
1021 int noop_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
1025 EXPORT_SYMBOL(noop_fsync
);
1027 void kfree_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
1030 char *s
= nd_get_link(nd
);
1034 EXPORT_SYMBOL(kfree_put_link
);
1037 * nop .set_page_dirty method so that people can use .page_mkwrite on
1040 static int anon_set_page_dirty(struct page
*page
)
1046 * A single inode exists for all anon_inode files. Contrary to pipes,
1047 * anon_inode inodes have no associated per-instance data, so we need
1048 * only allocate one of them.
1050 struct inode
*alloc_anon_inode(struct super_block
*s
)
1052 static const struct address_space_operations anon_aops
= {
1053 .set_page_dirty
= anon_set_page_dirty
,
1055 struct inode
*inode
= new_inode_pseudo(s
);
1058 return ERR_PTR(-ENOMEM
);
1060 inode
->i_ino
= get_next_ino();
1061 inode
->i_mapping
->a_ops
= &anon_aops
;
1064 * Mark the inode dirty from the very beginning,
1065 * that way it will never be moved to the dirty
1066 * list because mark_inode_dirty() will think
1067 * that it already _is_ on the dirty list.
1069 inode
->i_state
= I_DIRTY
;
1070 inode
->i_mode
= S_IRUSR
| S_IWUSR
;
1071 inode
->i_uid
= current_fsuid();
1072 inode
->i_gid
= current_fsgid();
1073 inode
->i_flags
|= S_PRIVATE
;
1074 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1077 EXPORT_SYMBOL(alloc_anon_inode
);
1081 * Operations for a permanently empty directory.
1083 static struct dentry
*empty_dir_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
1085 return ERR_PTR(-ENOENT
);
1088 static int empty_dir_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1091 struct inode
*inode
= d_inode(dentry
);
1092 generic_fillattr(inode
, stat
);
1096 static int empty_dir_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1101 static int empty_dir_setxattr(struct dentry
*dentry
, const char *name
,
1102 const void *value
, size_t size
, int flags
)
1107 static ssize_t
empty_dir_getxattr(struct dentry
*dentry
, const char *name
,
1108 void *value
, size_t size
)
1113 static int empty_dir_removexattr(struct dentry
*dentry
, const char *name
)
1118 static ssize_t
empty_dir_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
1123 static const struct inode_operations empty_dir_inode_operations
= {
1124 .lookup
= empty_dir_lookup
,
1125 .permission
= generic_permission
,
1126 .setattr
= empty_dir_setattr
,
1127 .getattr
= empty_dir_getattr
,
1128 .setxattr
= empty_dir_setxattr
,
1129 .getxattr
= empty_dir_getxattr
,
1130 .removexattr
= empty_dir_removexattr
,
1131 .listxattr
= empty_dir_listxattr
,
1134 static loff_t
empty_dir_llseek(struct file
*file
, loff_t offset
, int whence
)
1136 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1137 return generic_file_llseek_size(file
, offset
, whence
, 2, 2);
1140 static int empty_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
1142 dir_emit_dots(file
, ctx
);
1146 static const struct file_operations empty_dir_operations
= {
1147 .llseek
= empty_dir_llseek
,
1148 .read
= generic_read_dir
,
1149 .iterate
= empty_dir_readdir
,
1150 .fsync
= noop_fsync
,
1154 void make_empty_dir_inode(struct inode
*inode
)
1156 set_nlink(inode
, 2);
1157 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
1158 inode
->i_uid
= GLOBAL_ROOT_UID
;
1159 inode
->i_gid
= GLOBAL_ROOT_GID
;
1162 inode
->i_blkbits
= PAGE_SHIFT
;
1163 inode
->i_blocks
= 0;
1165 inode
->i_op
= &empty_dir_inode_operations
;
1166 inode
->i_fop
= &empty_dir_operations
;
1169 bool is_empty_dir_inode(struct inode
*inode
)
1171 return (inode
->i_fop
== &empty_dir_operations
) &&
1172 (inode
->i_op
== &empty_dir_inode_operations
);