ALSA: pcm: Fix missing check of the new non-cached buffer type
[linux-2.6-block.git] / fs / libfs.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * fs/libfs.c
4 * Library for filesystems writers.
5 */
6
ac13a829 7#include <linux/blkdev.h>
630d9c47 8#include <linux/export.h>
1da177e4 9#include <linux/pagemap.h>
5a0e3ad6 10#include <linux/slab.h>
5b825c3a 11#include <linux/cred.h>
1da177e4
LT
12#include <linux/mount.h>
13#include <linux/vfs.h>
7bb46a67 14#include <linux/quotaops.h>
7cf34c76 15#include <linux/mutex.h>
87dc800b 16#include <linux/namei.h>
2596110a 17#include <linux/exportfs.h>
d5aacad5 18#include <linux/writeback.h>
ff01bb48 19#include <linux/buffer_head.h> /* sync_mapping_buffers */
31d6d5ce
DH
20#include <linux/fs_context.h>
21#include <linux/pseudo_fs.h>
7cf34c76 22
7c0f6ba6 23#include <linux/uaccess.h>
1da177e4 24
a4464dbc
AV
25#include "internal.h"
26
a528d35e
DH
27int simple_getattr(const struct path *path, struct kstat *stat,
28 u32 request_mask, unsigned int query_flags)
1da177e4 29{
a528d35e 30 struct inode *inode = d_inode(path->dentry);
1da177e4 31 generic_fillattr(inode, stat);
09cbfeaf 32 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
1da177e4
LT
33 return 0;
34}
12f38872 35EXPORT_SYMBOL(simple_getattr);
1da177e4 36
726c3342 37int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 38{
726c3342 39 buf->f_type = dentry->d_sb->s_magic;
09cbfeaf 40 buf->f_bsize = PAGE_SIZE;
1da177e4
LT
41 buf->f_namelen = NAME_MAX;
42 return 0;
43}
12f38872 44EXPORT_SYMBOL(simple_statfs);
1da177e4
LT
45
46/*
47 * Retaining negative dentries for an in-memory filesystem just wastes
48 * memory and lookup time: arrange for them to be deleted immediately.
49 */
b26d4cd3 50int always_delete_dentry(const struct dentry *dentry)
1da177e4
LT
51{
52 return 1;
53}
b26d4cd3
AV
54EXPORT_SYMBOL(always_delete_dentry);
55
56const struct dentry_operations simple_dentry_operations = {
57 .d_delete = always_delete_dentry,
58};
59EXPORT_SYMBOL(simple_dentry_operations);
1da177e4
LT
60
61/*
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.
64 */
00cd8dd3 65struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1da177e4 66{
1da177e4
LT
67 if (dentry->d_name.len > NAME_MAX)
68 return ERR_PTR(-ENAMETOOLONG);
74931da7
AV
69 if (!dentry->d_sb->s_d_op)
70 d_set_d_op(dentry, &simple_dentry_operations);
1da177e4
LT
71 d_add(dentry, NULL);
72 return NULL;
73}
12f38872 74EXPORT_SYMBOL(simple_lookup);
1da177e4 75
1da177e4
LT
76int dcache_dir_open(struct inode *inode, struct file *file)
77{
ba65dc5e 78 file->private_data = d_alloc_cursor(file->f_path.dentry);
1da177e4
LT
79
80 return file->private_data ? 0 : -ENOMEM;
81}
12f38872 82EXPORT_SYMBOL(dcache_dir_open);
1da177e4
LT
83
84int dcache_dir_close(struct inode *inode, struct file *file)
85{
86 dput(file->private_data);
87 return 0;
88}
12f38872 89EXPORT_SYMBOL(dcache_dir_close);
1da177e4 90
4f42c1b5
AV
91/* parent is locked at least shared */
92static struct dentry *next_positive(struct dentry *parent,
93 struct list_head *from,
94 int count)
95{
ebaaa80e
AV
96 unsigned *seq = &parent->d_inode->i_dir_seq, n;
97 struct dentry *res;
4f42c1b5 98 struct list_head *p;
ebaaa80e
AV
99 bool skipped;
100 int i;
101
102retry:
103 i = count;
104 skipped = false;
105 n = smp_load_acquire(seq) & ~1;
106 res = NULL;
107 rcu_read_lock();
4f42c1b5
AV
108 for (p = from->next; p != &parent->d_subdirs; p = p->next) {
109 struct dentry *d = list_entry(p, struct dentry, d_child);
ebaaa80e
AV
110 if (!simple_positive(d)) {
111 skipped = true;
112 } else if (!--i) {
4f42c1b5
AV
113 res = d;
114 break;
115 }
116 }
ebaaa80e
AV
117 rcu_read_unlock();
118 if (skipped) {
119 smp_rmb();
120 if (unlikely(*seq != n))
121 goto retry;
122 }
4f42c1b5
AV
123 return res;
124}
125
126static void move_cursor(struct dentry *cursor, struct list_head *after)
127{
128 struct dentry *parent = cursor->d_parent;
ebaaa80e 129 unsigned n, *seq = &parent->d_inode->i_dir_seq;
4f42c1b5 130 spin_lock(&parent->d_lock);
ebaaa80e
AV
131 for (;;) {
132 n = *seq;
133 if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
134 break;
135 cpu_relax();
136 }
4f42c1b5
AV
137 __list_del(cursor->d_child.prev, cursor->d_child.next);
138 if (after)
139 list_add(&cursor->d_child, after);
140 else
141 list_add_tail(&cursor->d_child, &parent->d_subdirs);
ebaaa80e 142 smp_store_release(seq, n + 2);
4f42c1b5
AV
143 spin_unlock(&parent->d_lock);
144}
145
965c8e59 146loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
1da177e4 147{
2fd6b7f5 148 struct dentry *dentry = file->f_path.dentry;
965c8e59 149 switch (whence) {
1da177e4
LT
150 case 1:
151 offset += file->f_pos;
0a4c9265 152 /* fall through */
1da177e4
LT
153 case 0:
154 if (offset >= 0)
155 break;
0a4c9265 156 /* fall through */
1da177e4 157 default:
1da177e4
LT
158 return -EINVAL;
159 }
160 if (offset != file->f_pos) {
161 file->f_pos = offset;
162 if (file->f_pos >= 2) {
1da177e4 163 struct dentry *cursor = file->private_data;
4f42c1b5 164 struct dentry *to;
1da177e4
LT
165 loff_t n = file->f_pos - 2;
166
274f5b04 167 inode_lock_shared(dentry->d_inode);
4f42c1b5
AV
168 to = next_positive(dentry, &dentry->d_subdirs, n);
169 move_cursor(cursor, to ? &to->d_child : NULL);
274f5b04 170 inode_unlock_shared(dentry->d_inode);
1da177e4
LT
171 }
172 }
1da177e4
LT
173 return offset;
174}
12f38872 175EXPORT_SYMBOL(dcache_dir_lseek);
1da177e4
LT
176
177/* Relationship between i_mode and the DT_xxx types */
178static inline unsigned char dt_type(struct inode *inode)
179{
180 return (inode->i_mode >> 12) & 15;
181}
182
183/*
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.
187 */
188
5f99f4e7 189int dcache_readdir(struct file *file, struct dir_context *ctx)
1da177e4 190{
5f99f4e7
AV
191 struct dentry *dentry = file->f_path.dentry;
192 struct dentry *cursor = file->private_data;
4f42c1b5
AV
193 struct list_head *p = &cursor->d_child;
194 struct dentry *next;
195 bool moved = false;
1da177e4 196
5f99f4e7
AV
197 if (!dir_emit_dots(file, ctx))
198 return 0;
5f99f4e7 199
4f42c1b5
AV
200 if (ctx->pos == 2)
201 p = &dentry->d_subdirs;
202 while ((next = next_positive(dentry, p, 1)) != NULL) {
5f99f4e7 203 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
dea655c2 204 d_inode(next)->i_ino, dt_type(d_inode(next))))
4f42c1b5
AV
205 break;
206 moved = true;
207 p = &next->d_child;
5f99f4e7 208 ctx->pos++;
1da177e4 209 }
4f42c1b5
AV
210 if (moved)
211 move_cursor(cursor, p);
1da177e4
LT
212 return 0;
213}
12f38872 214EXPORT_SYMBOL(dcache_readdir);
1da177e4
LT
215
216ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
217{
218 return -EISDIR;
219}
12f38872 220EXPORT_SYMBOL(generic_read_dir);
1da177e4 221
4b6f5d20 222const struct file_operations simple_dir_operations = {
1da177e4
LT
223 .open = dcache_dir_open,
224 .release = dcache_dir_close,
225 .llseek = dcache_dir_lseek,
226 .read = generic_read_dir,
4e82901c 227 .iterate_shared = dcache_readdir,
1b061d92 228 .fsync = noop_fsync,
1da177e4 229};
12f38872 230EXPORT_SYMBOL(simple_dir_operations);
1da177e4 231
92e1d5be 232const struct inode_operations simple_dir_inode_operations = {
1da177e4
LT
233 .lookup = simple_lookup,
234};
12f38872 235EXPORT_SYMBOL(simple_dir_inode_operations);
1da177e4 236
759b9775
HD
237static const struct super_operations simple_super_operations = {
238 .statfs = simple_statfs,
239};
240
db2c246a 241static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
1da177e4 242{
31d6d5ce 243 struct pseudo_fs_context *ctx = fc->fs_private;
1da177e4 244 struct inode *root;
1da177e4 245
89a4eb4b 246 s->s_maxbytes = MAX_LFS_FILESIZE;
3971e1a9
AN
247 s->s_blocksize = PAGE_SIZE;
248 s->s_blocksize_bits = PAGE_SHIFT;
8d9e46d8
AV
249 s->s_magic = ctx->magic;
250 s->s_op = ctx->ops ?: &simple_super_operations;
251 s->s_xattr = ctx->xattr;
1da177e4
LT
252 s->s_time_gran = 1;
253 root = new_inode(s);
254 if (!root)
db2c246a
DH
255 return -ENOMEM;
256
1a1c9bb4
JL
257 /*
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).
261 */
262 root->i_ino = 1;
1da177e4 263 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
078cd827 264 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
8d9e46d8
AV
265 s->s_root = d_make_root(root);
266 if (!s->s_root)
db2c246a 267 return -ENOMEM;
8d9e46d8 268 s->s_d_op = ctx->dops;
31d6d5ce 269 return 0;
db2c246a 270}
8d9e46d8 271
db2c246a
DH
272static int pseudo_fs_get_tree(struct fs_context *fc)
273{
2ac295d4 274 return get_tree_nodev(fc, pseudo_fs_fill_super);
31d6d5ce
DH
275}
276
277static void pseudo_fs_free(struct fs_context *fc)
278{
279 kfree(fc->fs_private);
280}
281
282static const struct fs_context_operations pseudo_fs_context_ops = {
283 .free = pseudo_fs_free,
284 .get_tree = pseudo_fs_get_tree,
285};
286
287/*
288 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
289 * will never be mountable)
290 */
291struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
292 unsigned long magic)
293{
294 struct pseudo_fs_context *ctx;
295
296 ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
297 if (likely(ctx)) {
298 ctx->magic = magic;
299 fc->fs_private = ctx;
300 fc->ops = &pseudo_fs_context_ops;
db2c246a
DH
301 fc->sb_flags |= SB_NOUSER;
302 fc->global = true;
1da177e4 303 }
31d6d5ce 304 return ctx;
1da177e4 305}
31d6d5ce 306EXPORT_SYMBOL(init_pseudo);
1da177e4 307
20955e89
SB
308int simple_open(struct inode *inode, struct file *file)
309{
310 if (inode->i_private)
311 file->private_data = inode->i_private;
312 return 0;
313}
12f38872 314EXPORT_SYMBOL(simple_open);
20955e89 315
1da177e4
LT
316int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
317{
dea655c2 318 struct inode *inode = d_inode(old_dentry);
1da177e4 319
078cd827 320 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
d8c76e6f 321 inc_nlink(inode);
7de9c6ee 322 ihold(inode);
1da177e4
LT
323 dget(dentry);
324 d_instantiate(dentry, inode);
325 return 0;
326}
12f38872 327EXPORT_SYMBOL(simple_link);
1da177e4 328
1da177e4
LT
329int simple_empty(struct dentry *dentry)
330{
331 struct dentry *child;
332 int ret = 0;
333
2fd6b7f5 334 spin_lock(&dentry->d_lock);
946e51f2 335 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
da502956
NP
336 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
337 if (simple_positive(child)) {
338 spin_unlock(&child->d_lock);
1da177e4 339 goto out;
da502956
NP
340 }
341 spin_unlock(&child->d_lock);
342 }
1da177e4
LT
343 ret = 1;
344out:
2fd6b7f5 345 spin_unlock(&dentry->d_lock);
1da177e4
LT
346 return ret;
347}
12f38872 348EXPORT_SYMBOL(simple_empty);
1da177e4
LT
349
350int simple_unlink(struct inode *dir, struct dentry *dentry)
351{
dea655c2 352 struct inode *inode = d_inode(dentry);
1da177e4 353
078cd827 354 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
9a53c3a7 355 drop_nlink(inode);
1da177e4
LT
356 dput(dentry);
357 return 0;
358}
12f38872 359EXPORT_SYMBOL(simple_unlink);
1da177e4
LT
360
361int simple_rmdir(struct inode *dir, struct dentry *dentry)
362{
363 if (!simple_empty(dentry))
364 return -ENOTEMPTY;
365
dea655c2 366 drop_nlink(d_inode(dentry));
1da177e4 367 simple_unlink(dir, dentry);
9a53c3a7 368 drop_nlink(dir);
1da177e4
LT
369 return 0;
370}
12f38872 371EXPORT_SYMBOL(simple_rmdir);
1da177e4
LT
372
373int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
e0e0be8a
MS
374 struct inode *new_dir, struct dentry *new_dentry,
375 unsigned int flags)
1da177e4 376{
dea655c2 377 struct inode *inode = d_inode(old_dentry);
e36cb0b8 378 int they_are_dirs = d_is_dir(old_dentry);
1da177e4 379
e0e0be8a
MS
380 if (flags & ~RENAME_NOREPLACE)
381 return -EINVAL;
382
1da177e4
LT
383 if (!simple_empty(new_dentry))
384 return -ENOTEMPTY;
385
dea655c2 386 if (d_really_is_positive(new_dentry)) {
1da177e4 387 simple_unlink(new_dir, new_dentry);
841590ce 388 if (they_are_dirs) {
dea655c2 389 drop_nlink(d_inode(new_dentry));
9a53c3a7 390 drop_nlink(old_dir);
841590ce 391 }
1da177e4 392 } else if (they_are_dirs) {
9a53c3a7 393 drop_nlink(old_dir);
d8c76e6f 394 inc_nlink(new_dir);
1da177e4
LT
395 }
396
397 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
078cd827 398 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
1da177e4
LT
399
400 return 0;
401}
12f38872 402EXPORT_SYMBOL(simple_rename);
1da177e4 403
7bb46a67 404/**
eef2380c 405 * simple_setattr - setattr for simple filesystem
7bb46a67 406 * @dentry: dentry
407 * @iattr: iattr structure
408 *
409 * Returns 0 on success, -error on failure.
410 *
eef2380c
CH
411 * simple_setattr is a simple ->setattr implementation without a proper
412 * implementation of size changes.
413 *
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.
7bb46a67 417 */
418int simple_setattr(struct dentry *dentry, struct iattr *iattr)
419{
dea655c2 420 struct inode *inode = d_inode(dentry);
7bb46a67 421 int error;
422
31051c85 423 error = setattr_prepare(dentry, iattr);
7bb46a67 424 if (error)
425 return error;
426
2c27c65e
CH
427 if (iattr->ia_valid & ATTR_SIZE)
428 truncate_setsize(inode, iattr->ia_size);
6a1a90ad 429 setattr_copy(inode, iattr);
eef2380c
CH
430 mark_inode_dirty(inode);
431 return 0;
7bb46a67 432}
433EXPORT_SYMBOL(simple_setattr);
434
1da177e4
LT
435int simple_readpage(struct file *file, struct page *page)
436{
c0d92cbc 437 clear_highpage(page);
1da177e4
LT
438 flush_dcache_page(page);
439 SetPageUptodate(page);
1da177e4
LT
440 unlock_page(page);
441 return 0;
442}
12f38872 443EXPORT_SYMBOL(simple_readpage);
1da177e4 444
afddba49
NP
445int simple_write_begin(struct file *file, struct address_space *mapping,
446 loff_t pos, unsigned len, unsigned flags,
447 struct page **pagep, void **fsdata)
448{
449 struct page *page;
450 pgoff_t index;
afddba49 451
09cbfeaf 452 index = pos >> PAGE_SHIFT;
afddba49 453
54566b2c 454 page = grab_cache_page_write_begin(mapping, index, flags);
afddba49
NP
455 if (!page)
456 return -ENOMEM;
457
458 *pagep = page;
459
09cbfeaf
KS
460 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
461 unsigned from = pos & (PAGE_SIZE - 1);
193cf4b9 462
09cbfeaf 463 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
193cf4b9
BH
464 }
465 return 0;
afddba49 466}
12f38872 467EXPORT_SYMBOL(simple_write_begin);
afddba49 468
ad2a722f
BH
469/**
470 * simple_write_end - .write_end helper for non-block-device FSes
471 * @available: See .write_end of address_space_operations
472 * @file: "
473 * @mapping: "
474 * @pos: "
475 * @len: "
476 * @copied: "
477 * @page: "
478 * @fsdata: "
479 *
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.
04fff641
AV
489 *
490 * Use *ONLY* with simple_readpage()
ad2a722f 491 */
afddba49
NP
492int simple_write_end(struct file *file, struct address_space *mapping,
493 loff_t pos, unsigned len, unsigned copied,
494 struct page *page, void *fsdata)
495{
ad2a722f
BH
496 struct inode *inode = page->mapping->host;
497 loff_t last_pos = pos + copied;
afddba49
NP
498
499 /* zero the stale part of the page if we did a short copy */
04fff641
AV
500 if (!PageUptodate(page)) {
501 if (copied < len) {
502 unsigned from = pos & (PAGE_SIZE - 1);
afddba49 503
04fff641
AV
504 zero_user(page, from + copied, len - copied);
505 }
ad2a722f 506 SetPageUptodate(page);
04fff641 507 }
ad2a722f
BH
508 /*
509 * No need to use i_size_read() here, the i_size
510 * cannot change under us because we hold the i_mutex.
511 */
512 if (last_pos > inode->i_size)
513 i_size_write(inode, last_pos);
afddba49 514
ad2a722f 515 set_page_dirty(page);
afddba49 516 unlock_page(page);
09cbfeaf 517 put_page(page);
afddba49
NP
518
519 return copied;
520}
12f38872 521EXPORT_SYMBOL(simple_write_end);
afddba49 522
1a1c9bb4
JL
523/*
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.
527 */
7d683a09 528int simple_fill_super(struct super_block *s, unsigned long magic,
cda37124 529 const struct tree_descr *files)
1da177e4 530{
1da177e4
LT
531 struct inode *inode;
532 struct dentry *root;
533 struct dentry *dentry;
534 int i;
535
09cbfeaf
KS
536 s->s_blocksize = PAGE_SIZE;
537 s->s_blocksize_bits = PAGE_SHIFT;
1da177e4 538 s->s_magic = magic;
759b9775 539 s->s_op = &simple_super_operations;
1da177e4
LT
540 s->s_time_gran = 1;
541
542 inode = new_inode(s);
543 if (!inode)
544 return -ENOMEM;
1a1c9bb4
JL
545 /*
546 * because the root inode is 1, the files array must not contain an
547 * entry at index 1
548 */
549 inode->i_ino = 1;
1da177e4 550 inode->i_mode = S_IFDIR | 0755;
078cd827 551 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1da177e4
LT
552 inode->i_op = &simple_dir_inode_operations;
553 inode->i_fop = &simple_dir_operations;
bfe86848 554 set_nlink(inode, 2);
48fde701
AV
555 root = d_make_root(inode);
556 if (!root)
1da177e4 557 return -ENOMEM;
1da177e4
LT
558 for (i = 0; !files->name || files->name[0]; i++, files++) {
559 if (!files->name)
560 continue;
1a1c9bb4
JL
561
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__,
566 s->s_type->name);
567
1da177e4
LT
568 dentry = d_alloc_name(root, files->name);
569 if (!dentry)
570 goto out;
571 inode = new_inode(s);
32096ea1
KK
572 if (!inode) {
573 dput(dentry);
1da177e4 574 goto out;
32096ea1 575 }
1da177e4 576 inode->i_mode = S_IFREG | files->mode;
078cd827 577 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1da177e4
LT
578 inode->i_fop = files->ops;
579 inode->i_ino = i;
580 d_add(dentry, inode);
581 }
582 s->s_root = root;
583 return 0;
584out:
585 d_genocide(root);
640946f2 586 shrink_dcache_parent(root);
1da177e4
LT
587 dput(root);
588 return -ENOMEM;
589}
12f38872 590EXPORT_SYMBOL(simple_fill_super);
1da177e4
LT
591
592static DEFINE_SPINLOCK(pin_fs_lock);
593
1f5ce9e9 594int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
1da177e4
LT
595{
596 struct vfsmount *mnt = NULL;
597 spin_lock(&pin_fs_lock);
598 if (unlikely(!*mount)) {
599 spin_unlock(&pin_fs_lock);
1751e8a6 600 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
1da177e4
LT
601 if (IS_ERR(mnt))
602 return PTR_ERR(mnt);
603 spin_lock(&pin_fs_lock);
604 if (!*mount)
605 *mount = mnt;
606 }
607 mntget(*mount);
608 ++*count;
609 spin_unlock(&pin_fs_lock);
610 mntput(mnt);
611 return 0;
612}
12f38872 613EXPORT_SYMBOL(simple_pin_fs);
1da177e4
LT
614
615void simple_release_fs(struct vfsmount **mount, int *count)
616{
617 struct vfsmount *mnt;
618 spin_lock(&pin_fs_lock);
619 mnt = *mount;
620 if (!--*count)
621 *mount = NULL;
622 spin_unlock(&pin_fs_lock);
623 mntput(mnt);
624}
12f38872 625EXPORT_SYMBOL(simple_release_fs);
1da177e4 626
6d1029b5
AM
627/**
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
634 *
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.
637 *
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.
640 **/
1da177e4
LT
641ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
642 const void *from, size_t available)
643{
644 loff_t pos = *ppos;
14be2746
SR
645 size_t ret;
646
1da177e4
LT
647 if (pos < 0)
648 return -EINVAL;
14be2746 649 if (pos >= available || !count)
1da177e4
LT
650 return 0;
651 if (count > available - pos)
652 count = available - pos;
14be2746
SR
653 ret = copy_to_user(to, from + pos, count);
654 if (ret == count)
1da177e4 655 return -EFAULT;
14be2746 656 count -= ret;
1da177e4
LT
657 *ppos = pos + count;
658 return count;
659}
12f38872 660EXPORT_SYMBOL(simple_read_from_buffer);
1da177e4 661
6a727b43
JS
662/**
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
669 *
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.
672 *
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.
675 **/
676ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
677 const void __user *from, size_t count)
678{
679 loff_t pos = *ppos;
680 size_t res;
681
682 if (pos < 0)
683 return -EINVAL;
684 if (pos >= available || !count)
685 return 0;
686 if (count > available - pos)
687 count = available - pos;
688 res = copy_from_user(to + pos, from, count);
689 if (res == count)
690 return -EFAULT;
691 count -= res;
692 *ppos = pos + count;
693 return count;
694}
12f38872 695EXPORT_SYMBOL(simple_write_to_buffer);
6a727b43 696
6d1029b5
AM
697/**
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
704 *
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.
707 *
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.
710 **/
93b07113
AM
711ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
712 const void *from, size_t available)
713{
714 loff_t pos = *ppos;
715
716 if (pos < 0)
717 return -EINVAL;
718 if (pos >= available)
719 return 0;
720 if (count > available - pos)
721 count = available - pos;
722 memcpy(to, from + pos, count);
723 *ppos = pos + count;
724
725 return count;
726}
12f38872 727EXPORT_SYMBOL(memory_read_from_buffer);
93b07113 728
1da177e4
LT
729/*
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
733 * file-local buffer.
734 */
76791ab2
IM
735
736void simple_transaction_set(struct file *file, size_t n)
737{
738 struct simple_transaction_argresp *ar = file->private_data;
739
740 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
741
742 /*
743 * The barrier ensures that ar->size will really remain zero until
744 * ar->data is ready for reading.
745 */
746 smp_mb();
747 ar->size = n;
748}
12f38872 749EXPORT_SYMBOL(simple_transaction_set);
76791ab2 750
1da177e4
LT
751char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
752{
753 struct simple_transaction_argresp *ar;
754 static DEFINE_SPINLOCK(simple_transaction_lock);
755
756 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
757 return ERR_PTR(-EFBIG);
758
759 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
760 if (!ar)
761 return ERR_PTR(-ENOMEM);
762
763 spin_lock(&simple_transaction_lock);
764
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);
770 }
771
772 file->private_data = ar;
773
774 spin_unlock(&simple_transaction_lock);
775
776 if (copy_from_user(ar->data, buf, size))
777 return ERR_PTR(-EFAULT);
778
779 return ar->data;
780}
12f38872 781EXPORT_SYMBOL(simple_transaction_get);
1da177e4
LT
782
783ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
784{
785 struct simple_transaction_argresp *ar = file->private_data;
786
787 if (!ar)
788 return 0;
789 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
790}
12f38872 791EXPORT_SYMBOL(simple_transaction_read);
1da177e4
LT
792
793int simple_transaction_release(struct inode *inode, struct file *file)
794{
795 free_page((unsigned long)file->private_data);
796 return 0;
797}
12f38872 798EXPORT_SYMBOL(simple_transaction_release);
1da177e4 799
acaefc25
AB
800/* Simple attribute files */
801
802struct simple_attr {
8b88b099
CH
803 int (*get)(void *, u64 *);
804 int (*set)(void *, u64);
acaefc25
AB
805 char get_buf[24]; /* enough to store a u64 and "\n\0" */
806 char set_buf[24];
807 void *data;
808 const char *fmt; /* format for read operation */
7cf34c76 809 struct mutex mutex; /* protects access to these buffers */
acaefc25
AB
810};
811
812/* simple_attr_open is called by an actual attribute open file operation
813 * to set the attribute specific access operations. */
814int simple_attr_open(struct inode *inode, struct file *file,
8b88b099 815 int (*get)(void *, u64 *), int (*set)(void *, u64),
acaefc25
AB
816 const char *fmt)
817{
818 struct simple_attr *attr;
819
820 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
821 if (!attr)
822 return -ENOMEM;
823
824 attr->get = get;
825 attr->set = set;
8e18e294 826 attr->data = inode->i_private;
acaefc25 827 attr->fmt = fmt;
7cf34c76 828 mutex_init(&attr->mutex);
acaefc25
AB
829
830 file->private_data = attr;
831
832 return nonseekable_open(inode, file);
833}
12f38872 834EXPORT_SYMBOL_GPL(simple_attr_open);
acaefc25 835
74bedc4d 836int simple_attr_release(struct inode *inode, struct file *file)
acaefc25
AB
837{
838 kfree(file->private_data);
839 return 0;
840}
12f38872 841EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
acaefc25
AB
842
843/* read from the buffer that is filled with the get function */
844ssize_t simple_attr_read(struct file *file, char __user *buf,
845 size_t len, loff_t *ppos)
846{
847 struct simple_attr *attr;
848 size_t size;
849 ssize_t ret;
850
851 attr = file->private_data;
852
853 if (!attr->get)
854 return -EACCES;
855
9261303a
CH
856 ret = mutex_lock_interruptible(&attr->mutex);
857 if (ret)
858 return ret;
859
8b88b099 860 if (*ppos) { /* continued read */
acaefc25 861 size = strlen(attr->get_buf);
8b88b099
CH
862 } else { /* first read */
863 u64 val;
864 ret = attr->get(attr->data, &val);
865 if (ret)
866 goto out;
867
acaefc25 868 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
8b88b099
CH
869 attr->fmt, (unsigned long long)val);
870 }
acaefc25
AB
871
872 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
8b88b099 873out:
7cf34c76 874 mutex_unlock(&attr->mutex);
acaefc25
AB
875 return ret;
876}
12f38872 877EXPORT_SYMBOL_GPL(simple_attr_read);
acaefc25
AB
878
879/* interpret the buffer as a number to call the set function with */
880ssize_t simple_attr_write(struct file *file, const char __user *buf,
881 size_t len, loff_t *ppos)
882{
883 struct simple_attr *attr;
884 u64 val;
885 size_t size;
886 ssize_t ret;
887
888 attr = file->private_data;
acaefc25
AB
889 if (!attr->set)
890 return -EACCES;
891
9261303a
CH
892 ret = mutex_lock_interruptible(&attr->mutex);
893 if (ret)
894 return ret;
895
acaefc25
AB
896 ret = -EFAULT;
897 size = min(sizeof(attr->set_buf) - 1, len);
898 if (copy_from_user(attr->set_buf, buf, size))
899 goto out;
900
acaefc25 901 attr->set_buf[size] = '\0';
f7b88631 902 val = simple_strtoll(attr->set_buf, NULL, 0);
05cc0cee
WF
903 ret = attr->set(attr->data, val);
904 if (ret == 0)
905 ret = len; /* on success, claim we got the whole input */
acaefc25 906out:
7cf34c76 907 mutex_unlock(&attr->mutex);
acaefc25
AB
908 return ret;
909}
12f38872 910EXPORT_SYMBOL_GPL(simple_attr_write);
acaefc25 911
2596110a
CH
912/**
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
919 *
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.
923 */
924struct 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))
927{
928 struct inode *inode = NULL;
929
930 if (fh_len < 2)
931 return NULL;
932
933 switch (fh_type) {
934 case FILEID_INO32_GEN:
935 case FILEID_INO32_GEN_PARENT:
936 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
937 break;
938 }
939
4ea3ada2 940 return d_obtain_alias(inode);
2596110a
CH
941}
942EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
943
944/**
ca186830 945 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
2596110a
CH
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
951 *
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.
956 */
957struct 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))
960{
961 struct inode *inode = NULL;
962
963 if (fh_len <= 2)
964 return NULL;
965
966 switch (fh_type) {
967 case FILEID_INO32_GEN_PARENT:
968 inode = get_inode(sb, fid->i32.parent_ino,
969 (fh_len > 3 ? fid->i32.parent_gen : 0));
970 break;
971 }
972
4ea3ada2 973 return d_obtain_alias(inode);
2596110a
CH
974}
975EXPORT_SYMBOL_GPL(generic_fh_to_parent);
976
1b061d92 977/**
ac13a829
FF
978 * __generic_file_fsync - generic fsync implementation for simple filesystems
979 *
1b061d92 980 * @file: file to synchronize
ac13a829
FF
981 * @start: start offset in bytes
982 * @end: end offset in bytes (inclusive)
1b061d92
CH
983 * @datasync: only synchronize essential metadata if true
984 *
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.
988 */
ac13a829
FF
989int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
990 int datasync)
d5aacad5 991{
7ea80859 992 struct inode *inode = file->f_mapping->host;
d5aacad5
AV
993 int err;
994 int ret;
995
383aa543 996 err = file_write_and_wait_range(file, start, end);
02c24a82
JB
997 if (err)
998 return err;
999
5955102c 1000 inode_lock(inode);
d5aacad5 1001 ret = sync_mapping_buffers(inode->i_mapping);
0ae45f63 1002 if (!(inode->i_state & I_DIRTY_ALL))
02c24a82 1003 goto out;
d5aacad5 1004 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
02c24a82 1005 goto out;
d5aacad5 1006
c3765016 1007 err = sync_inode_metadata(inode, 1);
d5aacad5
AV
1008 if (ret == 0)
1009 ret = err;
ac13a829 1010
02c24a82 1011out:
5955102c 1012 inode_unlock(inode);
383aa543
JL
1013 /* check and advance again to catch errors after syncing out buffers */
1014 err = file_check_and_advance_wb_err(file);
1015 if (ret == 0)
1016 ret = err;
1017 return ret;
d5aacad5 1018}
ac13a829
FF
1019EXPORT_SYMBOL(__generic_file_fsync);
1020
1021/**
1022 * generic_file_fsync - generic fsync implementation for simple filesystems
1023 * with flush
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
1028 *
1029 */
1030
1031int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1032 int datasync)
1033{
1034 struct inode *inode = file->f_mapping->host;
1035 int err;
1036
1037 err = __generic_file_fsync(file, start, end, datasync);
1038 if (err)
1039 return err;
1040 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1041}
1b061d92
CH
1042EXPORT_SYMBOL(generic_file_fsync);
1043
30ca22c7
PL
1044/**
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
1048 *
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.
1052 */
1053int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1054{
1055 u64 last_fs_block = num_blocks - 1;
a33f13ef 1056 u64 last_fs_page =
09cbfeaf 1057 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
30ca22c7
PL
1058
1059 if (unlikely(num_blocks == 0))
1060 return 0;
1061
09cbfeaf 1062 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
30ca22c7
PL
1063 return -EINVAL;
1064
a33f13ef
JB
1065 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1066 (last_fs_page > (pgoff_t)(~0ULL))) {
30ca22c7
PL
1067 return -EFBIG;
1068 }
1069 return 0;
1070}
1071EXPORT_SYMBOL(generic_check_addressable);
1072
1b061d92
CH
1073/*
1074 * No-op implementation of ->fsync for in-memory filesystems.
1075 */
02c24a82 1076int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1b061d92
CH
1077{
1078 return 0;
1079}
1b061d92 1080EXPORT_SYMBOL(noop_fsync);
87dc800b 1081
f44c7763
DW
1082int noop_set_page_dirty(struct page *page)
1083{
1084 /*
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.
1090 *
1091 * This callback is defined to prevent fallback to
1092 * __set_page_dirty_buffers() in set_page_dirty().
1093 */
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1097
1098void noop_invalidatepage(struct page *page, unsigned int offset,
1099 unsigned int length)
1100{
1101 /*
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().
1105 */
1106}
1107EXPORT_SYMBOL_GPL(noop_invalidatepage);
1108
1109ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1110{
1111 /*
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.
1116 */
1117 return -EINVAL;
1118}
1119EXPORT_SYMBOL_GPL(noop_direct_IO);
1120
fceef393
AV
1121/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1122void kfree_link(void *p)
87dc800b 1123{
fceef393 1124 kfree(p);
87dc800b 1125}
fceef393 1126EXPORT_SYMBOL(kfree_link);
6987843f
AV
1127
1128/*
1129 * nop .set_page_dirty method so that people can use .page_mkwrite on
1130 * anon inodes.
1131 */
1132static int anon_set_page_dirty(struct page *page)
1133{
1134 return 0;
1135};
1136
1137/*
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.
1141 */
1142struct inode *alloc_anon_inode(struct super_block *s)
1143{
1144 static const struct address_space_operations anon_aops = {
1145 .set_page_dirty = anon_set_page_dirty,
1146 };
1147 struct inode *inode = new_inode_pseudo(s);
1148
1149 if (!inode)
1150 return ERR_PTR(-ENOMEM);
1151
1152 inode->i_ino = get_next_ino();
1153 inode->i_mapping->a_ops = &anon_aops;
1154
1155 /*
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.
1160 */
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;
078cd827 1166 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
6987843f
AV
1167 return inode;
1168}
1169EXPORT_SYMBOL(alloc_anon_inode);
1c994a09
JL
1170
1171/**
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
e6f5c789 1176 * @priv: private data for lm_setup operation
1c994a09
JL
1177 *
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.
1180 */
1181int
e6f5c789
JL
1182simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1183 void **priv)
1c994a09
JL
1184{
1185 return -EINVAL;
1186}
1187EXPORT_SYMBOL(simple_nosetlease);
61ba64fc 1188
6ee9706a
EB
1189/**
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
1194 *
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.
1200 *
1201 * Return: the symlink target
1202 */
6b255391 1203const char *simple_get_link(struct dentry *dentry, struct inode *inode,
fceef393 1204 struct delayed_call *done)
61ba64fc 1205{
6b255391 1206 return inode->i_link;
61ba64fc 1207}
6b255391 1208EXPORT_SYMBOL(simple_get_link);
61ba64fc
AV
1209
1210const struct inode_operations simple_symlink_inode_operations = {
6b255391 1211 .get_link = simple_get_link,
61ba64fc
AV
1212};
1213EXPORT_SYMBOL(simple_symlink_inode_operations);
fbabfd0f
EB
1214
1215/*
1216 * Operations for a permanently empty directory.
1217 */
1218static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1219{
1220 return ERR_PTR(-ENOENT);
1221}
1222
a528d35e
DH
1223static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1224 u32 request_mask, unsigned int query_flags)
fbabfd0f 1225{
a528d35e 1226 struct inode *inode = d_inode(path->dentry);
fbabfd0f
EB
1227 generic_fillattr(inode, stat);
1228 return 0;
1229}
1230
1231static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1232{
1233 return -EPERM;
1234}
1235
fbabfd0f
EB
1236static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1237{
1238 return -EOPNOTSUPP;
1239}
1240
1241static 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,
fbabfd0f
EB
1246 .listxattr = empty_dir_listxattr,
1247};
1248
1249static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1250{
1251 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1252 return generic_file_llseek_size(file, offset, whence, 2, 2);
1253}
1254
1255static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1256{
1257 dir_emit_dots(file, ctx);
1258 return 0;
1259}
1260
1261static const struct file_operations empty_dir_operations = {
1262 .llseek = empty_dir_llseek,
1263 .read = generic_read_dir,
c51da20c 1264 .iterate_shared = empty_dir_readdir,
fbabfd0f
EB
1265 .fsync = noop_fsync,
1266};
1267
1268
1269void make_empty_dir_inode(struct inode *inode)
1270{
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;
1275 inode->i_rdev = 0;
4b75de86 1276 inode->i_size = 0;
fbabfd0f
EB
1277 inode->i_blkbits = PAGE_SHIFT;
1278 inode->i_blocks = 0;
1279
1280 inode->i_op = &empty_dir_inode_operations;
f5c24438 1281 inode->i_opflags &= ~IOP_XATTR;
fbabfd0f
EB
1282 inode->i_fop = &empty_dir_operations;
1283}
1284
1285bool is_empty_dir_inode(struct inode *inode)
1286{
1287 return (inode->i_fop == &empty_dir_operations) &&
1288 (inode->i_op == &empty_dir_inode_operations);
1289}