1 // SPDX-License-Identifier: GPL-2.0-only
4 * Library for filesystems writers.
7 #include <linux/blkdev.h>
8 #include <linux/export.h>
9 #include <linux/pagemap.h>
10 #include <linux/slab.h>
11 #include <linux/cred.h>
12 #include <linux/mount.h>
13 #include <linux/vfs.h>
14 #include <linux/quotaops.h>
15 #include <linux/mutex.h>
16 #include <linux/namei.h>
17 #include <linux/exportfs.h>
18 #include <linux/writeback.h>
19 #include <linux/buffer_head.h> /* sync_mapping_buffers */
20 #include <linux/fs_context.h>
21 #include <linux/pseudo_fs.h>
23 #include <linux/uaccess.h>
27 int simple_getattr(const struct path *path, struct kstat *stat,
28 u32 request_mask, unsigned int query_flags)
30 struct inode *inode = d_inode(path->dentry);
31 generic_fillattr(inode, stat);
32 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
35 EXPORT_SYMBOL(simple_getattr);
37 int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
39 buf->f_type = dentry->d_sb->s_magic;
40 buf->f_bsize = PAGE_SIZE;
41 buf->f_namelen = NAME_MAX;
44 EXPORT_SYMBOL(simple_statfs);
47 * Retaining negative dentries for an in-memory filesystem just wastes
48 * memory and lookup time: arrange for them to be deleted immediately.
50 int always_delete_dentry(const struct dentry *dentry)
54 EXPORT_SYMBOL(always_delete_dentry);
56 const struct dentry_operations simple_dentry_operations = {
57 .d_delete = always_delete_dentry,
59 EXPORT_SYMBOL(simple_dentry_operations);
62 * Lookup the data. This is trivial - if the dentry didn't already
63 * exist, we know it is negative. Set d_op to delete negative dentries.
65 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
67 if (dentry->d_name.len > NAME_MAX)
68 return ERR_PTR(-ENAMETOOLONG);
69 if (!dentry->d_sb->s_d_op)
70 d_set_d_op(dentry, &simple_dentry_operations);
74 EXPORT_SYMBOL(simple_lookup);
76 int dcache_dir_open(struct inode *inode, struct file *file)
78 file->private_data = d_alloc_cursor(file->f_path.dentry);
80 return file->private_data ? 0 : -ENOMEM;
82 EXPORT_SYMBOL(dcache_dir_open);
84 int dcache_dir_close(struct inode *inode, struct file *file)
86 dput(file->private_data);
89 EXPORT_SYMBOL(dcache_dir_close);
91 /* parent is locked at least shared */
92 static struct dentry *next_positive(struct dentry *parent,
93 struct list_head *from,
96 unsigned *seq = &parent->d_inode->i_dir_seq, n;
105 n = smp_load_acquire(seq) & ~1;
108 for (p = from->next; p != &parent->d_subdirs; p = p->next) {
109 struct dentry *d = list_entry(p, struct dentry, d_child);
110 if (!simple_positive(d)) {
120 if (unlikely(*seq != n))
126 static void move_cursor(struct dentry *cursor, struct list_head *after)
128 struct dentry *parent = cursor->d_parent;
129 unsigned n, *seq = &parent->d_inode->i_dir_seq;
130 spin_lock(&parent->d_lock);
133 if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
137 __list_del(cursor->d_child.prev, cursor->d_child.next);
139 list_add(&cursor->d_child, after);
141 list_add_tail(&cursor->d_child, &parent->d_subdirs);
142 smp_store_release(seq, n + 2);
143 spin_unlock(&parent->d_lock);
146 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
148 struct dentry *dentry = file->f_path.dentry;
151 offset += file->f_pos;
160 if (offset != file->f_pos) {
161 file->f_pos = offset;
162 if (file->f_pos >= 2) {
163 struct dentry *cursor = file->private_data;
165 loff_t n = file->f_pos - 2;
167 inode_lock_shared(dentry->d_inode);
168 to = next_positive(dentry, &dentry->d_subdirs, n);
169 move_cursor(cursor, to ? &to->d_child : NULL);
170 inode_unlock_shared(dentry->d_inode);
175 EXPORT_SYMBOL(dcache_dir_lseek);
177 /* Relationship between i_mode and the DT_xxx types */
178 static inline unsigned char dt_type(struct inode *inode)
180 return (inode->i_mode >> 12) & 15;
184 * Directory is locked and all positive dentries in it are safe, since
185 * for ramfs-type trees they can't go away without unlink() or rmdir(),
186 * both impossible due to the lock on directory.
189 int dcache_readdir(struct file *file, struct dir_context *ctx)
191 struct dentry *dentry = file->f_path.dentry;
192 struct dentry *cursor = file->private_data;
193 struct list_head *p = &cursor->d_child;
197 if (!dir_emit_dots(file, ctx))
201 p = &dentry->d_subdirs;
202 while ((next = next_positive(dentry, p, 1)) != NULL) {
203 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
204 d_inode(next)->i_ino, dt_type(d_inode(next))))
211 move_cursor(cursor, p);
214 EXPORT_SYMBOL(dcache_readdir);
216 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
220 EXPORT_SYMBOL(generic_read_dir);
222 const struct file_operations simple_dir_operations = {
223 .open = dcache_dir_open,
224 .release = dcache_dir_close,
225 .llseek = dcache_dir_lseek,
226 .read = generic_read_dir,
227 .iterate_shared = dcache_readdir,
230 EXPORT_SYMBOL(simple_dir_operations);
232 const struct inode_operations simple_dir_inode_operations = {
233 .lookup = simple_lookup,
235 EXPORT_SYMBOL(simple_dir_inode_operations);
237 static const struct super_operations simple_super_operations = {
238 .statfs = simple_statfs,
241 static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
243 struct pseudo_fs_context *ctx = fc->fs_private;
246 s->s_maxbytes = MAX_LFS_FILESIZE;
247 s->s_blocksize = PAGE_SIZE;
248 s->s_blocksize_bits = PAGE_SHIFT;
249 s->s_magic = ctx->magic;
250 s->s_op = ctx->ops ?: &simple_super_operations;
251 s->s_xattr = ctx->xattr;
258 * since this is the first inode, make it number 1. New inodes created
259 * after this must take care not to collide with it (by passing
260 * max_reserved of 1 to iunique).
263 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
264 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
265 s->s_root = d_make_root(root);
268 s->s_d_op = ctx->dops;
272 static int pseudo_fs_get_tree(struct fs_context *fc)
274 return get_tree_nodev(fc, pseudo_fs_fill_super);
277 static void pseudo_fs_free(struct fs_context *fc)
279 kfree(fc->fs_private);
282 static const struct fs_context_operations pseudo_fs_context_ops = {
283 .free = pseudo_fs_free,
284 .get_tree = pseudo_fs_get_tree,
288 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
289 * will never be mountable)
291 struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
294 struct pseudo_fs_context *ctx;
296 ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
299 fc->fs_private = ctx;
300 fc->ops = &pseudo_fs_context_ops;
301 fc->sb_flags |= SB_NOUSER;
306 EXPORT_SYMBOL(init_pseudo);
308 int simple_open(struct inode *inode, struct file *file)
310 if (inode->i_private)
311 file->private_data = inode->i_private;
314 EXPORT_SYMBOL(simple_open);
316 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
318 struct inode *inode = d_inode(old_dentry);
320 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
324 d_instantiate(dentry, inode);
327 EXPORT_SYMBOL(simple_link);
329 int simple_empty(struct dentry *dentry)
331 struct dentry *child;
334 spin_lock(&dentry->d_lock);
335 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
336 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
337 if (simple_positive(child)) {
338 spin_unlock(&child->d_lock);
341 spin_unlock(&child->d_lock);
345 spin_unlock(&dentry->d_lock);
348 EXPORT_SYMBOL(simple_empty);
350 int simple_unlink(struct inode *dir, struct dentry *dentry)
352 struct inode *inode = d_inode(dentry);
354 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
359 EXPORT_SYMBOL(simple_unlink);
361 int simple_rmdir(struct inode *dir, struct dentry *dentry)
363 if (!simple_empty(dentry))
366 drop_nlink(d_inode(dentry));
367 simple_unlink(dir, dentry);
371 EXPORT_SYMBOL(simple_rmdir);
373 int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
374 struct inode *new_dir, struct dentry *new_dentry,
377 struct inode *inode = d_inode(old_dentry);
378 int they_are_dirs = d_is_dir(old_dentry);
380 if (flags & ~RENAME_NOREPLACE)
383 if (!simple_empty(new_dentry))
386 if (d_really_is_positive(new_dentry)) {
387 simple_unlink(new_dir, new_dentry);
389 drop_nlink(d_inode(new_dentry));
392 } else if (they_are_dirs) {
397 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
398 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
402 EXPORT_SYMBOL(simple_rename);
405 * simple_setattr - setattr for simple filesystem
407 * @iattr: iattr structure
409 * Returns 0 on success, -error on failure.
411 * simple_setattr is a simple ->setattr implementation without a proper
412 * implementation of size changes.
414 * It can either be used for in-memory filesystems or special files
415 * on simple regular filesystems. Anything that needs to change on-disk
416 * or wire state on size changes needs its own setattr method.
418 int simple_setattr(struct dentry *dentry, struct iattr *iattr)
420 struct inode *inode = d_inode(dentry);
423 error = setattr_prepare(dentry, iattr);
427 if (iattr->ia_valid & ATTR_SIZE)
428 truncate_setsize(inode, iattr->ia_size);
429 setattr_copy(inode, iattr);
430 mark_inode_dirty(inode);
433 EXPORT_SYMBOL(simple_setattr);
435 int simple_readpage(struct file *file, struct page *page)
437 clear_highpage(page);
438 flush_dcache_page(page);
439 SetPageUptodate(page);
443 EXPORT_SYMBOL(simple_readpage);
445 int simple_write_begin(struct file *file, struct address_space *mapping,
446 loff_t pos, unsigned len, unsigned flags,
447 struct page **pagep, void **fsdata)
452 index = pos >> PAGE_SHIFT;
454 page = grab_cache_page_write_begin(mapping, index, flags);
460 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
461 unsigned from = pos & (PAGE_SIZE - 1);
463 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
467 EXPORT_SYMBOL(simple_write_begin);
470 * simple_write_end - .write_end helper for non-block-device FSes
471 * @available: See .write_end of address_space_operations
480 * simple_write_end does the minimum needed for updating a page after writing is
481 * done. It has the same API signature as the .write_end of
482 * address_space_operations vector. So it can just be set onto .write_end for
483 * FSes that don't need any other processing. i_mutex is assumed to be held.
484 * Block based filesystems should use generic_write_end().
485 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
486 * is not called, so a filesystem that actually does store data in .write_inode
487 * should extend on what's done here with a call to mark_inode_dirty() in the
488 * case that i_size has changed.
490 * Use *ONLY* with simple_readpage()
492 int simple_write_end(struct file *file, struct address_space *mapping,
493 loff_t pos, unsigned len, unsigned copied,
494 struct page *page, void *fsdata)
496 struct inode *inode = page->mapping->host;
497 loff_t last_pos = pos + copied;
499 /* zero the stale part of the page if we did a short copy */
500 if (!PageUptodate(page)) {
502 unsigned from = pos & (PAGE_SIZE - 1);
504 zero_user(page, from + copied, len - copied);
506 SetPageUptodate(page);
509 * No need to use i_size_read() here, the i_size
510 * cannot change under us because we hold the i_mutex.
512 if (last_pos > inode->i_size)
513 i_size_write(inode, last_pos);
515 set_page_dirty(page);
521 EXPORT_SYMBOL(simple_write_end);
524 * the inodes created here are not hashed. If you use iunique to generate
525 * unique inode values later for this filesystem, then you must take care
526 * to pass it an appropriate max_reserved value to avoid collisions.
528 int simple_fill_super(struct super_block *s, unsigned long magic,
529 const struct tree_descr *files)
533 struct dentry *dentry;
536 s->s_blocksize = PAGE_SIZE;
537 s->s_blocksize_bits = PAGE_SHIFT;
539 s->s_op = &simple_super_operations;
542 inode = new_inode(s);
546 * because the root inode is 1, the files array must not contain an
550 inode->i_mode = S_IFDIR | 0755;
551 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
552 inode->i_op = &simple_dir_inode_operations;
553 inode->i_fop = &simple_dir_operations;
555 root = d_make_root(inode);
558 for (i = 0; !files->name || files->name[0]; i++, files++) {
562 /* warn if it tries to conflict with the root inode */
563 if (unlikely(i == 1))
564 printk(KERN_WARNING "%s: %s passed in a files array"
565 "with an index of 1!\n", __func__,
568 dentry = d_alloc_name(root, files->name);
571 inode = new_inode(s);
576 inode->i_mode = S_IFREG | files->mode;
577 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
578 inode->i_fop = files->ops;
580 d_add(dentry, inode);
586 shrink_dcache_parent(root);
590 EXPORT_SYMBOL(simple_fill_super);
592 static DEFINE_SPINLOCK(pin_fs_lock);
594 int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
596 struct vfsmount *mnt = NULL;
597 spin_lock(&pin_fs_lock);
598 if (unlikely(!*mount)) {
599 spin_unlock(&pin_fs_lock);
600 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
603 spin_lock(&pin_fs_lock);
609 spin_unlock(&pin_fs_lock);
613 EXPORT_SYMBOL(simple_pin_fs);
615 void simple_release_fs(struct vfsmount **mount, int *count)
617 struct vfsmount *mnt;
618 spin_lock(&pin_fs_lock);
622 spin_unlock(&pin_fs_lock);
625 EXPORT_SYMBOL(simple_release_fs);
628 * simple_read_from_buffer - copy data from the buffer to user space
629 * @to: the user space buffer to read to
630 * @count: the maximum number of bytes to read
631 * @ppos: the current position in the buffer
632 * @from: the buffer to read from
633 * @available: the size of the buffer
635 * The simple_read_from_buffer() function reads up to @count bytes from the
636 * buffer @from at offset @ppos into the user space address starting at @to.
638 * On success, the number of bytes read is returned and the offset @ppos is
639 * advanced by this number, or negative value is returned on error.
641 ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
642 const void *from, size_t available)
649 if (pos >= available || !count)
651 if (count > available - pos)
652 count = available - pos;
653 ret = copy_to_user(to, from + pos, count);
660 EXPORT_SYMBOL(simple_read_from_buffer);
663 * simple_write_to_buffer - copy data from user space to the buffer
664 * @to: the buffer to write to
665 * @available: the size of the buffer
666 * @ppos: the current position in the buffer
667 * @from: the user space buffer to read from
668 * @count: the maximum number of bytes to read
670 * The simple_write_to_buffer() function reads up to @count bytes from the user
671 * space address starting at @from into the buffer @to at offset @ppos.
673 * On success, the number of bytes written is returned and the offset @ppos is
674 * advanced by this number, or negative value is returned on error.
676 ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
677 const void __user *from, size_t count)
684 if (pos >= available || !count)
686 if (count > available - pos)
687 count = available - pos;
688 res = copy_from_user(to + pos, from, count);
695 EXPORT_SYMBOL(simple_write_to_buffer);
698 * memory_read_from_buffer - copy data from the buffer
699 * @to: the kernel space buffer to read to
700 * @count: the maximum number of bytes to read
701 * @ppos: the current position in the buffer
702 * @from: the buffer to read from
703 * @available: the size of the buffer
705 * The memory_read_from_buffer() function reads up to @count bytes from the
706 * buffer @from at offset @ppos into the kernel space address starting at @to.
708 * On success, the number of bytes read is returned and the offset @ppos is
709 * advanced by this number, or negative value is returned on error.
711 ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
712 const void *from, size_t available)
718 if (pos >= available)
720 if (count > available - pos)
721 count = available - pos;
722 memcpy(to, from + pos, count);
727 EXPORT_SYMBOL(memory_read_from_buffer);
730 * Transaction based IO.
731 * The file expects a single write which triggers the transaction, and then
732 * possibly a read which collects the result - which is stored in a
736 void simple_transaction_set(struct file *file, size_t n)
738 struct simple_transaction_argresp *ar = file->private_data;
740 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
743 * The barrier ensures that ar->size will really remain zero until
744 * ar->data is ready for reading.
749 EXPORT_SYMBOL(simple_transaction_set);
751 char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
753 struct simple_transaction_argresp *ar;
754 static DEFINE_SPINLOCK(simple_transaction_lock);
756 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
757 return ERR_PTR(-EFBIG);
759 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
761 return ERR_PTR(-ENOMEM);
763 spin_lock(&simple_transaction_lock);
765 /* only one write allowed per open */
766 if (file->private_data) {
767 spin_unlock(&simple_transaction_lock);
768 free_page((unsigned long)ar);
769 return ERR_PTR(-EBUSY);
772 file->private_data = ar;
774 spin_unlock(&simple_transaction_lock);
776 if (copy_from_user(ar->data, buf, size))
777 return ERR_PTR(-EFAULT);
781 EXPORT_SYMBOL(simple_transaction_get);
783 ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
785 struct simple_transaction_argresp *ar = file->private_data;
789 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
791 EXPORT_SYMBOL(simple_transaction_read);
793 int simple_transaction_release(struct inode *inode, struct file *file)
795 free_page((unsigned long)file->private_data);
798 EXPORT_SYMBOL(simple_transaction_release);
800 /* Simple attribute files */
803 int (*get)(void *, u64 *);
804 int (*set)(void *, u64);
805 char get_buf[24]; /* enough to store a u64 and "\n\0" */
808 const char *fmt; /* format for read operation */
809 struct mutex mutex; /* protects access to these buffers */
812 /* simple_attr_open is called by an actual attribute open file operation
813 * to set the attribute specific access operations. */
814 int simple_attr_open(struct inode *inode, struct file *file,
815 int (*get)(void *, u64 *), int (*set)(void *, u64),
818 struct simple_attr *attr;
820 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
826 attr->data = inode->i_private;
828 mutex_init(&attr->mutex);
830 file->private_data = attr;
832 return nonseekable_open(inode, file);
834 EXPORT_SYMBOL_GPL(simple_attr_open);
836 int simple_attr_release(struct inode *inode, struct file *file)
838 kfree(file->private_data);
841 EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
843 /* read from the buffer that is filled with the get function */
844 ssize_t simple_attr_read(struct file *file, char __user *buf,
845 size_t len, loff_t *ppos)
847 struct simple_attr *attr;
851 attr = file->private_data;
856 ret = mutex_lock_interruptible(&attr->mutex);
860 if (*ppos) { /* continued read */
861 size = strlen(attr->get_buf);
862 } else { /* first read */
864 ret = attr->get(attr->data, &val);
868 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
869 attr->fmt, (unsigned long long)val);
872 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
874 mutex_unlock(&attr->mutex);
877 EXPORT_SYMBOL_GPL(simple_attr_read);
879 /* interpret the buffer as a number to call the set function with */
880 ssize_t simple_attr_write(struct file *file, const char __user *buf,
881 size_t len, loff_t *ppos)
883 struct simple_attr *attr;
888 attr = file->private_data;
892 ret = mutex_lock_interruptible(&attr->mutex);
897 size = min(sizeof(attr->set_buf) - 1, len);
898 if (copy_from_user(attr->set_buf, buf, size))
901 attr->set_buf[size] = '\0';
902 val = simple_strtoll(attr->set_buf, NULL, 0);
903 ret = attr->set(attr->data, val);
905 ret = len; /* on success, claim we got the whole input */
907 mutex_unlock(&attr->mutex);
910 EXPORT_SYMBOL_GPL(simple_attr_write);
913 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
914 * @sb: filesystem to do the file handle conversion on
915 * @fid: file handle to convert
916 * @fh_len: length of the file handle in bytes
917 * @fh_type: type of file handle
918 * @get_inode: filesystem callback to retrieve inode
920 * This function decodes @fid as long as it has one of the well-known
921 * Linux filehandle types and calls @get_inode on it to retrieve the
922 * inode for the object specified in the file handle.
924 struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
925 int fh_len, int fh_type, struct inode *(*get_inode)
926 (struct super_block *sb, u64 ino, u32 gen))
928 struct inode *inode = NULL;
934 case FILEID_INO32_GEN:
935 case FILEID_INO32_GEN_PARENT:
936 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
940 return d_obtain_alias(inode);
942 EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
945 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
946 * @sb: filesystem to do the file handle conversion on
947 * @fid: file handle to convert
948 * @fh_len: length of the file handle in bytes
949 * @fh_type: type of file handle
950 * @get_inode: filesystem callback to retrieve inode
952 * This function decodes @fid as long as it has one of the well-known
953 * Linux filehandle types and calls @get_inode on it to retrieve the
954 * inode for the _parent_ object specified in the file handle if it
955 * is specified in the file handle, or NULL otherwise.
957 struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
958 int fh_len, int fh_type, struct inode *(*get_inode)
959 (struct super_block *sb, u64 ino, u32 gen))
961 struct inode *inode = NULL;
967 case FILEID_INO32_GEN_PARENT:
968 inode = get_inode(sb, fid->i32.parent_ino,
969 (fh_len > 3 ? fid->i32.parent_gen : 0));
973 return d_obtain_alias(inode);
975 EXPORT_SYMBOL_GPL(generic_fh_to_parent);
978 * __generic_file_fsync - generic fsync implementation for simple filesystems
980 * @file: file to synchronize
981 * @start: start offset in bytes
982 * @end: end offset in bytes (inclusive)
983 * @datasync: only synchronize essential metadata if true
985 * This is a generic implementation of the fsync method for simple
986 * filesystems which track all non-inode metadata in the buffers list
987 * hanging off the address_space structure.
989 int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
992 struct inode *inode = file->f_mapping->host;
996 err = file_write_and_wait_range(file, start, end);
1001 ret = sync_mapping_buffers(inode->i_mapping);
1002 if (!(inode->i_state & I_DIRTY_ALL))
1004 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
1007 err = sync_inode_metadata(inode, 1);
1012 inode_unlock(inode);
1013 /* check and advance again to catch errors after syncing out buffers */
1014 err = file_check_and_advance_wb_err(file);
1019 EXPORT_SYMBOL(__generic_file_fsync);
1022 * generic_file_fsync - generic fsync implementation for simple filesystems
1024 * @file: file to synchronize
1025 * @start: start offset in bytes
1026 * @end: end offset in bytes (inclusive)
1027 * @datasync: only synchronize essential metadata if true
1031 int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1034 struct inode *inode = file->f_mapping->host;
1037 err = __generic_file_fsync(file, start, end, datasync);
1040 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1042 EXPORT_SYMBOL(generic_file_fsync);
1045 * generic_check_addressable - Check addressability of file system
1046 * @blocksize_bits: log of file system block size
1047 * @num_blocks: number of blocks in file system
1049 * Determine whether a file system with @num_blocks blocks (and a
1050 * block size of 2**@blocksize_bits) is addressable by the sector_t
1051 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
1053 int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1055 u64 last_fs_block = num_blocks - 1;
1057 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
1059 if (unlikely(num_blocks == 0))
1062 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
1065 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1066 (last_fs_page > (pgoff_t)(~0ULL))) {
1071 EXPORT_SYMBOL(generic_check_addressable);
1074 * No-op implementation of ->fsync for in-memory filesystems.
1076 int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1080 EXPORT_SYMBOL(noop_fsync);
1082 int noop_set_page_dirty(struct page *page)
1085 * Unlike __set_page_dirty_no_writeback that handles dirty page
1086 * tracking in the page object, dax does all dirty tracking in
1087 * the inode address_space in response to mkwrite faults. In the
1088 * dax case we only need to worry about potentially dirty CPU
1089 * caches, not dirty page cache pages to write back.
1091 * This callback is defined to prevent fallback to
1092 * __set_page_dirty_buffers() in set_page_dirty().
1096 EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1098 void noop_invalidatepage(struct page *page, unsigned int offset,
1099 unsigned int length)
1102 * There is no page cache to invalidate in the dax case, however
1103 * we need this callback defined to prevent falling back to
1104 * block_invalidatepage() in do_invalidatepage().
1107 EXPORT_SYMBOL_GPL(noop_invalidatepage);
1109 ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1112 * iomap based filesystems support direct I/O without need for
1113 * this callback. However, it still needs to be set in
1114 * inode->a_ops so that open/fcntl know that direct I/O is
1115 * generally supported.
1119 EXPORT_SYMBOL_GPL(noop_direct_IO);
1121 /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1122 void kfree_link(void *p)
1126 EXPORT_SYMBOL(kfree_link);
1129 * nop .set_page_dirty method so that people can use .page_mkwrite on
1132 static int anon_set_page_dirty(struct page *page)
1138 * A single inode exists for all anon_inode files. Contrary to pipes,
1139 * anon_inode inodes have no associated per-instance data, so we need
1140 * only allocate one of them.
1142 struct inode *alloc_anon_inode(struct super_block *s)
1144 static const struct address_space_operations anon_aops = {
1145 .set_page_dirty = anon_set_page_dirty,
1147 struct inode *inode = new_inode_pseudo(s);
1150 return ERR_PTR(-ENOMEM);
1152 inode->i_ino = get_next_ino();
1153 inode->i_mapping->a_ops = &anon_aops;
1156 * Mark the inode dirty from the very beginning,
1157 * that way it will never be moved to the dirty
1158 * list because mark_inode_dirty() will think
1159 * that it already _is_ on the dirty list.
1161 inode->i_state = I_DIRTY;
1162 inode->i_mode = S_IRUSR | S_IWUSR;
1163 inode->i_uid = current_fsuid();
1164 inode->i_gid = current_fsgid();
1165 inode->i_flags |= S_PRIVATE;
1166 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1169 EXPORT_SYMBOL(alloc_anon_inode);
1172 * simple_nosetlease - generic helper for prohibiting leases
1173 * @filp: file pointer
1174 * @arg: type of lease to obtain
1175 * @flp: new lease supplied for insertion
1176 * @priv: private data for lm_setup operation
1178 * Generic helper for filesystems that do not wish to allow leases to be set.
1179 * All arguments are ignored and it just returns -EINVAL.
1182 simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1187 EXPORT_SYMBOL(simple_nosetlease);
1190 * simple_get_link - generic helper to get the target of "fast" symlinks
1191 * @dentry: not used here
1192 * @inode: the symlink inode
1193 * @done: not used here
1195 * Generic helper for filesystems to use for symlink inodes where a pointer to
1196 * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
1197 * since as an optimization the path lookup code uses any non-NULL ->i_link
1198 * directly, without calling ->get_link(). But ->get_link() still must be set,
1199 * to mark the inode_operations as being for a symlink.
1201 * Return: the symlink target
1203 const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1204 struct delayed_call *done)
1206 return inode->i_link;
1208 EXPORT_SYMBOL(simple_get_link);
1210 const struct inode_operations simple_symlink_inode_operations = {
1211 .get_link = simple_get_link,
1213 EXPORT_SYMBOL(simple_symlink_inode_operations);
1216 * Operations for a permanently empty directory.
1218 static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1220 return ERR_PTR(-ENOENT);
1223 static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1224 u32 request_mask, unsigned int query_flags)
1226 struct inode *inode = d_inode(path->dentry);
1227 generic_fillattr(inode, stat);
1231 static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1236 static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1241 static const struct inode_operations empty_dir_inode_operations = {
1242 .lookup = empty_dir_lookup,
1243 .permission = generic_permission,
1244 .setattr = empty_dir_setattr,
1245 .getattr = empty_dir_getattr,
1246 .listxattr = empty_dir_listxattr,
1249 static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1251 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1252 return generic_file_llseek_size(file, offset, whence, 2, 2);
1255 static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1257 dir_emit_dots(file, ctx);
1261 static const struct file_operations empty_dir_operations = {
1262 .llseek = empty_dir_llseek,
1263 .read = generic_read_dir,
1264 .iterate_shared = empty_dir_readdir,
1265 .fsync = noop_fsync,
1269 void make_empty_dir_inode(struct inode *inode)
1271 set_nlink(inode, 2);
1272 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1273 inode->i_uid = GLOBAL_ROOT_UID;
1274 inode->i_gid = GLOBAL_ROOT_GID;
1277 inode->i_blkbits = PAGE_SHIFT;
1278 inode->i_blocks = 0;
1280 inode->i_op = &empty_dir_inode_operations;
1281 inode->i_opflags &= ~IOP_XATTR;
1282 inode->i_fop = &empty_dir_operations;
1285 bool is_empty_dir_inode(struct inode *inode)
1287 return (inode->i_fop == &empty_dir_operations) &&
1288 (inode->i_op == &empty_dir_inode_operations);