Merge tag 'vfs-6.9.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[linux-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>
5ca14835 18#include <linux/iversion.h>
d5aacad5 19#include <linux/writeback.h>
ff01bb48 20#include <linux/buffer_head.h> /* sync_mapping_buffers */
31d6d5ce
DH
21#include <linux/fs_context.h>
22#include <linux/pseudo_fs.h>
a3d1e7eb 23#include <linux/fsnotify.h>
c843843e
DR
24#include <linux/unicode.h>
25#include <linux/fscrypt.h>
7cf34c76 26
7c0f6ba6 27#include <linux/uaccess.h>
1da177e4 28
a4464dbc
AV
29#include "internal.h"
30
b74d24f7 31int simple_getattr(struct mnt_idmap *idmap, const struct path *path,
549c7297
CB
32 struct kstat *stat, u32 request_mask,
33 unsigned int query_flags)
1da177e4 34{
a528d35e 35 struct inode *inode = d_inode(path->dentry);
0d72b928 36 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
09cbfeaf 37 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
1da177e4
LT
38 return 0;
39}
12f38872 40EXPORT_SYMBOL(simple_getattr);
1da177e4 41
726c3342 42int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 43{
ae62bcb5
AG
44 u64 id = huge_encode_dev(dentry->d_sb->s_dev);
45
46 buf->f_fsid = u64_to_fsid(id);
726c3342 47 buf->f_type = dentry->d_sb->s_magic;
09cbfeaf 48 buf->f_bsize = PAGE_SIZE;
1da177e4
LT
49 buf->f_namelen = NAME_MAX;
50 return 0;
51}
12f38872 52EXPORT_SYMBOL(simple_statfs);
1da177e4
LT
53
54/*
55 * Retaining negative dentries for an in-memory filesystem just wastes
56 * memory and lookup time: arrange for them to be deleted immediately.
57 */
b26d4cd3 58int always_delete_dentry(const struct dentry *dentry)
1da177e4
LT
59{
60 return 1;
61}
b26d4cd3
AV
62EXPORT_SYMBOL(always_delete_dentry);
63
64const struct dentry_operations simple_dentry_operations = {
65 .d_delete = always_delete_dentry,
66};
67EXPORT_SYMBOL(simple_dentry_operations);
1da177e4
LT
68
69/*
70 * Lookup the data. This is trivial - if the dentry didn't already
71 * exist, we know it is negative. Set d_op to delete negative dentries.
72 */
00cd8dd3 73struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1da177e4 74{
1da177e4
LT
75 if (dentry->d_name.len > NAME_MAX)
76 return ERR_PTR(-ENAMETOOLONG);
74931da7
AV
77 if (!dentry->d_sb->s_d_op)
78 d_set_d_op(dentry, &simple_dentry_operations);
1da177e4
LT
79 d_add(dentry, NULL);
80 return NULL;
81}
12f38872 82EXPORT_SYMBOL(simple_lookup);
1da177e4 83
1da177e4
LT
84int dcache_dir_open(struct inode *inode, struct file *file)
85{
ba65dc5e 86 file->private_data = d_alloc_cursor(file->f_path.dentry);
1da177e4
LT
87
88 return file->private_data ? 0 : -ENOMEM;
89}
12f38872 90EXPORT_SYMBOL(dcache_dir_open);
1da177e4
LT
91
92int dcache_dir_close(struct inode *inode, struct file *file)
93{
94 dput(file->private_data);
95 return 0;
96}
12f38872 97EXPORT_SYMBOL(dcache_dir_close);
1da177e4 98
4f42c1b5 99/* parent is locked at least shared */
d4f4de5e
AV
100/*
101 * Returns an element of siblings' list.
102 * We are looking for <count>th positive after <p>; if
26b6c984
AV
103 * found, dentry is grabbed and returned to caller.
104 * If no such element exists, NULL is returned.
d4f4de5e 105 */
26b6c984 106static struct dentry *scan_positives(struct dentry *cursor,
da549bdd 107 struct hlist_node **p,
d4f4de5e 108 loff_t count,
26b6c984 109 struct dentry *last)
4f42c1b5 110{
d4f4de5e
AV
111 struct dentry *dentry = cursor->d_parent, *found = NULL;
112
113 spin_lock(&dentry->d_lock);
da549bdd
AV
114 while (*p) {
115 struct dentry *d = hlist_entry(*p, struct dentry, d_sib);
116 p = &d->d_sib.next;
d4f4de5e
AV
117 // we must at least skip cursors, to avoid livelocks
118 if (d->d_flags & DCACHE_DENTRY_CURSOR)
119 continue;
120 if (simple_positive(d) && !--count) {
121 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
122 if (simple_positive(d))
123 found = dget_dlock(d);
124 spin_unlock(&d->d_lock);
125 if (likely(found))
126 break;
127 count = 1;
128 }
129 if (need_resched()) {
da549bdd
AV
130 if (!hlist_unhashed(&cursor->d_sib))
131 __hlist_del(&cursor->d_sib);
132 hlist_add_behind(&cursor->d_sib, &d->d_sib);
133 p = &cursor->d_sib.next;
d4f4de5e
AV
134 spin_unlock(&dentry->d_lock);
135 cond_resched();
136 spin_lock(&dentry->d_lock);
4f42c1b5
AV
137 }
138 }
d4f4de5e 139 spin_unlock(&dentry->d_lock);
26b6c984
AV
140 dput(last);
141 return found;
4f42c1b5
AV
142}
143
965c8e59 144loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
1da177e4 145{
2fd6b7f5 146 struct dentry *dentry = file->f_path.dentry;
965c8e59 147 switch (whence) {
1da177e4
LT
148 case 1:
149 offset += file->f_pos;
df561f66 150 fallthrough;
1da177e4
LT
151 case 0:
152 if (offset >= 0)
153 break;
df561f66 154 fallthrough;
1da177e4 155 default:
1da177e4
LT
156 return -EINVAL;
157 }
158 if (offset != file->f_pos) {
d4f4de5e
AV
159 struct dentry *cursor = file->private_data;
160 struct dentry *to = NULL;
d4f4de5e 161
d4f4de5e
AV
162 inode_lock_shared(dentry->d_inode);
163
26b6c984 164 if (offset > 2)
da549bdd 165 to = scan_positives(cursor, &dentry->d_children.first,
26b6c984
AV
166 offset - 2, NULL);
167 spin_lock(&dentry->d_lock);
da549bdd 168 hlist_del_init(&cursor->d_sib);
26b6c984 169 if (to)
da549bdd 170 hlist_add_behind(&cursor->d_sib, &to->d_sib);
26b6c984 171 spin_unlock(&dentry->d_lock);
d4f4de5e
AV
172 dput(to);
173
26b6c984
AV
174 file->f_pos = offset;
175
d4f4de5e 176 inode_unlock_shared(dentry->d_inode);
1da177e4 177 }
1da177e4
LT
178 return offset;
179}
12f38872 180EXPORT_SYMBOL(dcache_dir_lseek);
1da177e4 181
1da177e4
LT
182/*
183 * Directory is locked and all positive dentries in it are safe, since
184 * for ramfs-type trees they can't go away without unlink() or rmdir(),
185 * both impossible due to the lock on directory.
186 */
187
5f99f4e7 188int dcache_readdir(struct file *file, struct dir_context *ctx)
1da177e4 189{
5f99f4e7
AV
190 struct dentry *dentry = file->f_path.dentry;
191 struct dentry *cursor = file->private_data;
d4f4de5e 192 struct dentry *next = NULL;
da549bdd 193 struct hlist_node **p;
1da177e4 194
5f99f4e7
AV
195 if (!dir_emit_dots(file, ctx))
196 return 0;
5f99f4e7 197
4f42c1b5 198 if (ctx->pos == 2)
da549bdd 199 p = &dentry->d_children.first;
26b6c984 200 else
da549bdd 201 p = &cursor->d_sib.next;
d4f4de5e 202
26b6c984 203 while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
5f99f4e7 204 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
364595a6
JL
205 d_inode(next)->i_ino,
206 fs_umode_to_dtype(d_inode(next)->i_mode)))
4f42c1b5 207 break;
5f99f4e7 208 ctx->pos++;
da549bdd 209 p = &next->d_sib.next;
1da177e4 210 }
d4f4de5e 211 spin_lock(&dentry->d_lock);
da549bdd 212 hlist_del_init(&cursor->d_sib);
26b6c984 213 if (next)
da549bdd 214 hlist_add_before(&cursor->d_sib, &next->d_sib);
d4f4de5e
AV
215 spin_unlock(&dentry->d_lock);
216 dput(next);
217
1da177e4
LT
218 return 0;
219}
12f38872 220EXPORT_SYMBOL(dcache_readdir);
1da177e4
LT
221
222ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
223{
224 return -EISDIR;
225}
12f38872 226EXPORT_SYMBOL(generic_read_dir);
1da177e4 227
4b6f5d20 228const struct file_operations simple_dir_operations = {
1da177e4
LT
229 .open = dcache_dir_open,
230 .release = dcache_dir_close,
231 .llseek = dcache_dir_lseek,
232 .read = generic_read_dir,
4e82901c 233 .iterate_shared = dcache_readdir,
1b061d92 234 .fsync = noop_fsync,
1da177e4 235};
12f38872 236EXPORT_SYMBOL(simple_dir_operations);
1da177e4 237
92e1d5be 238const struct inode_operations simple_dir_inode_operations = {
1da177e4
LT
239 .lookup = simple_lookup,
240};
12f38872 241EXPORT_SYMBOL(simple_dir_inode_operations);
1da177e4 242
7beea725
CL
243/* 0 is '.', 1 is '..', so always start with offset 2 or more */
244enum {
245 DIR_OFFSET_MIN = 2,
246};
247
0e4a8621 248static void offset_set(struct dentry *dentry, long offset)
6faddda6 249{
0e4a8621 250 dentry->d_fsdata = (void *)offset;
6faddda6
CL
251}
252
0e4a8621 253static long dentry2offset(struct dentry *dentry)
6faddda6 254{
0e4a8621 255 return (long)dentry->d_fsdata;
6faddda6
CL
256}
257
0e4a8621 258static struct lock_class_key simple_offset_lock_class;
bbaef797 259
6faddda6
CL
260/**
261 * simple_offset_init - initialize an offset_ctx
262 * @octx: directory offset map to be initialized
263 *
264 */
265void simple_offset_init(struct offset_ctx *octx)
266{
0e4a8621
CL
267 mt_init_flags(&octx->mt, MT_FLAGS_ALLOC_RANGE);
268 lockdep_set_class(&octx->mt.ma_lock, &simple_offset_lock_class);
7beea725 269 octx->next_offset = DIR_OFFSET_MIN;
6faddda6
CL
270}
271
272/**
273 * simple_offset_add - Add an entry to a directory's offset map
274 * @octx: directory offset ctx to be updated
275 * @dentry: new dentry being added
276 *
0e4a8621 277 * Returns zero on success. @octx and the dentry's offset are updated.
6faddda6
CL
278 * Otherwise, a negative errno value is returned.
279 */
280int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
281{
0e4a8621 282 unsigned long offset;
6faddda6
CL
283 int ret;
284
285 if (dentry2offset(dentry) != 0)
286 return -EBUSY;
287
0e4a8621
CL
288 ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN,
289 LONG_MAX, &octx->next_offset, GFP_KERNEL);
6faddda6
CL
290 if (ret < 0)
291 return ret;
292
293 offset_set(dentry, offset);
294 return 0;
295}
296
297/**
298 * simple_offset_remove - Remove an entry to a directory's offset map
299 * @octx: directory offset ctx to be updated
300 * @dentry: dentry being removed
301 *
302 */
303void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry)
304{
0e4a8621 305 long offset;
6faddda6
CL
306
307 offset = dentry2offset(dentry);
308 if (offset == 0)
309 return;
310
0e4a8621 311 mtree_erase(&octx->mt, offset);
6faddda6
CL
312 offset_set(dentry, 0);
313}
314
ecba88a3
CL
315/**
316 * simple_offset_empty - Check if a dentry can be unlinked
317 * @dentry: dentry to be tested
318 *
319 * Returns 0 if @dentry is a non-empty directory; otherwise returns 1.
320 */
321int simple_offset_empty(struct dentry *dentry)
322{
323 struct inode *inode = d_inode(dentry);
324 struct offset_ctx *octx;
325 struct dentry *child;
326 unsigned long index;
327 int ret = 1;
328
329 if (!inode || !S_ISDIR(inode->i_mode))
330 return ret;
331
332 index = DIR_OFFSET_MIN;
333 octx = inode->i_op->get_offset_ctx(inode);
0e4a8621 334 mt_for_each(&octx->mt, child, index, LONG_MAX) {
ecba88a3
CL
335 spin_lock(&child->d_lock);
336 if (simple_positive(child)) {
337 spin_unlock(&child->d_lock);
338 ret = 0;
339 break;
340 }
341 spin_unlock(&child->d_lock);
342 }
343
344 return ret;
345}
346
6faddda6
CL
347/**
348 * simple_offset_rename_exchange - exchange rename with directory offsets
349 * @old_dir: parent of dentry being moved
350 * @old_dentry: dentry being moved
351 * @new_dir: destination parent
352 * @new_dentry: destination dentry
353 *
354 * Returns zero on success. Otherwise a negative errno is returned and the
355 * rename is rolled back.
356 */
357int simple_offset_rename_exchange(struct inode *old_dir,
358 struct dentry *old_dentry,
359 struct inode *new_dir,
360 struct dentry *new_dentry)
361{
362 struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir);
363 struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir);
0e4a8621
CL
364 long old_index = dentry2offset(old_dentry);
365 long new_index = dentry2offset(new_dentry);
6faddda6
CL
366 int ret;
367
368 simple_offset_remove(old_ctx, old_dentry);
369 simple_offset_remove(new_ctx, new_dentry);
370
371 ret = simple_offset_add(new_ctx, old_dentry);
372 if (ret)
373 goto out_restore;
374
375 ret = simple_offset_add(old_ctx, new_dentry);
376 if (ret) {
377 simple_offset_remove(new_ctx, old_dentry);
378 goto out_restore;
379 }
380
381 ret = simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
382 if (ret) {
383 simple_offset_remove(new_ctx, old_dentry);
384 simple_offset_remove(old_ctx, new_dentry);
385 goto out_restore;
386 }
387 return 0;
388
389out_restore:
390 offset_set(old_dentry, old_index);
0e4a8621 391 mtree_store(&old_ctx->mt, old_index, old_dentry, GFP_KERNEL);
6faddda6 392 offset_set(new_dentry, new_index);
0e4a8621 393 mtree_store(&new_ctx->mt, new_index, new_dentry, GFP_KERNEL);
6faddda6
CL
394 return ret;
395}
396
397/**
398 * simple_offset_destroy - Release offset map
399 * @octx: directory offset ctx that is about to be destroyed
400 *
401 * During fs teardown (eg. umount), a directory's offset map might still
402 * contain entries. xa_destroy() cleans out anything that remains.
403 */
404void simple_offset_destroy(struct offset_ctx *octx)
405{
0e4a8621 406 mtree_destroy(&octx->mt);
6faddda6
CL
407}
408
409/**
410 * offset_dir_llseek - Advance the read position of a directory descriptor
411 * @file: an open directory whose position is to be updated
412 * @offset: a byte offset
413 * @whence: enumerator describing the starting position for this update
414 *
415 * SEEK_END, SEEK_DATA, and SEEK_HOLE are not supported for directories.
416 *
417 * Returns the updated read position if successful; otherwise a
418 * negative errno is returned and the read position remains unchanged.
419 */
420static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
421{
422 switch (whence) {
423 case SEEK_CUR:
424 offset += file->f_pos;
425 fallthrough;
426 case SEEK_SET:
427 if (offset >= 0)
428 break;
429 fallthrough;
430 default:
431 return -EINVAL;
432 }
433
796432ef
CL
434 /* In this case, ->private_data is protected by f_pos_lock */
435 file->private_data = NULL;
0e4a8621 436 return vfs_setpos(file, offset, LONG_MAX);
6faddda6
CL
437}
438
3f6d8106 439static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset)
6faddda6 440{
0e4a8621 441 MA_STATE(mas, &octx->mt, offset, offset);
6faddda6
CL
442 struct dentry *child, *found = NULL;
443
444 rcu_read_lock();
0e4a8621 445 child = mas_find(&mas, LONG_MAX);
6faddda6
CL
446 if (!child)
447 goto out;
2be4f05a 448 spin_lock(&child->d_lock);
6faddda6
CL
449 if (simple_positive(child))
450 found = dget_dlock(child);
451 spin_unlock(&child->d_lock);
452out:
453 rcu_read_unlock();
454 return found;
455}
456
457static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
458{
6faddda6 459 struct inode *inode = d_inode(dentry);
0e4a8621 460 long offset = dentry2offset(dentry);
6faddda6
CL
461
462 return ctx->actor(ctx, dentry->d_name.name, dentry->d_name.len, offset,
463 inode->i_ino, fs_umode_to_dtype(inode->i_mode));
464}
465
796432ef 466static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
6faddda6 467{
3f6d8106 468 struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode);
6faddda6
CL
469 struct dentry *dentry;
470
471 while (true) {
3f6d8106 472 dentry = offset_find_next(octx, ctx->pos);
6faddda6 473 if (!dentry)
796432ef 474 return ERR_PTR(-ENOENT);
6faddda6
CL
475
476 if (!offset_dir_emit(ctx, dentry)) {
477 dput(dentry);
478 break;
479 }
480
3f6d8106 481 ctx->pos = dentry2offset(dentry) + 1;
6faddda6 482 dput(dentry);
6faddda6 483 }
796432ef 484 return NULL;
6faddda6
CL
485}
486
487/**
488 * offset_readdir - Emit entries starting at offset @ctx->pos
489 * @file: an open directory to iterate over
490 * @ctx: directory iteration context
491 *
492 * Caller must hold @file's i_rwsem to prevent insertion or removal of
493 * entries during this call.
494 *
495 * On entry, @ctx->pos contains an offset that represents the first entry
496 * to be read from the directory.
497 *
498 * The operation continues until there are no more entries to read, or
499 * until the ctx->actor indicates there is no more space in the caller's
500 * output buffer.
501 *
502 * On return, @ctx->pos contains an offset that will read the next entry
2be4f05a 503 * in this directory when offset_readdir() is called again with @ctx.
6faddda6
CL
504 *
505 * Return values:
506 * %0 - Complete
507 */
508static int offset_readdir(struct file *file, struct dir_context *ctx)
509{
510 struct dentry *dir = file->f_path.dentry;
511
512 lockdep_assert_held(&d_inode(dir)->i_rwsem);
513
514 if (!dir_emit_dots(file, ctx))
515 return 0;
516
796432ef 517 /* In this case, ->private_data is protected by f_pos_lock */
7beea725 518 if (ctx->pos == DIR_OFFSET_MIN)
796432ef
CL
519 file->private_data = NULL;
520 else if (file->private_data == ERR_PTR(-ENOENT))
521 return 0;
522 file->private_data = offset_iterate_dir(d_inode(dir), ctx);
6faddda6
CL
523 return 0;
524}
525
526const struct file_operations simple_offset_dir_operations = {
527 .llseek = offset_dir_llseek,
528 .iterate_shared = offset_readdir,
529 .read = generic_read_dir,
530 .fsync = noop_fsync,
531};
532
a3d1e7eb
AV
533static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
534{
da549bdd 535 struct dentry *child = NULL, *d;
a3d1e7eb
AV
536
537 spin_lock(&parent->d_lock);
da549bdd
AV
538 d = prev ? d_next_sibling(prev) : d_first_child(parent);
539 hlist_for_each_entry_from(d, d_sib) {
a3d1e7eb
AV
540 if (simple_positive(d)) {
541 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
542 if (simple_positive(d))
543 child = dget_dlock(d);
544 spin_unlock(&d->d_lock);
545 if (likely(child))
546 break;
547 }
548 }
549 spin_unlock(&parent->d_lock);
550 dput(prev);
551 return child;
552}
553
554void simple_recursive_removal(struct dentry *dentry,
555 void (*callback)(struct dentry *))
556{
557 struct dentry *this = dget(dentry);
558 while (true) {
559 struct dentry *victim = NULL, *child;
560 struct inode *inode = this->d_inode;
561
562 inode_lock(inode);
563 if (d_is_dir(this))
564 inode->i_flags |= S_DEAD;
565 while ((child = find_next_child(this, victim)) == NULL) {
566 // kill and ascend
567 // update metadata while it's still locked
f7f43858 568 inode_set_ctime_current(inode);
a3d1e7eb
AV
569 clear_nlink(inode);
570 inode_unlock(inode);
571 victim = this;
572 this = this->d_parent;
573 inode = this->d_inode;
574 inode_lock(inode);
575 if (simple_positive(victim)) {
576 d_invalidate(victim); // avoid lost mounts
577 if (d_is_dir(victim))
578 fsnotify_rmdir(inode, victim);
579 else
580 fsnotify_unlink(inode, victim);
581 if (callback)
582 callback(victim);
583 dput(victim); // unpin it
584 }
585 if (victim == dentry) {
077c212f
JL
586 inode_set_mtime_to_ts(inode,
587 inode_set_ctime_current(inode));
a3d1e7eb
AV
588 if (d_is_dir(dentry))
589 drop_nlink(inode);
590 inode_unlock(inode);
591 dput(dentry);
592 return;
593 }
594 }
595 inode_unlock(inode);
596 this = child;
597 }
598}
599EXPORT_SYMBOL(simple_recursive_removal);
600
759b9775
HD
601static const struct super_operations simple_super_operations = {
602 .statfs = simple_statfs,
603};
604
db2c246a 605static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
1da177e4 606{
31d6d5ce 607 struct pseudo_fs_context *ctx = fc->fs_private;
1da177e4 608 struct inode *root;
1da177e4 609
89a4eb4b 610 s->s_maxbytes = MAX_LFS_FILESIZE;
3971e1a9
AN
611 s->s_blocksize = PAGE_SIZE;
612 s->s_blocksize_bits = PAGE_SHIFT;
8d9e46d8
AV
613 s->s_magic = ctx->magic;
614 s->s_op = ctx->ops ?: &simple_super_operations;
615 s->s_xattr = ctx->xattr;
1da177e4
LT
616 s->s_time_gran = 1;
617 root = new_inode(s);
618 if (!root)
db2c246a
DH
619 return -ENOMEM;
620
1a1c9bb4
JL
621 /*
622 * since this is the first inode, make it number 1. New inodes created
623 * after this must take care not to collide with it (by passing
624 * max_reserved of 1 to iunique).
625 */
626 root->i_ino = 1;
1da177e4 627 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
077c212f 628 simple_inode_init_ts(root);
8d9e46d8
AV
629 s->s_root = d_make_root(root);
630 if (!s->s_root)
db2c246a 631 return -ENOMEM;
8d9e46d8 632 s->s_d_op = ctx->dops;
31d6d5ce 633 return 0;
db2c246a 634}
8d9e46d8 635
db2c246a
DH
636static int pseudo_fs_get_tree(struct fs_context *fc)
637{
2ac295d4 638 return get_tree_nodev(fc, pseudo_fs_fill_super);
31d6d5ce
DH
639}
640
641static void pseudo_fs_free(struct fs_context *fc)
642{
643 kfree(fc->fs_private);
644}
645
646static const struct fs_context_operations pseudo_fs_context_ops = {
647 .free = pseudo_fs_free,
648 .get_tree = pseudo_fs_get_tree,
649};
650
651/*
652 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
653 * will never be mountable)
654 */
655struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
656 unsigned long magic)
657{
658 struct pseudo_fs_context *ctx;
659
660 ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
661 if (likely(ctx)) {
662 ctx->magic = magic;
663 fc->fs_private = ctx;
664 fc->ops = &pseudo_fs_context_ops;
db2c246a
DH
665 fc->sb_flags |= SB_NOUSER;
666 fc->global = true;
1da177e4 667 }
31d6d5ce 668 return ctx;
1da177e4 669}
31d6d5ce 670EXPORT_SYMBOL(init_pseudo);
1da177e4 671
20955e89
SB
672int simple_open(struct inode *inode, struct file *file)
673{
674 if (inode->i_private)
675 file->private_data = inode->i_private;
676 return 0;
677}
12f38872 678EXPORT_SYMBOL(simple_open);
20955e89 679
1da177e4
LT
680int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
681{
dea655c2 682 struct inode *inode = d_inode(old_dentry);
1da177e4 683
077c212f
JL
684 inode_set_mtime_to_ts(dir,
685 inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
d8c76e6f 686 inc_nlink(inode);
7de9c6ee 687 ihold(inode);
1da177e4
LT
688 dget(dentry);
689 d_instantiate(dentry, inode);
690 return 0;
691}
12f38872 692EXPORT_SYMBOL(simple_link);
1da177e4 693
1da177e4
LT
694int simple_empty(struct dentry *dentry)
695{
696 struct dentry *child;
697 int ret = 0;
698
2fd6b7f5 699 spin_lock(&dentry->d_lock);
da549bdd 700 hlist_for_each_entry(child, &dentry->d_children, d_sib) {
da502956
NP
701 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
702 if (simple_positive(child)) {
703 spin_unlock(&child->d_lock);
1da177e4 704 goto out;
da502956
NP
705 }
706 spin_unlock(&child->d_lock);
707 }
1da177e4
LT
708 ret = 1;
709out:
2fd6b7f5 710 spin_unlock(&dentry->d_lock);
1da177e4
LT
711 return ret;
712}
12f38872 713EXPORT_SYMBOL(simple_empty);
1da177e4
LT
714
715int simple_unlink(struct inode *dir, struct dentry *dentry)
716{
dea655c2 717 struct inode *inode = d_inode(dentry);
1da177e4 718
077c212f
JL
719 inode_set_mtime_to_ts(dir,
720 inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
9a53c3a7 721 drop_nlink(inode);
1da177e4
LT
722 dput(dentry);
723 return 0;
724}
12f38872 725EXPORT_SYMBOL(simple_unlink);
1da177e4
LT
726
727int simple_rmdir(struct inode *dir, struct dentry *dentry)
728{
729 if (!simple_empty(dentry))
730 return -ENOTEMPTY;
731
dea655c2 732 drop_nlink(d_inode(dentry));
1da177e4 733 simple_unlink(dir, dentry);
9a53c3a7 734 drop_nlink(dir);
1da177e4
LT
735 return 0;
736}
12f38872 737EXPORT_SYMBOL(simple_rmdir);
1da177e4 738
0c476792
JL
739/**
740 * simple_rename_timestamp - update the various inode timestamps for rename
741 * @old_dir: old parent directory
742 * @old_dentry: dentry that is being renamed
743 * @new_dir: new parent directory
744 * @new_dentry: target for rename
745 *
746 * POSIX mandates that the old and new parent directories have their ctime and
747 * mtime updated, and that inodes of @old_dentry and @new_dentry (if any), have
748 * their ctime updated.
749 */
750void simple_rename_timestamp(struct inode *old_dir, struct dentry *old_dentry,
751 struct inode *new_dir, struct dentry *new_dentry)
752{
753 struct inode *newino = d_inode(new_dentry);
754
077c212f 755 inode_set_mtime_to_ts(old_dir, inode_set_ctime_current(old_dir));
0c476792 756 if (new_dir != old_dir)
077c212f
JL
757 inode_set_mtime_to_ts(new_dir,
758 inode_set_ctime_current(new_dir));
0c476792
JL
759 inode_set_ctime_current(d_inode(old_dentry));
760 if (newino)
761 inode_set_ctime_current(newino);
762}
763EXPORT_SYMBOL_GPL(simple_rename_timestamp);
764
6429e463
LB
765int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
766 struct inode *new_dir, struct dentry *new_dentry)
767{
768 bool old_is_dir = d_is_dir(old_dentry);
769 bool new_is_dir = d_is_dir(new_dentry);
770
771 if (old_dir != new_dir && old_is_dir != new_is_dir) {
772 if (old_is_dir) {
773 drop_nlink(old_dir);
774 inc_nlink(new_dir);
775 } else {
776 drop_nlink(new_dir);
777 inc_nlink(old_dir);
778 }
779 }
0c476792 780 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
6429e463
LB
781 return 0;
782}
783EXPORT_SYMBOL_GPL(simple_rename_exchange);
784
e18275ae 785int simple_rename(struct mnt_idmap *idmap, struct inode *old_dir,
549c7297
CB
786 struct dentry *old_dentry, struct inode *new_dir,
787 struct dentry *new_dentry, unsigned int flags)
1da177e4 788{
e36cb0b8 789 int they_are_dirs = d_is_dir(old_dentry);
1da177e4 790
3871cb8c 791 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
e0e0be8a
MS
792 return -EINVAL;
793
3871cb8c
LB
794 if (flags & RENAME_EXCHANGE)
795 return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
796
1da177e4
LT
797 if (!simple_empty(new_dentry))
798 return -ENOTEMPTY;
799
dea655c2 800 if (d_really_is_positive(new_dentry)) {
1da177e4 801 simple_unlink(new_dir, new_dentry);
841590ce 802 if (they_are_dirs) {
dea655c2 803 drop_nlink(d_inode(new_dentry));
9a53c3a7 804 drop_nlink(old_dir);
841590ce 805 }
1da177e4 806 } else if (they_are_dirs) {
9a53c3a7 807 drop_nlink(old_dir);
d8c76e6f 808 inc_nlink(new_dir);
1da177e4
LT
809 }
810
0c476792 811 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1da177e4
LT
812 return 0;
813}
12f38872 814EXPORT_SYMBOL(simple_rename);
1da177e4 815
7bb46a67 816/**
eef2380c 817 * simple_setattr - setattr for simple filesystem
c1632a0f 818 * @idmap: idmap of the target mount
7bb46a67 819 * @dentry: dentry
820 * @iattr: iattr structure
821 *
822 * Returns 0 on success, -error on failure.
823 *
eef2380c
CH
824 * simple_setattr is a simple ->setattr implementation without a proper
825 * implementation of size changes.
826 *
827 * It can either be used for in-memory filesystems or special files
828 * on simple regular filesystems. Anything that needs to change on-disk
829 * or wire state on size changes needs its own setattr method.
7bb46a67 830 */
c1632a0f 831int simple_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
549c7297 832 struct iattr *iattr)
7bb46a67 833{
dea655c2 834 struct inode *inode = d_inode(dentry);
7bb46a67 835 int error;
836
c1632a0f 837 error = setattr_prepare(idmap, dentry, iattr);
7bb46a67 838 if (error)
839 return error;
840
2c27c65e
CH
841 if (iattr->ia_valid & ATTR_SIZE)
842 truncate_setsize(inode, iattr->ia_size);
c1632a0f 843 setattr_copy(idmap, inode, iattr);
eef2380c
CH
844 mark_inode_dirty(inode);
845 return 0;
7bb46a67 846}
847EXPORT_SYMBOL(simple_setattr);
848
a77f580a 849static int simple_read_folio(struct file *file, struct folio *folio)
1da177e4 850{
a77f580a
MWO
851 folio_zero_range(folio, 0, folio_size(folio));
852 flush_dcache_folio(folio);
853 folio_mark_uptodate(folio);
854 folio_unlock(folio);
1da177e4
LT
855 return 0;
856}
857
afddba49 858int simple_write_begin(struct file *file, struct address_space *mapping,
9d6b0cd7 859 loff_t pos, unsigned len,
afddba49
NP
860 struct page **pagep, void **fsdata)
861{
5522d9f7 862 struct folio *folio;
afddba49 863
5522d9f7
MWO
864 folio = __filemap_get_folio(mapping, pos / PAGE_SIZE, FGP_WRITEBEGIN,
865 mapping_gfp_mask(mapping));
866 if (IS_ERR(folio))
867 return PTR_ERR(folio);
afddba49 868
5522d9f7 869 *pagep = &folio->page;
afddba49 870
5522d9f7
MWO
871 if (!folio_test_uptodate(folio) && (len != folio_size(folio))) {
872 size_t from = offset_in_folio(folio, pos);
193cf4b9 873
5522d9f7
MWO
874 folio_zero_segments(folio, 0, from,
875 from + len, folio_size(folio));
193cf4b9
BH
876 }
877 return 0;
afddba49 878}
12f38872 879EXPORT_SYMBOL(simple_write_begin);
afddba49 880
ad2a722f
BH
881/**
882 * simple_write_end - .write_end helper for non-block-device FSes
8e88bfba 883 * @file: See .write_end of address_space_operations
ad2a722f
BH
884 * @mapping: "
885 * @pos: "
886 * @len: "
887 * @copied: "
888 * @page: "
889 * @fsdata: "
890 *
891 * simple_write_end does the minimum needed for updating a page after writing is
892 * done. It has the same API signature as the .write_end of
893 * address_space_operations vector. So it can just be set onto .write_end for
894 * FSes that don't need any other processing. i_mutex is assumed to be held.
895 * Block based filesystems should use generic_write_end().
896 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
897 * is not called, so a filesystem that actually does store data in .write_inode
898 * should extend on what's done here with a call to mark_inode_dirty() in the
899 * case that i_size has changed.
04fff641 900 *
a77f580a 901 * Use *ONLY* with simple_read_folio()
ad2a722f 902 */
c1e3dbe9 903static int simple_write_end(struct file *file, struct address_space *mapping,
afddba49
NP
904 loff_t pos, unsigned len, unsigned copied,
905 struct page *page, void *fsdata)
906{
5522d9f7
MWO
907 struct folio *folio = page_folio(page);
908 struct inode *inode = folio->mapping->host;
ad2a722f 909 loff_t last_pos = pos + copied;
afddba49 910
5522d9f7
MWO
911 /* zero the stale part of the folio if we did a short copy */
912 if (!folio_test_uptodate(folio)) {
04fff641 913 if (copied < len) {
5522d9f7 914 size_t from = offset_in_folio(folio, pos);
afddba49 915
5522d9f7 916 folio_zero_range(folio, from + copied, len - copied);
04fff641 917 }
5522d9f7 918 folio_mark_uptodate(folio);
04fff641 919 }
ad2a722f
BH
920 /*
921 * No need to use i_size_read() here, the i_size
922 * cannot change under us because we hold the i_mutex.
923 */
924 if (last_pos > inode->i_size)
925 i_size_write(inode, last_pos);
afddba49 926
5522d9f7
MWO
927 folio_mark_dirty(folio);
928 folio_unlock(folio);
929 folio_put(folio);
afddba49
NP
930
931 return copied;
932}
c1e3dbe9
CH
933
934/*
935 * Provides ramfs-style behavior: data in the pagecache, but no writeback.
936 */
937const struct address_space_operations ram_aops = {
a77f580a 938 .read_folio = simple_read_folio,
c1e3dbe9
CH
939 .write_begin = simple_write_begin,
940 .write_end = simple_write_end,
46de8b97 941 .dirty_folio = noop_dirty_folio,
c1e3dbe9
CH
942};
943EXPORT_SYMBOL(ram_aops);
afddba49 944
1a1c9bb4
JL
945/*
946 * the inodes created here are not hashed. If you use iunique to generate
947 * unique inode values later for this filesystem, then you must take care
948 * to pass it an appropriate max_reserved value to avoid collisions.
949 */
7d683a09 950int simple_fill_super(struct super_block *s, unsigned long magic,
cda37124 951 const struct tree_descr *files)
1da177e4 952{
1da177e4 953 struct inode *inode;
1da177e4
LT
954 struct dentry *dentry;
955 int i;
956
09cbfeaf
KS
957 s->s_blocksize = PAGE_SIZE;
958 s->s_blocksize_bits = PAGE_SHIFT;
1da177e4 959 s->s_magic = magic;
759b9775 960 s->s_op = &simple_super_operations;
1da177e4
LT
961 s->s_time_gran = 1;
962
963 inode = new_inode(s);
964 if (!inode)
965 return -ENOMEM;
1a1c9bb4
JL
966 /*
967 * because the root inode is 1, the files array must not contain an
968 * entry at index 1
969 */
970 inode->i_ino = 1;
1da177e4 971 inode->i_mode = S_IFDIR | 0755;
077c212f 972 simple_inode_init_ts(inode);
1da177e4
LT
973 inode->i_op = &simple_dir_inode_operations;
974 inode->i_fop = &simple_dir_operations;
bfe86848 975 set_nlink(inode, 2);
715cd66a
AV
976 s->s_root = d_make_root(inode);
977 if (!s->s_root)
1da177e4 978 return -ENOMEM;
1da177e4
LT
979 for (i = 0; !files->name || files->name[0]; i++, files++) {
980 if (!files->name)
981 continue;
1a1c9bb4
JL
982
983 /* warn if it tries to conflict with the root inode */
984 if (unlikely(i == 1))
985 printk(KERN_WARNING "%s: %s passed in a files array"
986 "with an index of 1!\n", __func__,
987 s->s_type->name);
988
715cd66a 989 dentry = d_alloc_name(s->s_root, files->name);
1da177e4 990 if (!dentry)
715cd66a 991 return -ENOMEM;
1da177e4 992 inode = new_inode(s);
32096ea1
KK
993 if (!inode) {
994 dput(dentry);
715cd66a 995 return -ENOMEM;
32096ea1 996 }
1da177e4 997 inode->i_mode = S_IFREG | files->mode;
077c212f 998 simple_inode_init_ts(inode);
1da177e4
LT
999 inode->i_fop = files->ops;
1000 inode->i_ino = i;
1001 d_add(dentry, inode);
1002 }
1da177e4 1003 return 0;
1da177e4 1004}
12f38872 1005EXPORT_SYMBOL(simple_fill_super);
1da177e4
LT
1006
1007static DEFINE_SPINLOCK(pin_fs_lock);
1008
1f5ce9e9 1009int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
1da177e4
LT
1010{
1011 struct vfsmount *mnt = NULL;
1012 spin_lock(&pin_fs_lock);
1013 if (unlikely(!*mount)) {
1014 spin_unlock(&pin_fs_lock);
1751e8a6 1015 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
1da177e4
LT
1016 if (IS_ERR(mnt))
1017 return PTR_ERR(mnt);
1018 spin_lock(&pin_fs_lock);
1019 if (!*mount)
1020 *mount = mnt;
1021 }
1022 mntget(*mount);
1023 ++*count;
1024 spin_unlock(&pin_fs_lock);
1025 mntput(mnt);
1026 return 0;
1027}
12f38872 1028EXPORT_SYMBOL(simple_pin_fs);
1da177e4
LT
1029
1030void simple_release_fs(struct vfsmount **mount, int *count)
1031{
1032 struct vfsmount *mnt;
1033 spin_lock(&pin_fs_lock);
1034 mnt = *mount;
1035 if (!--*count)
1036 *mount = NULL;
1037 spin_unlock(&pin_fs_lock);
1038 mntput(mnt);
1039}
12f38872 1040EXPORT_SYMBOL(simple_release_fs);
1da177e4 1041
6d1029b5
AM
1042/**
1043 * simple_read_from_buffer - copy data from the buffer to user space
1044 * @to: the user space buffer to read to
1045 * @count: the maximum number of bytes to read
1046 * @ppos: the current position in the buffer
1047 * @from: the buffer to read from
1048 * @available: the size of the buffer
1049 *
1050 * The simple_read_from_buffer() function reads up to @count bytes from the
1051 * buffer @from at offset @ppos into the user space address starting at @to.
1052 *
1053 * On success, the number of bytes read is returned and the offset @ppos is
1054 * advanced by this number, or negative value is returned on error.
1055 **/
1da177e4
LT
1056ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
1057 const void *from, size_t available)
1058{
1059 loff_t pos = *ppos;
14be2746
SR
1060 size_t ret;
1061
1da177e4
LT
1062 if (pos < 0)
1063 return -EINVAL;
14be2746 1064 if (pos >= available || !count)
1da177e4
LT
1065 return 0;
1066 if (count > available - pos)
1067 count = available - pos;
14be2746
SR
1068 ret = copy_to_user(to, from + pos, count);
1069 if (ret == count)
1da177e4 1070 return -EFAULT;
14be2746 1071 count -= ret;
1da177e4
LT
1072 *ppos = pos + count;
1073 return count;
1074}
12f38872 1075EXPORT_SYMBOL(simple_read_from_buffer);
1da177e4 1076
6a727b43
JS
1077/**
1078 * simple_write_to_buffer - copy data from user space to the buffer
1079 * @to: the buffer to write to
1080 * @available: the size of the buffer
1081 * @ppos: the current position in the buffer
1082 * @from: the user space buffer to read from
1083 * @count: the maximum number of bytes to read
1084 *
1085 * The simple_write_to_buffer() function reads up to @count bytes from the user
1086 * space address starting at @from into the buffer @to at offset @ppos.
1087 *
1088 * On success, the number of bytes written is returned and the offset @ppos is
1089 * advanced by this number, or negative value is returned on error.
1090 **/
1091ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
1092 const void __user *from, size_t count)
1093{
1094 loff_t pos = *ppos;
1095 size_t res;
1096
1097 if (pos < 0)
1098 return -EINVAL;
1099 if (pos >= available || !count)
1100 return 0;
1101 if (count > available - pos)
1102 count = available - pos;
1103 res = copy_from_user(to + pos, from, count);
1104 if (res == count)
1105 return -EFAULT;
1106 count -= res;
1107 *ppos = pos + count;
1108 return count;
1109}
12f38872 1110EXPORT_SYMBOL(simple_write_to_buffer);
6a727b43 1111
6d1029b5
AM
1112/**
1113 * memory_read_from_buffer - copy data from the buffer
1114 * @to: the kernel space buffer to read to
1115 * @count: the maximum number of bytes to read
1116 * @ppos: the current position in the buffer
1117 * @from: the buffer to read from
1118 * @available: the size of the buffer
1119 *
1120 * The memory_read_from_buffer() function reads up to @count bytes from the
1121 * buffer @from at offset @ppos into the kernel space address starting at @to.
1122 *
1123 * On success, the number of bytes read is returned and the offset @ppos is
1124 * advanced by this number, or negative value is returned on error.
1125 **/
93b07113
AM
1126ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
1127 const void *from, size_t available)
1128{
1129 loff_t pos = *ppos;
1130
1131 if (pos < 0)
1132 return -EINVAL;
1133 if (pos >= available)
1134 return 0;
1135 if (count > available - pos)
1136 count = available - pos;
1137 memcpy(to, from + pos, count);
1138 *ppos = pos + count;
1139
1140 return count;
1141}
12f38872 1142EXPORT_SYMBOL(memory_read_from_buffer);
93b07113 1143
1da177e4
LT
1144/*
1145 * Transaction based IO.
1146 * The file expects a single write which triggers the transaction, and then
1147 * possibly a read which collects the result - which is stored in a
1148 * file-local buffer.
1149 */
76791ab2
IM
1150
1151void simple_transaction_set(struct file *file, size_t n)
1152{
1153 struct simple_transaction_argresp *ar = file->private_data;
1154
1155 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
1156
1157 /*
1158 * The barrier ensures that ar->size will really remain zero until
1159 * ar->data is ready for reading.
1160 */
1161 smp_mb();
1162 ar->size = n;
1163}
12f38872 1164EXPORT_SYMBOL(simple_transaction_set);
76791ab2 1165
1da177e4
LT
1166char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
1167{
1168 struct simple_transaction_argresp *ar;
1169 static DEFINE_SPINLOCK(simple_transaction_lock);
1170
1171 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
1172 return ERR_PTR(-EFBIG);
1173
1174 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
1175 if (!ar)
1176 return ERR_PTR(-ENOMEM);
1177
1178 spin_lock(&simple_transaction_lock);
1179
1180 /* only one write allowed per open */
1181 if (file->private_data) {
1182 spin_unlock(&simple_transaction_lock);
1183 free_page((unsigned long)ar);
1184 return ERR_PTR(-EBUSY);
1185 }
1186
1187 file->private_data = ar;
1188
1189 spin_unlock(&simple_transaction_lock);
1190
1191 if (copy_from_user(ar->data, buf, size))
1192 return ERR_PTR(-EFAULT);
1193
1194 return ar->data;
1195}
12f38872 1196EXPORT_SYMBOL(simple_transaction_get);
1da177e4
LT
1197
1198ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
1199{
1200 struct simple_transaction_argresp *ar = file->private_data;
1201
1202 if (!ar)
1203 return 0;
1204 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
1205}
12f38872 1206EXPORT_SYMBOL(simple_transaction_read);
1da177e4
LT
1207
1208int simple_transaction_release(struct inode *inode, struct file *file)
1209{
1210 free_page((unsigned long)file->private_data);
1211 return 0;
1212}
12f38872 1213EXPORT_SYMBOL(simple_transaction_release);
1da177e4 1214
acaefc25
AB
1215/* Simple attribute files */
1216
1217struct simple_attr {
8b88b099
CH
1218 int (*get)(void *, u64 *);
1219 int (*set)(void *, u64);
acaefc25
AB
1220 char get_buf[24]; /* enough to store a u64 and "\n\0" */
1221 char set_buf[24];
1222 void *data;
1223 const char *fmt; /* format for read operation */
7cf34c76 1224 struct mutex mutex; /* protects access to these buffers */
acaefc25
AB
1225};
1226
1227/* simple_attr_open is called by an actual attribute open file operation
1228 * to set the attribute specific access operations. */
1229int simple_attr_open(struct inode *inode, struct file *file,
8b88b099 1230 int (*get)(void *, u64 *), int (*set)(void *, u64),
acaefc25
AB
1231 const char *fmt)
1232{
1233 struct simple_attr *attr;
1234
a65cab7d 1235 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
acaefc25
AB
1236 if (!attr)
1237 return -ENOMEM;
1238
1239 attr->get = get;
1240 attr->set = set;
8e18e294 1241 attr->data = inode->i_private;
acaefc25 1242 attr->fmt = fmt;
7cf34c76 1243 mutex_init(&attr->mutex);
acaefc25
AB
1244
1245 file->private_data = attr;
1246
1247 return nonseekable_open(inode, file);
1248}
12f38872 1249EXPORT_SYMBOL_GPL(simple_attr_open);
acaefc25 1250
74bedc4d 1251int simple_attr_release(struct inode *inode, struct file *file)
acaefc25
AB
1252{
1253 kfree(file->private_data);
1254 return 0;
1255}
12f38872 1256EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
acaefc25
AB
1257
1258/* read from the buffer that is filled with the get function */
1259ssize_t simple_attr_read(struct file *file, char __user *buf,
1260 size_t len, loff_t *ppos)
1261{
1262 struct simple_attr *attr;
1263 size_t size;
1264 ssize_t ret;
1265
1266 attr = file->private_data;
1267
1268 if (!attr->get)
1269 return -EACCES;
1270
9261303a
CH
1271 ret = mutex_lock_interruptible(&attr->mutex);
1272 if (ret)
1273 return ret;
1274
a65cab7d
EB
1275 if (*ppos && attr->get_buf[0]) {
1276 /* continued read */
acaefc25 1277 size = strlen(attr->get_buf);
a65cab7d
EB
1278 } else {
1279 /* first read */
8b88b099
CH
1280 u64 val;
1281 ret = attr->get(attr->data, &val);
1282 if (ret)
1283 goto out;
1284
acaefc25 1285 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
8b88b099
CH
1286 attr->fmt, (unsigned long long)val);
1287 }
acaefc25
AB
1288
1289 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
8b88b099 1290out:
7cf34c76 1291 mutex_unlock(&attr->mutex);
acaefc25
AB
1292 return ret;
1293}
12f38872 1294EXPORT_SYMBOL_GPL(simple_attr_read);
acaefc25
AB
1295
1296/* interpret the buffer as a number to call the set function with */
2e41f274
AM
1297static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf,
1298 size_t len, loff_t *ppos, bool is_signed)
acaefc25
AB
1299{
1300 struct simple_attr *attr;
488dac0c 1301 unsigned long long val;
acaefc25
AB
1302 size_t size;
1303 ssize_t ret;
1304
1305 attr = file->private_data;
acaefc25
AB
1306 if (!attr->set)
1307 return -EACCES;
1308
9261303a
CH
1309 ret = mutex_lock_interruptible(&attr->mutex);
1310 if (ret)
1311 return ret;
1312
acaefc25
AB
1313 ret = -EFAULT;
1314 size = min(sizeof(attr->set_buf) - 1, len);
1315 if (copy_from_user(attr->set_buf, buf, size))
1316 goto out;
1317
acaefc25 1318 attr->set_buf[size] = '\0';
2e41f274
AM
1319 if (is_signed)
1320 ret = kstrtoll(attr->set_buf, 0, &val);
1321 else
1322 ret = kstrtoull(attr->set_buf, 0, &val);
488dac0c
YY
1323 if (ret)
1324 goto out;
05cc0cee
WF
1325 ret = attr->set(attr->data, val);
1326 if (ret == 0)
1327 ret = len; /* on success, claim we got the whole input */
acaefc25 1328out:
7cf34c76 1329 mutex_unlock(&attr->mutex);
acaefc25
AB
1330 return ret;
1331}
2e41f274
AM
1332
1333ssize_t simple_attr_write(struct file *file, const char __user *buf,
1334 size_t len, loff_t *ppos)
1335{
1336 return simple_attr_write_xsigned(file, buf, len, ppos, false);
1337}
12f38872 1338EXPORT_SYMBOL_GPL(simple_attr_write);
acaefc25 1339
2e41f274
AM
1340ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
1341 size_t len, loff_t *ppos)
1342{
1343 return simple_attr_write_xsigned(file, buf, len, ppos, true);
1344}
1345EXPORT_SYMBOL_GPL(simple_attr_write_signed);
1346
d9e5d922
AG
1347/**
1348 * generic_encode_ino32_fh - generic export_operations->encode_fh function
1349 * @inode: the object to encode
1350 * @fh: where to store the file handle fragment
1351 * @max_len: maximum length to store there (in 4 byte units)
1352 * @parent: parent directory inode, if wanted
1353 *
1354 * This generic encode_fh function assumes that the 32 inode number
1355 * is suitable for locating an inode, and that the generation number
1356 * can be used to check that it is still valid. It places them in the
1357 * filehandle fragment where export_decode_fh expects to find them.
1358 */
1359int generic_encode_ino32_fh(struct inode *inode, __u32 *fh, int *max_len,
1360 struct inode *parent)
1361{
1362 struct fid *fid = (void *)fh;
1363 int len = *max_len;
1364 int type = FILEID_INO32_GEN;
1365
1366 if (parent && (len < 4)) {
1367 *max_len = 4;
1368 return FILEID_INVALID;
1369 } else if (len < 2) {
1370 *max_len = 2;
1371 return FILEID_INVALID;
1372 }
1373
1374 len = 2;
1375 fid->i32.ino = inode->i_ino;
1376 fid->i32.gen = inode->i_generation;
1377 if (parent) {
1378 fid->i32.parent_ino = parent->i_ino;
1379 fid->i32.parent_gen = parent->i_generation;
1380 len = 4;
1381 type = FILEID_INO32_GEN_PARENT;
1382 }
1383 *max_len = len;
1384 return type;
1385}
1386EXPORT_SYMBOL_GPL(generic_encode_ino32_fh);
1387
2596110a
CH
1388/**
1389 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
1390 * @sb: filesystem to do the file handle conversion on
1391 * @fid: file handle to convert
1392 * @fh_len: length of the file handle in bytes
1393 * @fh_type: type of file handle
1394 * @get_inode: filesystem callback to retrieve inode
1395 *
1396 * This function decodes @fid as long as it has one of the well-known
1397 * Linux filehandle types and calls @get_inode on it to retrieve the
1398 * inode for the object specified in the file handle.
1399 */
1400struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
1401 int fh_len, int fh_type, struct inode *(*get_inode)
1402 (struct super_block *sb, u64 ino, u32 gen))
1403{
1404 struct inode *inode = NULL;
1405
1406 if (fh_len < 2)
1407 return NULL;
1408
1409 switch (fh_type) {
1410 case FILEID_INO32_GEN:
1411 case FILEID_INO32_GEN_PARENT:
1412 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
1413 break;
1414 }
1415
4ea3ada2 1416 return d_obtain_alias(inode);
2596110a
CH
1417}
1418EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
1419
1420/**
ca186830 1421 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
2596110a
CH
1422 * @sb: filesystem to do the file handle conversion on
1423 * @fid: file handle to convert
1424 * @fh_len: length of the file handle in bytes
1425 * @fh_type: type of file handle
1426 * @get_inode: filesystem callback to retrieve inode
1427 *
1428 * This function decodes @fid as long as it has one of the well-known
1429 * Linux filehandle types and calls @get_inode on it to retrieve the
1430 * inode for the _parent_ object specified in the file handle if it
1431 * is specified in the file handle, or NULL otherwise.
1432 */
1433struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
1434 int fh_len, int fh_type, struct inode *(*get_inode)
1435 (struct super_block *sb, u64 ino, u32 gen))
1436{
1437 struct inode *inode = NULL;
1438
1439 if (fh_len <= 2)
1440 return NULL;
1441
1442 switch (fh_type) {
1443 case FILEID_INO32_GEN_PARENT:
1444 inode = get_inode(sb, fid->i32.parent_ino,
1445 (fh_len > 3 ? fid->i32.parent_gen : 0));
1446 break;
1447 }
1448
4ea3ada2 1449 return d_obtain_alias(inode);
2596110a
CH
1450}
1451EXPORT_SYMBOL_GPL(generic_fh_to_parent);
1452
1b061d92 1453/**
ac13a829
FF
1454 * __generic_file_fsync - generic fsync implementation for simple filesystems
1455 *
1b061d92 1456 * @file: file to synchronize
ac13a829
FF
1457 * @start: start offset in bytes
1458 * @end: end offset in bytes (inclusive)
1b061d92
CH
1459 * @datasync: only synchronize essential metadata if true
1460 *
1461 * This is a generic implementation of the fsync method for simple
1462 * filesystems which track all non-inode metadata in the buffers list
1463 * hanging off the address_space structure.
1464 */
ac13a829
FF
1465int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
1466 int datasync)
d5aacad5 1467{
7ea80859 1468 struct inode *inode = file->f_mapping->host;
d5aacad5
AV
1469 int err;
1470 int ret;
1471
383aa543 1472 err = file_write_and_wait_range(file, start, end);
02c24a82
JB
1473 if (err)
1474 return err;
1475
5955102c 1476 inode_lock(inode);
d5aacad5 1477 ret = sync_mapping_buffers(inode->i_mapping);
0ae45f63 1478 if (!(inode->i_state & I_DIRTY_ALL))
02c24a82 1479 goto out;
d5aacad5 1480 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
02c24a82 1481 goto out;
d5aacad5 1482
c3765016 1483 err = sync_inode_metadata(inode, 1);
d5aacad5
AV
1484 if (ret == 0)
1485 ret = err;
ac13a829 1486
02c24a82 1487out:
5955102c 1488 inode_unlock(inode);
383aa543
JL
1489 /* check and advance again to catch errors after syncing out buffers */
1490 err = file_check_and_advance_wb_err(file);
1491 if (ret == 0)
1492 ret = err;
1493 return ret;
d5aacad5 1494}
ac13a829
FF
1495EXPORT_SYMBOL(__generic_file_fsync);
1496
1497/**
1498 * generic_file_fsync - generic fsync implementation for simple filesystems
1499 * with flush
1500 * @file: file to synchronize
1501 * @start: start offset in bytes
1502 * @end: end offset in bytes (inclusive)
1503 * @datasync: only synchronize essential metadata if true
1504 *
1505 */
1506
1507int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1508 int datasync)
1509{
1510 struct inode *inode = file->f_mapping->host;
1511 int err;
1512
1513 err = __generic_file_fsync(file, start, end, datasync);
1514 if (err)
1515 return err;
c6bf3f0e 1516 return blkdev_issue_flush(inode->i_sb->s_bdev);
ac13a829 1517}
1b061d92
CH
1518EXPORT_SYMBOL(generic_file_fsync);
1519
30ca22c7
PL
1520/**
1521 * generic_check_addressable - Check addressability of file system
1522 * @blocksize_bits: log of file system block size
1523 * @num_blocks: number of blocks in file system
1524 *
1525 * Determine whether a file system with @num_blocks blocks (and a
1526 * block size of 2**@blocksize_bits) is addressable by the sector_t
1527 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
1528 */
1529int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1530{
1531 u64 last_fs_block = num_blocks - 1;
a33f13ef 1532 u64 last_fs_page =
09cbfeaf 1533 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
30ca22c7
PL
1534
1535 if (unlikely(num_blocks == 0))
1536 return 0;
1537
09cbfeaf 1538 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
30ca22c7
PL
1539 return -EINVAL;
1540
a33f13ef
JB
1541 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1542 (last_fs_page > (pgoff_t)(~0ULL))) {
30ca22c7
PL
1543 return -EFBIG;
1544 }
1545 return 0;
1546}
1547EXPORT_SYMBOL(generic_check_addressable);
1548
1b061d92
CH
1549/*
1550 * No-op implementation of ->fsync for in-memory filesystems.
1551 */
02c24a82 1552int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1b061d92
CH
1553{
1554 return 0;
1555}
1b061d92 1556EXPORT_SYMBOL(noop_fsync);
87dc800b 1557
f44c7763
DW
1558ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1559{
1560 /*
1561 * iomap based filesystems support direct I/O without need for
1562 * this callback. However, it still needs to be set in
1563 * inode->a_ops so that open/fcntl know that direct I/O is
1564 * generally supported.
1565 */
1566 return -EINVAL;
1567}
1568EXPORT_SYMBOL_GPL(noop_direct_IO);
1569
fceef393
AV
1570/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1571void kfree_link(void *p)
87dc800b 1572{
fceef393 1573 kfree(p);
87dc800b 1574}
fceef393 1575EXPORT_SYMBOL(kfree_link);
6987843f 1576
6987843f
AV
1577struct inode *alloc_anon_inode(struct super_block *s)
1578{
1579 static const struct address_space_operations anon_aops = {
46de8b97 1580 .dirty_folio = noop_dirty_folio,
6987843f
AV
1581 };
1582 struct inode *inode = new_inode_pseudo(s);
1583
1584 if (!inode)
1585 return ERR_PTR(-ENOMEM);
1586
1587 inode->i_ino = get_next_ino();
1588 inode->i_mapping->a_ops = &anon_aops;
1589
1590 /*
1591 * Mark the inode dirty from the very beginning,
1592 * that way it will never be moved to the dirty
1593 * list because mark_inode_dirty() will think
1594 * that it already _is_ on the dirty list.
1595 */
1596 inode->i_state = I_DIRTY;
1597 inode->i_mode = S_IRUSR | S_IWUSR;
1598 inode->i_uid = current_fsuid();
1599 inode->i_gid = current_fsgid();
1600 inode->i_flags |= S_PRIVATE;
077c212f 1601 simple_inode_init_ts(inode);
6987843f
AV
1602 return inode;
1603}
1604EXPORT_SYMBOL(alloc_anon_inode);
1c994a09
JL
1605
1606/**
1607 * simple_nosetlease - generic helper for prohibiting leases
1608 * @filp: file pointer
1609 * @arg: type of lease to obtain
1610 * @flp: new lease supplied for insertion
e6f5c789 1611 * @priv: private data for lm_setup operation
1c994a09
JL
1612 *
1613 * Generic helper for filesystems that do not wish to allow leases to be set.
1614 * All arguments are ignored and it just returns -EINVAL.
1615 */
1616int
ed5f17f6 1617simple_nosetlease(struct file *filp, int arg, struct file_lock **flp,
e6f5c789 1618 void **priv)
1c994a09
JL
1619{
1620 return -EINVAL;
1621}
1622EXPORT_SYMBOL(simple_nosetlease);
61ba64fc 1623
6ee9706a
EB
1624/**
1625 * simple_get_link - generic helper to get the target of "fast" symlinks
1626 * @dentry: not used here
1627 * @inode: the symlink inode
1628 * @done: not used here
1629 *
1630 * Generic helper for filesystems to use for symlink inodes where a pointer to
1631 * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
1632 * since as an optimization the path lookup code uses any non-NULL ->i_link
1633 * directly, without calling ->get_link(). But ->get_link() still must be set,
1634 * to mark the inode_operations as being for a symlink.
1635 *
1636 * Return: the symlink target
1637 */
6b255391 1638const char *simple_get_link(struct dentry *dentry, struct inode *inode,
fceef393 1639 struct delayed_call *done)
61ba64fc 1640{
6b255391 1641 return inode->i_link;
61ba64fc 1642}
6b255391 1643EXPORT_SYMBOL(simple_get_link);
61ba64fc
AV
1644
1645const struct inode_operations simple_symlink_inode_operations = {
6b255391 1646 .get_link = simple_get_link,
61ba64fc
AV
1647};
1648EXPORT_SYMBOL(simple_symlink_inode_operations);
fbabfd0f
EB
1649
1650/*
1651 * Operations for a permanently empty directory.
1652 */
1653static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1654{
1655 return ERR_PTR(-ENOENT);
1656}
1657
b74d24f7 1658static int empty_dir_getattr(struct mnt_idmap *idmap,
549c7297 1659 const struct path *path, struct kstat *stat,
a528d35e 1660 u32 request_mask, unsigned int query_flags)
fbabfd0f 1661{
a528d35e 1662 struct inode *inode = d_inode(path->dentry);
0d72b928 1663 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
fbabfd0f
EB
1664 return 0;
1665}
1666
c1632a0f 1667static int empty_dir_setattr(struct mnt_idmap *idmap,
549c7297 1668 struct dentry *dentry, struct iattr *attr)
fbabfd0f
EB
1669{
1670 return -EPERM;
1671}
1672
fbabfd0f
EB
1673static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1674{
1675 return -EOPNOTSUPP;
1676}
1677
1678static const struct inode_operations empty_dir_inode_operations = {
1679 .lookup = empty_dir_lookup,
1680 .permission = generic_permission,
1681 .setattr = empty_dir_setattr,
1682 .getattr = empty_dir_getattr,
fbabfd0f
EB
1683 .listxattr = empty_dir_listxattr,
1684};
1685
1686static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1687{
1688 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1689 return generic_file_llseek_size(file, offset, whence, 2, 2);
1690}
1691
1692static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1693{
1694 dir_emit_dots(file, ctx);
1695 return 0;
1696}
1697
1698static const struct file_operations empty_dir_operations = {
1699 .llseek = empty_dir_llseek,
1700 .read = generic_read_dir,
c51da20c 1701 .iterate_shared = empty_dir_readdir,
fbabfd0f
EB
1702 .fsync = noop_fsync,
1703};
1704
1705
1706void make_empty_dir_inode(struct inode *inode)
1707{
1708 set_nlink(inode, 2);
1709 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1710 inode->i_uid = GLOBAL_ROOT_UID;
1711 inode->i_gid = GLOBAL_ROOT_GID;
1712 inode->i_rdev = 0;
4b75de86 1713 inode->i_size = 0;
fbabfd0f
EB
1714 inode->i_blkbits = PAGE_SHIFT;
1715 inode->i_blocks = 0;
1716
1717 inode->i_op = &empty_dir_inode_operations;
f5c24438 1718 inode->i_opflags &= ~IOP_XATTR;
fbabfd0f
EB
1719 inode->i_fop = &empty_dir_operations;
1720}
1721
1722bool is_empty_dir_inode(struct inode *inode)
1723{
1724 return (inode->i_fop == &empty_dir_operations) &&
1725 (inode->i_op == &empty_dir_inode_operations);
1726}
c843843e 1727
5298d4bf 1728#if IS_ENABLED(CONFIG_UNICODE)
c843843e
DR
1729/**
1730 * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
1731 * @dentry: dentry whose name we are checking against
1732 * @len: len of name of dentry
1733 * @str: str pointer to name of dentry
1734 * @name: Name to compare against
1735 *
1736 * Return: 0 if names match, 1 if mismatch, or -ERRNO
1737 */
794c43f7
EB
1738static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
1739 const char *str, const struct qstr *name)
c843843e 1740{
0906fbb2
GKB
1741 const struct dentry *parent;
1742 const struct inode *dir;
c843843e 1743 char strbuf[DNAME_INLINE_LEN];
0906fbb2
GKB
1744 struct qstr qstr;
1745
1746 /*
1747 * Attempt a case-sensitive match first. It is cheaper and
1748 * should cover most lookups, including all the sane
1749 * applications that expect a case-sensitive filesystem.
1750 *
1751 * This comparison is safe under RCU because the caller
1752 * guarantees the consistency between str and len. See
1753 * __d_lookup_rcu_op_compare() for details.
1754 */
1755 if (len == name->len && !memcmp(str, name->name, len))
1756 return 0;
c843843e 1757
0906fbb2
GKB
1758 parent = READ_ONCE(dentry->d_parent);
1759 dir = READ_ONCE(parent->d_inode);
af494af3 1760 if (!dir || !IS_CASEFOLDED(dir))
0906fbb2
GKB
1761 return 1;
1762
c843843e
DR
1763 /*
1764 * If the dentry name is stored in-line, then it may be concurrently
1765 * modified by a rename. If this happens, the VFS will eventually retry
1766 * the lookup, so it doesn't matter what ->d_compare() returns.
1767 * However, it's unsafe to call utf8_strncasecmp() with an unstable
1768 * string. Therefore, we have to copy the name into a temporary buffer.
1769 */
1770 if (len <= DNAME_INLINE_LEN - 1) {
1771 memcpy(strbuf, str, len);
1772 strbuf[len] = 0;
0906fbb2 1773 str = strbuf;
c843843e
DR
1774 /* prevent compiler from optimizing out the temporary buffer */
1775 barrier();
1776 }
0906fbb2
GKB
1777 qstr.len = len;
1778 qstr.name = str;
c843843e 1779
0906fbb2 1780 return utf8_strncasecmp(dentry->d_sb->s_encoding, name, &qstr);
c843843e 1781}
c843843e
DR
1782
1783/**
1784 * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
1785 * @dentry: dentry of the parent directory
1786 * @str: qstr of name whose hash we should fill in
1787 *
1788 * Return: 0 if hash was successful or unchanged, and -EINVAL on error
1789 */
794c43f7 1790static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
c843843e
DR
1791{
1792 const struct inode *dir = READ_ONCE(dentry->d_inode);
1793 struct super_block *sb = dentry->d_sb;
1794 const struct unicode_map *um = sb->s_encoding;
bae8bc46 1795 int ret;
c843843e 1796
af494af3 1797 if (!dir || !IS_CASEFOLDED(dir))
c843843e
DR
1798 return 0;
1799
1800 ret = utf8_casefold_hash(um, dentry, str);
1801 if (ret < 0 && sb_has_strict_encoding(sb))
1802 return -EINVAL;
1803 return 0;
1804}
608af703
DR
1805
1806static const struct dentry_operations generic_ci_dentry_ops = {
1807 .d_hash = generic_ci_d_hash,
1808 .d_compare = generic_ci_d_compare,
608af703 1809#ifdef CONFIG_FS_ENCRYPTION
608af703 1810 .d_revalidate = fscrypt_d_revalidate,
e6ca2883 1811#endif
608af703
DR
1812};
1813#endif
1814
e6ca2883
GKB
1815#ifdef CONFIG_FS_ENCRYPTION
1816static const struct dentry_operations generic_encrypted_dentry_ops = {
608af703
DR
1817 .d_revalidate = fscrypt_d_revalidate,
1818};
1819#endif
1820
1821/**
70dfe3f0
GKB
1822 * generic_set_sb_d_ops - helper for choosing the set of
1823 * filesystem-wide dentry operations for the enabled features
1824 * @sb: superblock to be configured
608af703 1825 *
70dfe3f0
GKB
1826 * Filesystems supporting casefolding and/or fscrypt can call this
1827 * helper at mount-time to configure sb->s_d_op to best set of dentry
1828 * operations required for the enabled features. The helper must be
1829 * called after these have been configured, but before the root dentry
1830 * is created.
608af703 1831 */
70dfe3f0 1832void generic_set_sb_d_ops(struct super_block *sb)
608af703 1833{
5298d4bf 1834#if IS_ENABLED(CONFIG_UNICODE)
70dfe3f0
GKB
1835 if (sb->s_encoding) {
1836 sb->s_d_op = &generic_ci_dentry_ops;
608af703
DR
1837 return;
1838 }
c843843e 1839#endif
608af703 1840#ifdef CONFIG_FS_ENCRYPTION
70dfe3f0
GKB
1841 if (sb->s_cop) {
1842 sb->s_d_op = &generic_encrypted_dentry_ops;
608af703
DR
1843 return;
1844 }
1845#endif
1846}
70dfe3f0 1847EXPORT_SYMBOL(generic_set_sb_d_ops);
5ca14835
AM
1848
1849/**
1850 * inode_maybe_inc_iversion - increments i_version
1851 * @inode: inode with the i_version that should be updated
1852 * @force: increment the counter even if it's not necessary?
1853 *
1854 * Every time the inode is modified, the i_version field must be seen to have
1855 * changed by any observer.
1856 *
1857 * If "force" is set or the QUERIED flag is set, then ensure that we increment
1858 * the value, and clear the queried flag.
1859 *
1860 * In the common case where neither is set, then we can return "false" without
1861 * updating i_version.
1862 *
1863 * If this function returns false, and no other metadata has changed, then we
1864 * can avoid logging the metadata.
1865 */
1866bool inode_maybe_inc_iversion(struct inode *inode, bool force)
1867{
1868 u64 cur, new;
1869
1870 /*
1871 * The i_version field is not strictly ordered with any other inode
1872 * information, but the legacy inode_inc_iversion code used a spinlock
1873 * to serialize increments.
1874 *
1875 * Here, we add full memory barriers to ensure that any de-facto
1876 * ordering with other info is preserved.
1877 *
1878 * This barrier pairs with the barrier in inode_query_iversion()
1879 */
1880 smp_mb();
1881 cur = inode_peek_iversion_raw(inode);
1882 do {
1883 /* If flag is clear then we needn't do anything */
1884 if (!force && !(cur & I_VERSION_QUERIED))
1885 return false;
1886
1887 /* Since lowest bit is flag, add 2 to avoid it */
1888 new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT;
1889 } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
1890 return true;
1891}
1892EXPORT_SYMBOL(inode_maybe_inc_iversion);
c5bc1b3f
JL
1893
1894/**
1895 * inode_query_iversion - read i_version for later use
1896 * @inode: inode from which i_version should be read
1897 *
1898 * Read the inode i_version counter. This should be used by callers that wish
1899 * to store the returned i_version for later comparison. This will guarantee
1900 * that a later query of the i_version will result in a different value if
1901 * anything has changed.
1902 *
1903 * In this implementation, we fetch the current value, set the QUERIED flag and
1904 * then try to swap it into place with a cmpxchg, if it wasn't already set. If
1905 * that fails, we try again with the newly fetched value from the cmpxchg.
1906 */
1907u64 inode_query_iversion(struct inode *inode)
1908{
1909 u64 cur, new;
1910
1911 cur = inode_peek_iversion_raw(inode);
1912 do {
1913 /* If flag is already set, then no need to swap */
1914 if (cur & I_VERSION_QUERIED) {
1915 /*
1916 * This barrier (and the implicit barrier in the
1917 * cmpxchg below) pairs with the barrier in
1918 * inode_maybe_inc_iversion().
1919 */
1920 smp_mb();
1921 break;
1922 }
1923
1924 new = cur | I_VERSION_QUERIED;
1925 } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
1926 return cur >> I_VERSION_QUERIED_SHIFT;
1927}
1928EXPORT_SYMBOL(inode_query_iversion);
44fff0fa
CH
1929
1930ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
1931 ssize_t direct_written, ssize_t buffered_written)
1932{
1933 struct address_space *mapping = iocb->ki_filp->f_mapping;
1934 loff_t pos = iocb->ki_pos - buffered_written;
1935 loff_t end = iocb->ki_pos - 1;
1936 int err;
1937
1938 /*
1939 * If the buffered write fallback returned an error, we want to return
1940 * the number of bytes which were written by direct I/O, or the error
1941 * code if that was zero.
1942 *
1943 * Note that this differs from normal direct-io semantics, which will
1944 * return -EFOO even if some bytes were written.
1945 */
1946 if (unlikely(buffered_written < 0)) {
1947 if (direct_written)
1948 return direct_written;
1949 return buffered_written;
1950 }
1951
1952 /*
1953 * We need to ensure that the page cache pages are written to disk and
1954 * invalidated to preserve the expected O_DIRECT semantics.
1955 */
1956 err = filemap_write_and_wait_range(mapping, pos, end);
1957 if (err < 0) {
1958 /*
1959 * We don't know how much we wrote, so just return the number of
1960 * bytes which were direct-written
1961 */
8287474a 1962 iocb->ki_pos -= buffered_written;
44fff0fa
CH
1963 if (direct_written)
1964 return direct_written;
1965 return err;
1966 }
1967 invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
1968 return direct_written + buffered_written;
1969}
1970EXPORT_SYMBOL_GPL(direct_write_fallback);
077c212f
JL
1971
1972/**
1973 * simple_inode_init_ts - initialize the timestamps for a new inode
1974 * @inode: inode to be initialized
1975 *
1976 * When a new inode is created, most filesystems set the timestamps to the
1977 * current time. Add a helper to do this.
1978 */
1979struct timespec64 simple_inode_init_ts(struct inode *inode)
1980{
1981 struct timespec64 ts = inode_set_ctime_current(inode);
1982
1983 inode_set_atime_to_ts(inode, ts);
1984 inode_set_mtime_to_ts(inode, ts);
1985 return ts;
1986}
1987EXPORT_SYMBOL(simple_inode_init_ts);