| 1 | |
| 2 | |
| 3 | * fs/libfs.c
|
| 4 | * Library for filesystems writers.
|
| 5 |
|
| 6 |
|
| 7 | #include <linux/blkdev.h>
|
| 8 | #include <linux/export.h>
|
| 9 | #include <linux/filelock.h>
|
| 10 | #include <linux/pagemap.h>
|
| 11 | #include <linux/slab.h>
|
| 12 | #include <linux/cred.h>
|
| 13 | #include <linux/mount.h>
|
| 14 | #include <linux/vfs.h>
|
| 15 | #include <linux/quotaops.h>
|
| 16 | #include <linux/mutex.h>
|
| 17 | #include <linux/namei.h>
|
| 18 | #include <linux/exportfs.h>
|
| 19 | #include <linux/iversion.h>
|
| 20 | #include <linux/writeback.h>
|
| 21 | #include <linux/fs_context.h>
|
| 22 | #include <linux/pseudo_fs.h>
|
| 23 | #include <linux/fsnotify.h>
|
| 24 | #include <linux/unicode.h>
|
| 25 | #include <linux/fscrypt.h>
|
| 26 | #include <linux/pidfs.h>
|
| 27 |
|
| 28 | #include <linux/uaccess.h>
|
| 29 |
|
| 30 | #include "internal.h"
|
| 31 |
|
| 32 | int simple_getattr(struct mnt_idmap *idmap, const struct path *path,
|
| 33 | struct kstat *stat, u32 request_mask,
|
| 34 | unsigned int query_flags)
|
| 35 | {
|
| 36 | struct inode *inode = d_inode(path->dentry);
|
| 37 | generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
|
| 38 | stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
|
| 39 | return 0;
|
| 40 | }
|
| 41 | EXPORT_SYMBOL(simple_getattr);
|
| 42 |
|
| 43 | int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
|
| 44 | {
|
| 45 | u64 id = huge_encode_dev(dentry->d_sb->s_dev);
|
| 46 |
|
| 47 | buf->f_fsid = u64_to_fsid(id);
|
| 48 | buf->f_type = dentry->d_sb->s_magic;
|
| 49 | buf->f_bsize = PAGE_SIZE;
|
| 50 | buf->f_namelen = NAME_MAX;
|
| 51 | return 0;
|
| 52 | }
|
| 53 | EXPORT_SYMBOL(simple_statfs);
|
| 54 |
|
| 55 | |
| 56 | * Retaining negative dentries for an in-memory filesystem just wastes
|
| 57 | * memory and lookup time: arrange for them to be deleted immediately.
|
| 58 |
|
| 59 | int always_delete_dentry(const struct dentry *dentry)
|
| 60 | {
|
| 61 | return 1;
|
| 62 | }
|
| 63 | EXPORT_SYMBOL(always_delete_dentry);
|
| 64 |
|
| 65 | |
| 66 | * Lookup the data. This is trivial - if the dentry didn't already
|
| 67 | * exist, we know it is negative. Set d_op to delete negative dentries.
|
| 68 |
|
| 69 | struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
|
| 70 | {
|
| 71 | if (dentry->d_name.len > NAME_MAX)
|
| 72 | return ERR_PTR(-ENAMETOOLONG);
|
| 73 | if (!dentry->d_op && !(dentry->d_flags & DCACHE_DONTCACHE)) {
|
| 74 | spin_lock(&dentry->d_lock);
|
| 75 | dentry->d_flags |= DCACHE_DONTCACHE;
|
| 76 | spin_unlock(&dentry->d_lock);
|
| 77 | }
|
| 78 | if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir))
|
| 79 | return NULL;
|
| 80 |
|
| 81 | return d_splice_alias(NULL, dentry);
|
| 82 | }
|
| 83 | EXPORT_SYMBOL(simple_lookup);
|
| 84 |
|
| 85 | int dcache_dir_open(struct inode *inode, struct file *file)
|
| 86 | {
|
| 87 | file->private_data = d_alloc_cursor(file->f_path.dentry);
|
| 88 |
|
| 89 | return file->private_data ? 0 : -ENOMEM;
|
| 90 | }
|
| 91 | EXPORT_SYMBOL(dcache_dir_open);
|
| 92 |
|
| 93 | int dcache_dir_close(struct inode *inode, struct file *file)
|
| 94 | {
|
| 95 | dput(file->private_data);
|
| 96 | return 0;
|
| 97 | }
|
| 98 | EXPORT_SYMBOL(dcache_dir_close);
|
| 99 |
|
| 100 |
|
| 101 | |
| 102 | * Returns an element of siblings' list.
|
| 103 | * We are looking for <count>th positive after <p>; if
|
| 104 | * found, dentry is grabbed and returned to caller.
|
| 105 | * If no such element exists, NULL is returned.
|
| 106 |
|
| 107 | static struct dentry *scan_positives(struct dentry *cursor,
|
| 108 | struct hlist_node **p,
|
| 109 | loff_t count,
|
| 110 | struct dentry *last)
|
| 111 | {
|
| 112 | struct dentry *dentry = cursor->d_parent, *found = NULL;
|
| 113 |
|
| 114 | spin_lock(&dentry->d_lock);
|
| 115 | while (*p) {
|
| 116 | struct dentry *d = hlist_entry(*p, struct dentry, d_sib);
|
| 117 | p = &d->d_sib.next;
|
| 118 | |
| 119 | if (d->d_flags & DCACHE_DENTRY_CURSOR)
|
| 120 | continue;
|
| 121 | if (simple_positive(d) && !--count) {
|
| 122 | spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
|
| 123 | if (simple_positive(d))
|
| 124 | found = dget_dlock(d);
|
| 125 | spin_unlock(&d->d_lock);
|
| 126 | if (likely(found))
|
| 127 | break;
|
| 128 | count = 1;
|
| 129 | }
|
| 130 | if (need_resched()) {
|
| 131 | if (!hlist_unhashed(&cursor->d_sib))
|
| 132 | __hlist_del(&cursor->d_sib);
|
| 133 | hlist_add_behind(&cursor->d_sib, &d->d_sib);
|
| 134 | p = &cursor->d_sib.next;
|
| 135 | spin_unlock(&dentry->d_lock);
|
| 136 | cond_resched();
|
| 137 | spin_lock(&dentry->d_lock);
|
| 138 | }
|
| 139 | }
|
| 140 | spin_unlock(&dentry->d_lock);
|
| 141 | dput(last);
|
| 142 | return found;
|
| 143 | }
|
| 144 |
|
| 145 | loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
|
| 146 | {
|
| 147 | struct dentry *dentry = file->f_path.dentry;
|
| 148 | switch (whence) {
|
| 149 | case 1:
|
| 150 | offset += file->f_pos;
|
| 151 | fallthrough;
|
| 152 | case 0:
|
| 153 | if (offset >= 0)
|
| 154 | break;
|
| 155 | fallthrough;
|
| 156 | default:
|
| 157 | return -EINVAL;
|
| 158 | }
|
| 159 | if (offset != file->f_pos) {
|
| 160 | struct dentry *cursor = file->private_data;
|
| 161 | struct dentry *to = NULL;
|
| 162 |
|
| 163 | inode_lock_shared(dentry->d_inode);
|
| 164 |
|
| 165 | if (offset > 2)
|
| 166 | to = scan_positives(cursor, &dentry->d_children.first,
|
| 167 | offset - 2, NULL);
|
| 168 | spin_lock(&dentry->d_lock);
|
| 169 | hlist_del_init(&cursor->d_sib);
|
| 170 | if (to)
|
| 171 | hlist_add_behind(&cursor->d_sib, &to->d_sib);
|
| 172 | spin_unlock(&dentry->d_lock);
|
| 173 | dput(to);
|
| 174 |
|
| 175 | file->f_pos = offset;
|
| 176 |
|
| 177 | inode_unlock_shared(dentry->d_inode);
|
| 178 | }
|
| 179 | return offset;
|
| 180 | }
|
| 181 | EXPORT_SYMBOL(dcache_dir_lseek);
|
| 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 |
|
| 189 | int dcache_readdir(struct file *file, struct dir_context *ctx)
|
| 190 | {
|
| 191 | struct dentry *dentry = file->f_path.dentry;
|
| 192 | struct dentry *cursor = file->private_data;
|
| 193 | struct dentry *next = NULL;
|
| 194 | struct hlist_node **p;
|
| 195 |
|
| 196 | if (!dir_emit_dots(file, ctx))
|
| 197 | return 0;
|
| 198 |
|
| 199 | if (ctx->pos == 2)
|
| 200 | p = &dentry->d_children.first;
|
| 201 | else
|
| 202 | p = &cursor->d_sib.next;
|
| 203 |
|
| 204 | while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
|
| 205 | if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
|
| 206 | d_inode(next)->i_ino,
|
| 207 | fs_umode_to_dtype(d_inode(next)->i_mode)))
|
| 208 | break;
|
| 209 | ctx->pos++;
|
| 210 | p = &next->d_sib.next;
|
| 211 | }
|
| 212 | spin_lock(&dentry->d_lock);
|
| 213 | hlist_del_init(&cursor->d_sib);
|
| 214 | if (next)
|
| 215 | hlist_add_before(&cursor->d_sib, &next->d_sib);
|
| 216 | spin_unlock(&dentry->d_lock);
|
| 217 | dput(next);
|
| 218 |
|
| 219 | return 0;
|
| 220 | }
|
| 221 | EXPORT_SYMBOL(dcache_readdir);
|
| 222 |
|
| 223 | ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
|
| 224 | {
|
| 225 | return -EISDIR;
|
| 226 | }
|
| 227 | EXPORT_SYMBOL(generic_read_dir);
|
| 228 |
|
| 229 | const struct file_operations simple_dir_operations = {
|
| 230 | .open = dcache_dir_open,
|
| 231 | .release = dcache_dir_close,
|
| 232 | .llseek = dcache_dir_lseek,
|
| 233 | .read = generic_read_dir,
|
| 234 | .iterate_shared = dcache_readdir,
|
| 235 | .fsync = noop_fsync,
|
| 236 | };
|
| 237 | EXPORT_SYMBOL(simple_dir_operations);
|
| 238 |
|
| 239 | const struct inode_operations simple_dir_inode_operations = {
|
| 240 | .lookup = simple_lookup,
|
| 241 | };
|
| 242 | EXPORT_SYMBOL(simple_dir_inode_operations);
|
| 243 |
|
| 244 |
|
| 245 | enum {
|
| 246 | DIR_OFFSET_FIRST = 2,
|
| 247 | DIR_OFFSET_EOD = S32_MAX,
|
| 248 | };
|
| 249 |
|
| 250 |
|
| 251 | enum {
|
| 252 | DIR_OFFSET_MIN = DIR_OFFSET_FIRST + 1,
|
| 253 | DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1,
|
| 254 | };
|
| 255 |
|
| 256 | static void offset_set(struct dentry *dentry, long offset)
|
| 257 | {
|
| 258 | dentry->d_fsdata = (void *)offset;
|
| 259 | }
|
| 260 |
|
| 261 | static long dentry2offset(struct dentry *dentry)
|
| 262 | {
|
| 263 | return (long)dentry->d_fsdata;
|
| 264 | }
|
| 265 |
|
| 266 | static struct lock_class_key simple_offset_lock_class;
|
| 267 |
|
| 268 | |
| 269 | * simple_offset_init - initialize an offset_ctx
|
| 270 | * @octx: directory offset map to be initialized
|
| 271 | *
|
| 272 |
|
| 273 | void simple_offset_init(struct offset_ctx *octx)
|
| 274 | {
|
| 275 | mt_init_flags(&octx->mt, MT_FLAGS_ALLOC_RANGE);
|
| 276 | lockdep_set_class(&octx->mt.ma_lock, &simple_offset_lock_class);
|
| 277 | octx->next_offset = DIR_OFFSET_MIN;
|
| 278 | }
|
| 279 |
|
| 280 | |
| 281 | * simple_offset_add - Add an entry to a directory's offset map
|
| 282 | * @octx: directory offset ctx to be updated
|
| 283 | * @dentry: new dentry being added
|
| 284 | *
|
| 285 | * Returns zero on success. @octx and the dentry's offset are updated.
|
| 286 | * Otherwise, a negative errno value is returned.
|
| 287 |
|
| 288 | int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
|
| 289 | {
|
| 290 | unsigned long offset;
|
| 291 | int ret;
|
| 292 |
|
| 293 | if (dentry2offset(dentry) != 0)
|
| 294 | return -EBUSY;
|
| 295 |
|
| 296 | ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN,
|
| 297 | DIR_OFFSET_MAX, &octx->next_offset,
|
| 298 | GFP_KERNEL);
|
| 299 | if (unlikely(ret < 0))
|
| 300 | return ret == -EBUSY ? -ENOSPC : ret;
|
| 301 |
|
| 302 | offset_set(dentry, offset);
|
| 303 | return 0;
|
| 304 | }
|
| 305 |
|
| 306 | static int simple_offset_replace(struct offset_ctx *octx, struct dentry *dentry,
|
| 307 | long offset)
|
| 308 | {
|
| 309 | int ret;
|
| 310 |
|
| 311 | ret = mtree_store(&octx->mt, offset, dentry, GFP_KERNEL);
|
| 312 | if (ret)
|
| 313 | return ret;
|
| 314 | offset_set(dentry, offset);
|
| 315 | return 0;
|
| 316 | }
|
| 317 |
|
| 318 | |
| 319 | * simple_offset_remove - Remove an entry to a directory's offset map
|
| 320 | * @octx: directory offset ctx to be updated
|
| 321 | * @dentry: dentry being removed
|
| 322 | *
|
| 323 |
|
| 324 | void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry)
|
| 325 | {
|
| 326 | long offset;
|
| 327 |
|
| 328 | offset = dentry2offset(dentry);
|
| 329 | if (offset == 0)
|
| 330 | return;
|
| 331 |
|
| 332 | mtree_erase(&octx->mt, offset);
|
| 333 | offset_set(dentry, 0);
|
| 334 | }
|
| 335 |
|
| 336 | |
| 337 | * simple_offset_rename - handle directory offsets for rename
|
| 338 | * @old_dir: parent directory of source entry
|
| 339 | * @old_dentry: dentry of source entry
|
| 340 | * @new_dir: parent_directory of destination entry
|
| 341 | * @new_dentry: dentry of destination
|
| 342 | *
|
| 343 | * Caller provides appropriate serialization.
|
| 344 | *
|
| 345 | * User space expects the directory offset value of the replaced
|
| 346 | * (new) directory entry to be unchanged after a rename.
|
| 347 | *
|
| 348 | * Caller must have grabbed a slot for new_dentry in the maple_tree
|
| 349 | * associated with new_dir, even if dentry is negative.
|
| 350 |
|
| 351 | void simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry,
|
| 352 | struct inode *new_dir, struct dentry *new_dentry)
|
| 353 | {
|
| 354 | struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir);
|
| 355 | struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir);
|
| 356 | long new_offset = dentry2offset(new_dentry);
|
| 357 |
|
| 358 | if (WARN_ON(!new_offset))
|
| 359 | return;
|
| 360 |
|
| 361 | simple_offset_remove(old_ctx, old_dentry);
|
| 362 | offset_set(new_dentry, 0);
|
| 363 | WARN_ON(simple_offset_replace(new_ctx, old_dentry, new_offset));
|
| 364 | }
|
| 365 |
|
| 366 | |
| 367 | * simple_offset_rename_exchange - exchange rename with directory offsets
|
| 368 | * @old_dir: parent of dentry being moved
|
| 369 | * @old_dentry: dentry being moved
|
| 370 | * @new_dir: destination parent
|
| 371 | * @new_dentry: destination dentry
|
| 372 | *
|
| 373 | * This API preserves the directory offset values. Caller provides
|
| 374 | * appropriate serialization.
|
| 375 | *
|
| 376 | * Returns zero on success. Otherwise a negative errno is returned and the
|
| 377 | * rename is rolled back.
|
| 378 |
|
| 379 | int simple_offset_rename_exchange(struct inode *old_dir,
|
| 380 | struct dentry *old_dentry,
|
| 381 | struct inode *new_dir,
|
| 382 | struct dentry *new_dentry)
|
| 383 | {
|
| 384 | struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir);
|
| 385 | struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir);
|
| 386 | long old_index = dentry2offset(old_dentry);
|
| 387 | long new_index = dentry2offset(new_dentry);
|
| 388 | int ret;
|
| 389 |
|
| 390 | if (WARN_ON(!old_index || !new_index))
|
| 391 | return -EINVAL;
|
| 392 |
|
| 393 | ret = mtree_store(&new_ctx->mt, new_index, old_dentry, GFP_KERNEL);
|
| 394 | if (WARN_ON(ret))
|
| 395 | return ret;
|
| 396 |
|
| 397 | ret = mtree_store(&old_ctx->mt, old_index, new_dentry, GFP_KERNEL);
|
| 398 | if (WARN_ON(ret)) {
|
| 399 | mtree_store(&new_ctx->mt, new_index, new_dentry, GFP_KERNEL);
|
| 400 | return ret;
|
| 401 | }
|
| 402 |
|
| 403 | offset_set(old_dentry, new_index);
|
| 404 | offset_set(new_dentry, old_index);
|
| 405 | simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
|
| 406 | return 0;
|
| 407 | }
|
| 408 |
|
| 409 | |
| 410 | * simple_offset_destroy - Release offset map
|
| 411 | * @octx: directory offset ctx that is about to be destroyed
|
| 412 | *
|
| 413 | * During fs teardown (eg. umount), a directory's offset map might still
|
| 414 | * contain entries. xa_destroy() cleans out anything that remains.
|
| 415 |
|
| 416 | void simple_offset_destroy(struct offset_ctx *octx)
|
| 417 | {
|
| 418 | mtree_destroy(&octx->mt);
|
| 419 | }
|
| 420 |
|
| 421 | |
| 422 | * offset_dir_llseek - Advance the read position of a directory descriptor
|
| 423 | * @file: an open directory whose position is to be updated
|
| 424 | * @offset: a byte offset
|
| 425 | * @whence: enumerator describing the starting position for this update
|
| 426 | *
|
| 427 | * SEEK_END, SEEK_DATA, and SEEK_HOLE are not supported for directories.
|
| 428 | *
|
| 429 | * Returns the updated read position if successful; otherwise a
|
| 430 | * negative errno is returned and the read position remains unchanged.
|
| 431 |
|
| 432 | static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
|
| 433 | {
|
| 434 | switch (whence) {
|
| 435 | case SEEK_CUR:
|
| 436 | offset += file->f_pos;
|
| 437 | fallthrough;
|
| 438 | case SEEK_SET:
|
| 439 | if (offset >= 0)
|
| 440 | break;
|
| 441 | fallthrough;
|
| 442 | default:
|
| 443 | return -EINVAL;
|
| 444 | }
|
| 445 |
|
| 446 | return vfs_setpos(file, offset, LONG_MAX);
|
| 447 | }
|
| 448 |
|
| 449 | static struct dentry *find_positive_dentry(struct dentry *parent,
|
| 450 | struct dentry *dentry,
|
| 451 | bool next)
|
| 452 | {
|
| 453 | struct dentry *found = NULL;
|
| 454 |
|
| 455 | spin_lock(&parent->d_lock);
|
| 456 | if (next)
|
| 457 | dentry = d_next_sibling(dentry);
|
| 458 | else if (!dentry)
|
| 459 | dentry = d_first_child(parent);
|
| 460 | hlist_for_each_entry_from(dentry, d_sib) {
|
| 461 | if (!simple_positive(dentry))
|
| 462 | continue;
|
| 463 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
|
| 464 | if (simple_positive(dentry))
|
| 465 | found = dget_dlock(dentry);
|
| 466 | spin_unlock(&dentry->d_lock);
|
| 467 | if (likely(found))
|
| 468 | break;
|
| 469 | }
|
| 470 | spin_unlock(&parent->d_lock);
|
| 471 | return found;
|
| 472 | }
|
| 473 |
|
| 474 | static noinline_for_stack struct dentry *
|
| 475 | offset_dir_lookup(struct dentry *parent, loff_t offset)
|
| 476 | {
|
| 477 | struct inode *inode = d_inode(parent);
|
| 478 | struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode);
|
| 479 | struct dentry *child, *found = NULL;
|
| 480 |
|
| 481 | MA_STATE(mas, &octx->mt, offset, offset);
|
| 482 |
|
| 483 | if (offset == DIR_OFFSET_FIRST)
|
| 484 | found = find_positive_dentry(parent, NULL, false);
|
| 485 | else {
|
| 486 | rcu_read_lock();
|
| 487 | child = mas_find_rev(&mas, DIR_OFFSET_MIN);
|
| 488 | found = find_positive_dentry(parent, child, false);
|
| 489 | rcu_read_unlock();
|
| 490 | }
|
| 491 | return found;
|
| 492 | }
|
| 493 |
|
| 494 | static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
|
| 495 | {
|
| 496 | struct inode *inode = d_inode(dentry);
|
| 497 |
|
| 498 | return dir_emit(ctx, dentry->d_name.name, dentry->d_name.len,
|
| 499 | inode->i_ino, fs_umode_to_dtype(inode->i_mode));
|
| 500 | }
|
| 501 |
|
| 502 | static void offset_iterate_dir(struct file *file, struct dir_context *ctx)
|
| 503 | {
|
| 504 | struct dentry *dir = file->f_path.dentry;
|
| 505 | struct dentry *dentry;
|
| 506 |
|
| 507 | dentry = offset_dir_lookup(dir, ctx->pos);
|
| 508 | if (!dentry)
|
| 509 | goto out_eod;
|
| 510 | while (true) {
|
| 511 | struct dentry *next;
|
| 512 |
|
| 513 | ctx->pos = dentry2offset(dentry);
|
| 514 | if (!offset_dir_emit(ctx, dentry))
|
| 515 | break;
|
| 516 |
|
| 517 | next = find_positive_dentry(dir, dentry, true);
|
| 518 | dput(dentry);
|
| 519 |
|
| 520 | if (!next)
|
| 521 | goto out_eod;
|
| 522 | dentry = next;
|
| 523 | }
|
| 524 | dput(dentry);
|
| 525 | return;
|
| 526 |
|
| 527 | out_eod:
|
| 528 | ctx->pos = DIR_OFFSET_EOD;
|
| 529 | }
|
| 530 |
|
| 531 | |
| 532 | * offset_readdir - Emit entries starting at offset @ctx->pos
|
| 533 | * @file: an open directory to iterate over
|
| 534 | * @ctx: directory iteration context
|
| 535 | *
|
| 536 | * Caller must hold @file's i_rwsem to prevent insertion or removal of
|
| 537 | * entries during this call.
|
| 538 | *
|
| 539 | * On entry, @ctx->pos contains an offset that represents the first entry
|
| 540 | * to be read from the directory.
|
| 541 | *
|
| 542 | * The operation continues until there are no more entries to read, or
|
| 543 | * until the ctx->actor indicates there is no more space in the caller's
|
| 544 | * output buffer.
|
| 545 | *
|
| 546 | * On return, @ctx->pos contains an offset that will read the next entry
|
| 547 | * in this directory when offset_readdir() is called again with @ctx.
|
| 548 | * Caller places this value in the d_off field of the last entry in the
|
| 549 | * user's buffer.
|
| 550 | *
|
| 551 | * Return values:
|
| 552 | * %0 - Complete
|
| 553 |
|
| 554 | static int offset_readdir(struct file *file, struct dir_context *ctx)
|
| 555 | {
|
| 556 | struct dentry *dir = file->f_path.dentry;
|
| 557 |
|
| 558 | lockdep_assert_held(&d_inode(dir)->i_rwsem);
|
| 559 |
|
| 560 | if (!dir_emit_dots(file, ctx))
|
| 561 | return 0;
|
| 562 | if (ctx->pos != DIR_OFFSET_EOD)
|
| 563 | offset_iterate_dir(file, ctx);
|
| 564 | return 0;
|
| 565 | }
|
| 566 |
|
| 567 | const struct file_operations simple_offset_dir_operations = {
|
| 568 | .llseek = offset_dir_llseek,
|
| 569 | .iterate_shared = offset_readdir,
|
| 570 | .read = generic_read_dir,
|
| 571 | .fsync = noop_fsync,
|
| 572 | .setlease = generic_setlease,
|
| 573 | };
|
| 574 |
|
| 575 | struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
|
| 576 | {
|
| 577 | struct dentry *child = NULL, *d;
|
| 578 |
|
| 579 | spin_lock(&parent->d_lock);
|
| 580 | d = prev ? d_next_sibling(prev) : d_first_child(parent);
|
| 581 | hlist_for_each_entry_from(d, d_sib) {
|
| 582 | if (simple_positive(d)) {
|
| 583 | spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
|
| 584 | if (simple_positive(d))
|
| 585 | child = dget_dlock(d);
|
| 586 | spin_unlock(&d->d_lock);
|
| 587 | if (likely(child))
|
| 588 | break;
|
| 589 | }
|
| 590 | }
|
| 591 | spin_unlock(&parent->d_lock);
|
| 592 | dput(prev);
|
| 593 | return child;
|
| 594 | }
|
| 595 | EXPORT_SYMBOL(find_next_child);
|
| 596 |
|
| 597 | static void __simple_recursive_removal(struct dentry *dentry,
|
| 598 | void (*callback)(struct dentry *),
|
| 599 | bool locked)
|
| 600 | {
|
| 601 | struct dentry *this = dget(dentry);
|
| 602 | while (true) {
|
| 603 | struct dentry *victim = NULL, *child;
|
| 604 | struct inode *inode = this->d_inode;
|
| 605 |
|
| 606 | inode_lock_nested(inode, I_MUTEX_CHILD);
|
| 607 | if (d_is_dir(this))
|
| 608 | inode->i_flags |= S_DEAD;
|
| 609 | while ((child = find_next_child(this, victim)) == NULL) {
|
| 610 | |
| 611 | |
| 612 | inode_set_ctime_current(inode);
|
| 613 | clear_nlink(inode);
|
| 614 | inode_unlock(inode);
|
| 615 | victim = this;
|
| 616 | this = this->d_parent;
|
| 617 | inode = this->d_inode;
|
| 618 | if (!locked || victim != dentry)
|
| 619 | inode_lock_nested(inode, I_MUTEX_CHILD);
|
| 620 | if (simple_positive(victim)) {
|
| 621 | d_invalidate(victim); |
| 622 | if (callback)
|
| 623 | callback(victim);
|
| 624 | fsnotify_delete(inode, d_inode(victim), victim);
|
| 625 | d_make_discardable(victim);
|
| 626 | }
|
| 627 | if (victim == dentry) {
|
| 628 | inode_set_mtime_to_ts(inode,
|
| 629 | inode_set_ctime_current(inode));
|
| 630 | if (d_is_dir(dentry))
|
| 631 | drop_nlink(inode);
|
| 632 | if (!locked)
|
| 633 | inode_unlock(inode);
|
| 634 | dput(dentry);
|
| 635 | return;
|
| 636 | }
|
| 637 | }
|
| 638 | inode_unlock(inode);
|
| 639 | this = child;
|
| 640 | }
|
| 641 | }
|
| 642 |
|
| 643 | void simple_recursive_removal(struct dentry *dentry,
|
| 644 | void (*callback)(struct dentry *))
|
| 645 | {
|
| 646 | return __simple_recursive_removal(dentry, callback, false);
|
| 647 | }
|
| 648 | EXPORT_SYMBOL(simple_recursive_removal);
|
| 649 |
|
| 650 | void simple_remove_by_name(struct dentry *parent, const char *name,
|
| 651 | void (*callback)(struct dentry *))
|
| 652 | {
|
| 653 | struct dentry *dentry;
|
| 654 |
|
| 655 | dentry = lookup_noperm_positive_unlocked(&QSTR(name), parent);
|
| 656 | if (!IS_ERR(dentry)) {
|
| 657 | simple_recursive_removal(dentry, callback);
|
| 658 | dput(dentry); |
| 659 | }
|
| 660 | }
|
| 661 | EXPORT_SYMBOL(simple_remove_by_name);
|
| 662 |
|
| 663 |
|
| 664 | void locked_recursive_removal(struct dentry *dentry,
|
| 665 | void (*callback)(struct dentry *))
|
| 666 | {
|
| 667 | return __simple_recursive_removal(dentry, callback, true);
|
| 668 | }
|
| 669 | EXPORT_SYMBOL(locked_recursive_removal);
|
| 670 |
|
| 671 | static const struct super_operations simple_super_operations = {
|
| 672 | .statfs = simple_statfs,
|
| 673 | };
|
| 674 |
|
| 675 | static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
|
| 676 | {
|
| 677 | struct pseudo_fs_context *ctx = fc->fs_private;
|
| 678 | struct inode *root;
|
| 679 |
|
| 680 | s->s_maxbytes = MAX_LFS_FILESIZE;
|
| 681 | s->s_blocksize = PAGE_SIZE;
|
| 682 | s->s_blocksize_bits = PAGE_SHIFT;
|
| 683 | s->s_magic = ctx->magic;
|
| 684 | s->s_op = ctx->ops ?: &simple_super_operations;
|
| 685 | s->s_export_op = ctx->eops;
|
| 686 | s->s_xattr = ctx->xattr;
|
| 687 | s->s_time_gran = 1;
|
| 688 | s->s_d_flags |= ctx->s_d_flags;
|
| 689 | root = new_inode(s);
|
| 690 | if (!root)
|
| 691 | return -ENOMEM;
|
| 692 |
|
| 693 | |
| 694 | * since this is the first inode, make it number 1. New inodes created
|
| 695 | * after this must take care not to collide with it (by passing
|
| 696 | * max_reserved of 1 to iunique).
|
| 697 |
|
| 698 | root->i_ino = 1;
|
| 699 | root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
|
| 700 | simple_inode_init_ts(root);
|
| 701 | s->s_root = d_make_root(root);
|
| 702 | if (!s->s_root)
|
| 703 | return -ENOMEM;
|
| 704 | set_default_d_op(s, ctx->dops);
|
| 705 | return 0;
|
| 706 | }
|
| 707 |
|
| 708 | static int pseudo_fs_get_tree(struct fs_context *fc)
|
| 709 | {
|
| 710 | return get_tree_nodev(fc, pseudo_fs_fill_super);
|
| 711 | }
|
| 712 |
|
| 713 | static void pseudo_fs_free(struct fs_context *fc)
|
| 714 | {
|
| 715 | kfree(fc->fs_private);
|
| 716 | }
|
| 717 |
|
| 718 | static const struct fs_context_operations pseudo_fs_context_ops = {
|
| 719 | .free = pseudo_fs_free,
|
| 720 | .get_tree = pseudo_fs_get_tree,
|
| 721 | };
|
| 722 |
|
| 723 | |
| 724 | * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
|
| 725 | * will never be mountable)
|
| 726 |
|
| 727 | struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
|
| 728 | unsigned long magic)
|
| 729 | {
|
| 730 | struct pseudo_fs_context *ctx;
|
| 731 |
|
| 732 | ctx = kzalloc_obj(struct pseudo_fs_context);
|
| 733 | if (likely(ctx)) {
|
| 734 | ctx->magic = magic;
|
| 735 | fc->fs_private = ctx;
|
| 736 | fc->ops = &pseudo_fs_context_ops;
|
| 737 | fc->sb_flags |= SB_NOUSER;
|
| 738 | fc->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
|
| 739 | fc->global = true;
|
| 740 | }
|
| 741 | return ctx;
|
| 742 | }
|
| 743 | EXPORT_SYMBOL(init_pseudo);
|
| 744 |
|
| 745 | int simple_open(struct inode *inode, struct file *file)
|
| 746 | {
|
| 747 | if (inode->i_private)
|
| 748 | file->private_data = inode->i_private;
|
| 749 | return 0;
|
| 750 | }
|
| 751 | EXPORT_SYMBOL(simple_open);
|
| 752 |
|
| 753 | int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
|
| 754 | {
|
| 755 | struct inode *inode = d_inode(old_dentry);
|
| 756 |
|
| 757 | inode_set_mtime_to_ts(dir,
|
| 758 | inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
|
| 759 | inc_nlink(inode);
|
| 760 | ihold(inode);
|
| 761 | d_make_persistent(dentry, inode);
|
| 762 | return 0;
|
| 763 | }
|
| 764 | EXPORT_SYMBOL(simple_link);
|
| 765 |
|
| 766 | int simple_empty(struct dentry *dentry)
|
| 767 | {
|
| 768 | struct dentry *child;
|
| 769 | int ret = 0;
|
| 770 |
|
| 771 | spin_lock(&dentry->d_lock);
|
| 772 | hlist_for_each_entry(child, &dentry->d_children, d_sib) {
|
| 773 | spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
|
| 774 | if (simple_positive(child)) {
|
| 775 | spin_unlock(&child->d_lock);
|
| 776 | goto out;
|
| 777 | }
|
| 778 | spin_unlock(&child->d_lock);
|
| 779 | }
|
| 780 | ret = 1;
|
| 781 | out:
|
| 782 | spin_unlock(&dentry->d_lock);
|
| 783 | return ret;
|
| 784 | }
|
| 785 | EXPORT_SYMBOL(simple_empty);
|
| 786 |
|
| 787 | void __simple_unlink(struct inode *dir, struct dentry *dentry)
|
| 788 | {
|
| 789 | struct inode *inode = d_inode(dentry);
|
| 790 |
|
| 791 | inode_set_mtime_to_ts(dir,
|
| 792 | inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
|
| 793 | drop_nlink(inode);
|
| 794 | }
|
| 795 | EXPORT_SYMBOL(__simple_unlink);
|
| 796 |
|
| 797 | void __simple_rmdir(struct inode *dir, struct dentry *dentry)
|
| 798 | {
|
| 799 | drop_nlink(d_inode(dentry));
|
| 800 | __simple_unlink(dir, dentry);
|
| 801 | drop_nlink(dir);
|
| 802 | }
|
| 803 | EXPORT_SYMBOL(__simple_rmdir);
|
| 804 |
|
| 805 | int simple_unlink(struct inode *dir, struct dentry *dentry)
|
| 806 | {
|
| 807 | __simple_unlink(dir, dentry);
|
| 808 | d_make_discardable(dentry);
|
| 809 | return 0;
|
| 810 | }
|
| 811 | EXPORT_SYMBOL(simple_unlink);
|
| 812 |
|
| 813 | int simple_rmdir(struct inode *dir, struct dentry *dentry)
|
| 814 | {
|
| 815 | if (!simple_empty(dentry))
|
| 816 | return -ENOTEMPTY;
|
| 817 |
|
| 818 | __simple_rmdir(dir, dentry);
|
| 819 | d_make_discardable(dentry);
|
| 820 | return 0;
|
| 821 | }
|
| 822 | EXPORT_SYMBOL(simple_rmdir);
|
| 823 |
|
| 824 | |
| 825 | * simple_rename_timestamp - update the various inode timestamps for rename
|
| 826 | * @old_dir: old parent directory
|
| 827 | * @old_dentry: dentry that is being renamed
|
| 828 | * @new_dir: new parent directory
|
| 829 | * @new_dentry: target for rename
|
| 830 | *
|
| 831 | * POSIX mandates that the old and new parent directories have their ctime and
|
| 832 | * mtime updated, and that inodes of @old_dentry and @new_dentry (if any), have
|
| 833 | * their ctime updated.
|
| 834 |
|
| 835 | void simple_rename_timestamp(struct inode *old_dir, struct dentry *old_dentry,
|
| 836 | struct inode *new_dir, struct dentry *new_dentry)
|
| 837 | {
|
| 838 | struct inode *newino = d_inode(new_dentry);
|
| 839 |
|
| 840 | inode_set_mtime_to_ts(old_dir, inode_set_ctime_current(old_dir));
|
| 841 | if (new_dir != old_dir)
|
| 842 | inode_set_mtime_to_ts(new_dir,
|
| 843 | inode_set_ctime_current(new_dir));
|
| 844 | inode_set_ctime_current(d_inode(old_dentry));
|
| 845 | if (newino)
|
| 846 | inode_set_ctime_current(newino);
|
| 847 | }
|
| 848 | EXPORT_SYMBOL_GPL(simple_rename_timestamp);
|
| 849 |
|
| 850 | int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
|
| 851 | struct inode *new_dir, struct dentry *new_dentry)
|
| 852 | {
|
| 853 | bool old_is_dir = d_is_dir(old_dentry);
|
| 854 | bool new_is_dir = d_is_dir(new_dentry);
|
| 855 |
|
| 856 | if (old_dir != new_dir && old_is_dir != new_is_dir) {
|
| 857 | if (old_is_dir) {
|
| 858 | drop_nlink(old_dir);
|
| 859 | inc_nlink(new_dir);
|
| 860 | } else {
|
| 861 | drop_nlink(new_dir);
|
| 862 | inc_nlink(old_dir);
|
| 863 | }
|
| 864 | }
|
| 865 | simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
|
| 866 | return 0;
|
| 867 | }
|
| 868 | EXPORT_SYMBOL_GPL(simple_rename_exchange);
|
| 869 |
|
| 870 | int simple_rename(struct mnt_idmap *idmap, struct inode *old_dir,
|
| 871 | struct dentry *old_dentry, struct inode *new_dir,
|
| 872 | struct dentry *new_dentry, unsigned int flags)
|
| 873 | {
|
| 874 | int they_are_dirs = d_is_dir(old_dentry);
|
| 875 |
|
| 876 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
|
| 877 | return -EINVAL;
|
| 878 |
|
| 879 | if (flags & RENAME_EXCHANGE)
|
| 880 | return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
|
| 881 |
|
| 882 | if (!simple_empty(new_dentry))
|
| 883 | return -ENOTEMPTY;
|
| 884 |
|
| 885 | if (d_really_is_positive(new_dentry)) {
|
| 886 | simple_unlink(new_dir, new_dentry);
|
| 887 | if (they_are_dirs) {
|
| 888 | drop_nlink(d_inode(new_dentry));
|
| 889 | drop_nlink(old_dir);
|
| 890 | }
|
| 891 | } else if (they_are_dirs) {
|
| 892 | drop_nlink(old_dir);
|
| 893 | inc_nlink(new_dir);
|
| 894 | }
|
| 895 |
|
| 896 | simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
|
| 897 | return 0;
|
| 898 | }
|
| 899 | EXPORT_SYMBOL(simple_rename);
|
| 900 |
|
| 901 | |
| 902 | * simple_setattr - setattr for simple filesystem
|
| 903 | * @idmap: idmap of the target mount
|
| 904 | * @dentry: dentry
|
| 905 | * @iattr: iattr structure
|
| 906 | *
|
| 907 | * Returns 0 on success, -error on failure.
|
| 908 | *
|
| 909 | * simple_setattr is a simple ->setattr implementation without a proper
|
| 910 | * implementation of size changes.
|
| 911 | *
|
| 912 | * It can either be used for in-memory filesystems or special files
|
| 913 | * on simple regular filesystems. Anything that needs to change on-disk
|
| 914 | * or wire state on size changes needs its own setattr method.
|
| 915 |
|
| 916 | int simple_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
|
| 917 | struct iattr *iattr)
|
| 918 | {
|
| 919 | struct inode *inode = d_inode(dentry);
|
| 920 | int error;
|
| 921 |
|
| 922 | error = setattr_prepare(idmap, dentry, iattr);
|
| 923 | if (error)
|
| 924 | return error;
|
| 925 |
|
| 926 | if (iattr->ia_valid & ATTR_SIZE)
|
| 927 | truncate_setsize(inode, iattr->ia_size);
|
| 928 | setattr_copy(idmap, inode, iattr);
|
| 929 | mark_inode_dirty(inode);
|
| 930 | return 0;
|
| 931 | }
|
| 932 | EXPORT_SYMBOL(simple_setattr);
|
| 933 |
|
| 934 | static int simple_read_folio(struct file *file, struct folio *folio)
|
| 935 | {
|
| 936 | folio_zero_range(folio, 0, folio_size(folio));
|
| 937 | flush_dcache_folio(folio);
|
| 938 | folio_mark_uptodate(folio);
|
| 939 | folio_unlock(folio);
|
| 940 | return 0;
|
| 941 | }
|
| 942 |
|
| 943 | int simple_write_begin(const struct kiocb *iocb, struct address_space *mapping,
|
| 944 | loff_t pos, unsigned len,
|
| 945 | struct folio **foliop, void **fsdata)
|
| 946 | {
|
| 947 | struct folio *folio;
|
| 948 |
|
| 949 | folio = __filemap_get_folio(mapping, pos / PAGE_SIZE, FGP_WRITEBEGIN,
|
| 950 | mapping_gfp_mask(mapping));
|
| 951 | if (IS_ERR(folio))
|
| 952 | return PTR_ERR(folio);
|
| 953 |
|
| 954 | *foliop = folio;
|
| 955 |
|
| 956 | if (!folio_test_uptodate(folio) && (len != folio_size(folio))) {
|
| 957 | size_t from = offset_in_folio(folio, pos);
|
| 958 |
|
| 959 | folio_zero_segments(folio, 0, from,
|
| 960 | from + len, folio_size(folio));
|
| 961 | }
|
| 962 | return 0;
|
| 963 | }
|
| 964 | EXPORT_SYMBOL(simple_write_begin);
|
| 965 |
|
| 966 | |
| 967 | * simple_write_end - .write_end helper for non-block-device FSes
|
| 968 | * @iocb: kernel I/O control block
|
| 969 | * @mapping: "
|
| 970 | * @pos: "
|
| 971 | * @len: "
|
| 972 | * @copied: "
|
| 973 | * @folio: "
|
| 974 | * @fsdata: "
|
| 975 | *
|
| 976 | * simple_write_end does the minimum needed for updating a folio after
|
| 977 | * writing is done. It has the same API signature as the .write_end of
|
| 978 | * address_space_operations vector. So it can just be set onto .write_end for
|
| 979 | * FSes that don't need any other processing. i_rwsem is assumed to be held
|
| 980 | * exclusively.
|
| 981 | * Block based filesystems should use generic_write_end().
|
| 982 | * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
|
| 983 | * is not called, so a filesystem that actually does store data in .write_inode
|
| 984 | * should extend on what's done here with a call to mark_inode_dirty() in the
|
| 985 | * case that i_size has changed.
|
| 986 | *
|
| 987 | * Use *ONLY* with simple_read_folio()
|
| 988 |
|
| 989 | static int simple_write_end(const struct kiocb *iocb,
|
| 990 | struct address_space *mapping,
|
| 991 | loff_t pos, unsigned len, unsigned copied,
|
| 992 | struct folio *folio, void *fsdata)
|
| 993 | {
|
| 994 | struct inode *inode = folio->mapping->host;
|
| 995 | loff_t last_pos = pos + copied;
|
| 996 |
|
| 997 |
|
| 998 | if (!folio_test_uptodate(folio)) {
|
| 999 | if (copied < len) {
|
| 1000 | size_t from = offset_in_folio(folio, pos);
|
| 1001 |
|
| 1002 | folio_zero_range(folio, from + copied, len - copied);
|
| 1003 | }
|
| 1004 | folio_mark_uptodate(folio);
|
| 1005 | }
|
| 1006 | |
| 1007 | * No need to use i_size_read() here, the i_size
|
| 1008 | * cannot change under us because we hold the i_rwsem.
|
| 1009 |
|
| 1010 | if (last_pos > inode->i_size)
|
| 1011 | i_size_write(inode, last_pos);
|
| 1012 |
|
| 1013 | folio_mark_dirty(folio);
|
| 1014 | folio_unlock(folio);
|
| 1015 | folio_put(folio);
|
| 1016 |
|
| 1017 | return copied;
|
| 1018 | }
|
| 1019 |
|
| 1020 | |
| 1021 | * Provides ramfs-style behavior: data in the pagecache, but no writeback.
|
| 1022 |
|
| 1023 | const struct address_space_operations ram_aops = {
|
| 1024 | .read_folio = simple_read_folio,
|
| 1025 | .write_begin = simple_write_begin,
|
| 1026 | .write_end = simple_write_end,
|
| 1027 | .dirty_folio = noop_dirty_folio,
|
| 1028 | };
|
| 1029 | EXPORT_SYMBOL(ram_aops);
|
| 1030 |
|
| 1031 | |
| 1032 | * the inodes created here are not hashed. If you use iunique to generate
|
| 1033 | * unique inode values later for this filesystem, then you must take care
|
| 1034 | * to pass it an appropriate max_reserved value to avoid collisions.
|
| 1035 |
|
| 1036 | int simple_fill_super(struct super_block *s, unsigned long magic,
|
| 1037 | const struct tree_descr *files)
|
| 1038 | {
|
| 1039 | struct inode *inode;
|
| 1040 | struct dentry *dentry;
|
| 1041 | int i;
|
| 1042 |
|
| 1043 | s->s_blocksize = PAGE_SIZE;
|
| 1044 | s->s_blocksize_bits = PAGE_SHIFT;
|
| 1045 | s->s_magic = magic;
|
| 1046 | s->s_op = &simple_super_operations;
|
| 1047 | s->s_time_gran = 1;
|
| 1048 |
|
| 1049 | inode = new_inode(s);
|
| 1050 | if (!inode)
|
| 1051 | return -ENOMEM;
|
| 1052 | |
| 1053 | * because the root inode is 1, the files array must not contain an
|
| 1054 | * entry at index 1
|
| 1055 |
|
| 1056 | inode->i_ino = 1;
|
| 1057 | inode->i_mode = S_IFDIR | 0755;
|
| 1058 | simple_inode_init_ts(inode);
|
| 1059 | inode->i_op = &simple_dir_inode_operations;
|
| 1060 | inode->i_fop = &simple_dir_operations;
|
| 1061 | set_nlink(inode, 2);
|
| 1062 | s->s_root = d_make_root(inode);
|
| 1063 | if (!s->s_root)
|
| 1064 | return -ENOMEM;
|
| 1065 | for (i = 0; !files->name || files->name[0]; i++, files++) {
|
| 1066 | if (!files->name)
|
| 1067 | continue;
|
| 1068 |
|
| 1069 |
|
| 1070 | if (unlikely(i == 1))
|
| 1071 | printk(KERN_WARNING "%s: %s passed in a files array"
|
| 1072 | "with an index of 1!\n", __func__,
|
| 1073 | s->s_type->name);
|
| 1074 |
|
| 1075 | dentry = d_alloc_name(s->s_root, files->name);
|
| 1076 | if (!dentry)
|
| 1077 | return -ENOMEM;
|
| 1078 | inode = new_inode(s);
|
| 1079 | if (!inode) {
|
| 1080 | dput(dentry);
|
| 1081 | return -ENOMEM;
|
| 1082 | }
|
| 1083 | inode->i_mode = S_IFREG | files->mode;
|
| 1084 | simple_inode_init_ts(inode);
|
| 1085 | inode->i_fop = files->ops;
|
| 1086 | inode->i_ino = i;
|
| 1087 | d_make_persistent(dentry, inode);
|
| 1088 | dput(dentry);
|
| 1089 | }
|
| 1090 | return 0;
|
| 1091 | }
|
| 1092 | EXPORT_SYMBOL(simple_fill_super);
|
| 1093 |
|
| 1094 | static DEFINE_SPINLOCK(pin_fs_lock);
|
| 1095 |
|
| 1096 | int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
|
| 1097 | {
|
| 1098 | struct vfsmount *mnt = NULL;
|
| 1099 | spin_lock(&pin_fs_lock);
|
| 1100 | if (unlikely(!*mount)) {
|
| 1101 | spin_unlock(&pin_fs_lock);
|
| 1102 | mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
|
| 1103 | if (IS_ERR(mnt))
|
| 1104 | return PTR_ERR(mnt);
|
| 1105 | spin_lock(&pin_fs_lock);
|
| 1106 | if (!*mount)
|
| 1107 | *mount = mnt;
|
| 1108 | }
|
| 1109 | mntget(*mount);
|
| 1110 | ++*count;
|
| 1111 | spin_unlock(&pin_fs_lock);
|
| 1112 | mntput(mnt);
|
| 1113 | return 0;
|
| 1114 | }
|
| 1115 | EXPORT_SYMBOL(simple_pin_fs);
|
| 1116 |
|
| 1117 | void simple_release_fs(struct vfsmount **mount, int *count)
|
| 1118 | {
|
| 1119 | struct vfsmount *mnt;
|
| 1120 | spin_lock(&pin_fs_lock);
|
| 1121 | mnt = *mount;
|
| 1122 | if (!--*count)
|
| 1123 | *mount = NULL;
|
| 1124 | spin_unlock(&pin_fs_lock);
|
| 1125 | mntput(mnt);
|
| 1126 | }
|
| 1127 | EXPORT_SYMBOL(simple_release_fs);
|
| 1128 |
|
| 1129 | |
| 1130 | * simple_read_from_buffer - copy data from the buffer to user space
|
| 1131 | * @to: the user space buffer to read to
|
| 1132 | * @count: the maximum number of bytes to read
|
| 1133 | * @ppos: the current position in the buffer
|
| 1134 | * @from: the buffer to read from
|
| 1135 | * @available: the size of the buffer
|
| 1136 | *
|
| 1137 | * The simple_read_from_buffer() function reads up to @count bytes from the
|
| 1138 | * buffer @from at offset @ppos into the user space address starting at @to.
|
| 1139 | *
|
| 1140 | * On success, the number of bytes read is returned and the offset @ppos is
|
| 1141 | * advanced by this number, or negative value is returned on error.
|
| 1142 | *
|
| 1143 | ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
|
| 1144 | const void *from, size_t available)
|
| 1145 | {
|
| 1146 | loff_t pos = *ppos;
|
| 1147 | size_t ret;
|
| 1148 |
|
| 1149 | if (pos < 0)
|
| 1150 | return -EINVAL;
|
| 1151 | if (pos >= available || !count)
|
| 1152 | return 0;
|
| 1153 | if (count > available - pos)
|
| 1154 | count = available - pos;
|
| 1155 | ret = copy_to_user(to, from + pos, count);
|
| 1156 | if (ret == count)
|
| 1157 | return -EFAULT;
|
| 1158 | count -= ret;
|
| 1159 | *ppos = pos + count;
|
| 1160 | return count;
|
| 1161 | }
|
| 1162 | EXPORT_SYMBOL(simple_read_from_buffer);
|
| 1163 |
|
| 1164 | |
| 1165 | * simple_write_to_buffer - copy data from user space to the buffer
|
| 1166 | * @to: the buffer to write to
|
| 1167 | * @available: the size of the buffer
|
| 1168 | * @ppos: the current position in the buffer
|
| 1169 | * @from: the user space buffer to read from
|
| 1170 | * @count: the maximum number of bytes to read
|
| 1171 | *
|
| 1172 | * The simple_write_to_buffer() function reads up to @count bytes from the user
|
| 1173 | * space address starting at @from into the buffer @to at offset @ppos.
|
| 1174 | *
|
| 1175 | * On success, the number of bytes written is returned and the offset @ppos is
|
| 1176 | * advanced by this number, or negative value is returned on error.
|
| 1177 | *
|
| 1178 | ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
|
| 1179 | const void __user *from, size_t count)
|
| 1180 | {
|
| 1181 | loff_t pos = *ppos;
|
| 1182 | size_t res;
|
| 1183 |
|
| 1184 | if (pos < 0)
|
| 1185 | return -EINVAL;
|
| 1186 | if (pos >= available || !count)
|
| 1187 | return 0;
|
| 1188 | if (count > available - pos)
|
| 1189 | count = available - pos;
|
| 1190 | res = copy_from_user(to + pos, from, count);
|
| 1191 | if (res == count)
|
| 1192 | return -EFAULT;
|
| 1193 | count -= res;
|
| 1194 | *ppos = pos + count;
|
| 1195 | return count;
|
| 1196 | }
|
| 1197 | EXPORT_SYMBOL(simple_write_to_buffer);
|
| 1198 |
|
| 1199 | |
| 1200 | * memory_read_from_buffer - copy data from the buffer
|
| 1201 | * @to: the kernel space buffer to read to
|
| 1202 | * @count: the maximum number of bytes to read
|
| 1203 | * @ppos: the current position in the buffer
|
| 1204 | * @from: the buffer to read from
|
| 1205 | * @available: the size of the buffer
|
| 1206 | *
|
| 1207 | * The memory_read_from_buffer() function reads up to @count bytes from the
|
| 1208 | * buffer @from at offset @ppos into the kernel space address starting at @to.
|
| 1209 | *
|
| 1210 | * On success, the number of bytes read is returned and the offset @ppos is
|
| 1211 | * advanced by this number, or negative value is returned on error.
|
| 1212 | *
|
| 1213 | ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
|
| 1214 | const void *from, size_t available)
|
| 1215 | {
|
| 1216 | loff_t pos = *ppos;
|
| 1217 |
|
| 1218 | if (pos < 0)
|
| 1219 | return -EINVAL;
|
| 1220 | if (pos >= available)
|
| 1221 | return 0;
|
| 1222 | if (count > available - pos)
|
| 1223 | count = available - pos;
|
| 1224 | memcpy(to, from + pos, count);
|
| 1225 | *ppos = pos + count;
|
| 1226 |
|
| 1227 | return count;
|
| 1228 | }
|
| 1229 | EXPORT_SYMBOL(memory_read_from_buffer);
|
| 1230 |
|
| 1231 | |
| 1232 | * Transaction based IO.
|
| 1233 | * The file expects a single write which triggers the transaction, and then
|
| 1234 | * possibly a read which collects the result - which is stored in a
|
| 1235 | * file-local buffer.
|
| 1236 |
|
| 1237 |
|
| 1238 | void simple_transaction_set(struct file *file, size_t n)
|
| 1239 | {
|
| 1240 | struct simple_transaction_argresp *ar = file->private_data;
|
| 1241 |
|
| 1242 | BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
|
| 1243 |
|
| 1244 | |
| 1245 | * The barrier ensures that ar->size will really remain zero until
|
| 1246 | * ar->data is ready for reading.
|
| 1247 |
|
| 1248 | smp_mb();
|
| 1249 | ar->size = n;
|
| 1250 | }
|
| 1251 | EXPORT_SYMBOL(simple_transaction_set);
|
| 1252 |
|
| 1253 | char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
|
| 1254 | {
|
| 1255 | struct simple_transaction_argresp *ar;
|
| 1256 | static DEFINE_SPINLOCK(simple_transaction_lock);
|
| 1257 |
|
| 1258 | if (size > SIMPLE_TRANSACTION_LIMIT - 1)
|
| 1259 | return ERR_PTR(-EFBIG);
|
| 1260 |
|
| 1261 | ar = kzalloc(PAGE_SIZE, GFP_KERNEL);
|
| 1262 | if (!ar)
|
| 1263 | return ERR_PTR(-ENOMEM);
|
| 1264 |
|
| 1265 | spin_lock(&simple_transaction_lock);
|
| 1266 |
|
| 1267 |
|
| 1268 | if (file->private_data) {
|
| 1269 | spin_unlock(&simple_transaction_lock);
|
| 1270 | kfree(ar);
|
| 1271 | return ERR_PTR(-EBUSY);
|
| 1272 | }
|
| 1273 |
|
| 1274 | file->private_data = ar;
|
| 1275 |
|
| 1276 | spin_unlock(&simple_transaction_lock);
|
| 1277 |
|
| 1278 | if (copy_from_user(ar->data, buf, size))
|
| 1279 | return ERR_PTR(-EFAULT);
|
| 1280 |
|
| 1281 | return ar->data;
|
| 1282 | }
|
| 1283 | EXPORT_SYMBOL(simple_transaction_get);
|
| 1284 |
|
| 1285 | ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
|
| 1286 | {
|
| 1287 | struct simple_transaction_argresp *ar = file->private_data;
|
| 1288 |
|
| 1289 | if (!ar)
|
| 1290 | return 0;
|
| 1291 | return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
|
| 1292 | }
|
| 1293 | EXPORT_SYMBOL(simple_transaction_read);
|
| 1294 |
|
| 1295 | int simple_transaction_release(struct inode *inode, struct file *file)
|
| 1296 | {
|
| 1297 | kfree(file->private_data);
|
| 1298 | return 0;
|
| 1299 | }
|
| 1300 | EXPORT_SYMBOL(simple_transaction_release);
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 | struct simple_attr {
|
| 1305 | int (*get)(void *, u64 *);
|
| 1306 | int (*set)(void *, u64);
|
| 1307 | char get_buf[24];
|
| 1308 | char set_buf[24];
|
| 1309 | void *data;
|
| 1310 | const char *fmt;
|
| 1311 | struct mutex mutex;
|
| 1312 | };
|
| 1313 |
|
| 1314 | |
| 1315 | * to set the attribute specific access operations.
|
| 1316 | int simple_attr_open(struct inode *inode, struct file *file,
|
| 1317 | int (*get)(void *, u64 *), int (*set)(void *, u64),
|
| 1318 | const char *fmt)
|
| 1319 | {
|
| 1320 | struct simple_attr *attr;
|
| 1321 |
|
| 1322 | attr = kzalloc_obj(*attr);
|
| 1323 | if (!attr)
|
| 1324 | return -ENOMEM;
|
| 1325 |
|
| 1326 | attr->get = get;
|
| 1327 | attr->set = set;
|
| 1328 | attr->data = inode->i_private;
|
| 1329 | attr->fmt = fmt;
|
| 1330 | mutex_init(&attr->mutex);
|
| 1331 |
|
| 1332 | file->private_data = attr;
|
| 1333 |
|
| 1334 | return nonseekable_open(inode, file);
|
| 1335 | }
|
| 1336 | EXPORT_SYMBOL_GPL(simple_attr_open);
|
| 1337 |
|
| 1338 | int simple_attr_release(struct inode *inode, struct file *file)
|
| 1339 | {
|
| 1340 | kfree(file->private_data);
|
| 1341 | return 0;
|
| 1342 | }
|
| 1343 | EXPORT_SYMBOL_GPL(simple_attr_release);
|
| 1344 |
|
| 1345 |
|
| 1346 | ssize_t simple_attr_read(struct file *file, char __user *buf,
|
| 1347 | size_t len, loff_t *ppos)
|
| 1348 | {
|
| 1349 | struct simple_attr *attr;
|
| 1350 | size_t size;
|
| 1351 | ssize_t ret;
|
| 1352 |
|
| 1353 | attr = file->private_data;
|
| 1354 |
|
| 1355 | if (!attr->get)
|
| 1356 | return -EACCES;
|
| 1357 |
|
| 1358 | ret = mutex_lock_interruptible(&attr->mutex);
|
| 1359 | if (ret)
|
| 1360 | return ret;
|
| 1361 |
|
| 1362 | if (*ppos && attr->get_buf[0]) {
|
| 1363 |
|
| 1364 | size = strlen(attr->get_buf);
|
| 1365 | } else {
|
| 1366 |
|
| 1367 | u64 val;
|
| 1368 | ret = attr->get(attr->data, &val);
|
| 1369 | if (ret)
|
| 1370 | goto out;
|
| 1371 |
|
| 1372 | size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
|
| 1373 | attr->fmt, (unsigned long long)val);
|
| 1374 | }
|
| 1375 |
|
| 1376 | ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
|
| 1377 | out:
|
| 1378 | mutex_unlock(&attr->mutex);
|
| 1379 | return ret;
|
| 1380 | }
|
| 1381 | EXPORT_SYMBOL_GPL(simple_attr_read);
|
| 1382 |
|
| 1383 |
|
| 1384 | static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf,
|
| 1385 | size_t len, loff_t *ppos, bool is_signed)
|
| 1386 | {
|
| 1387 | struct simple_attr *attr;
|
| 1388 | unsigned long long val;
|
| 1389 | size_t size;
|
| 1390 | ssize_t ret;
|
| 1391 |
|
| 1392 | attr = file->private_data;
|
| 1393 | if (!attr->set)
|
| 1394 | return -EACCES;
|
| 1395 |
|
| 1396 | ret = mutex_lock_interruptible(&attr->mutex);
|
| 1397 | if (ret)
|
| 1398 | return ret;
|
| 1399 |
|
| 1400 | ret = -EFAULT;
|
| 1401 | size = min(sizeof(attr->set_buf) - 1, len);
|
| 1402 | if (copy_from_user(attr->set_buf, buf, size))
|
| 1403 | goto out;
|
| 1404 |
|
| 1405 | attr->set_buf[size] = '\0';
|
| 1406 | if (is_signed)
|
| 1407 | ret = kstrtoll(attr->set_buf, 0, &val);
|
| 1408 | else
|
| 1409 | ret = kstrtoull(attr->set_buf, 0, &val);
|
| 1410 | if (ret)
|
| 1411 | goto out;
|
| 1412 | ret = attr->set(attr->data, val);
|
| 1413 | if (ret == 0)
|
| 1414 | ret = len;
|
| 1415 | out:
|
| 1416 | mutex_unlock(&attr->mutex);
|
| 1417 | return ret;
|
| 1418 | }
|
| 1419 |
|
| 1420 | ssize_t simple_attr_write(struct file *file, const char __user *buf,
|
| 1421 | size_t len, loff_t *ppos)
|
| 1422 | {
|
| 1423 | return simple_attr_write_xsigned(file, buf, len, ppos, false);
|
| 1424 | }
|
| 1425 | EXPORT_SYMBOL_GPL(simple_attr_write);
|
| 1426 |
|
| 1427 | ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
|
| 1428 | size_t len, loff_t *ppos)
|
| 1429 | {
|
| 1430 | return simple_attr_write_xsigned(file, buf, len, ppos, true);
|
| 1431 | }
|
| 1432 | EXPORT_SYMBOL_GPL(simple_attr_write_signed);
|
| 1433 |
|
| 1434 | |
| 1435 | * generic_encode_ino32_fh - generic export_operations->encode_fh function
|
| 1436 | * @inode: the object to encode
|
| 1437 | * @fh: where to store the file handle fragment
|
| 1438 | * @max_len: maximum length to store there (in 4 byte units)
|
| 1439 | * @parent: parent directory inode, if wanted
|
| 1440 | *
|
| 1441 | * This generic encode_fh function assumes that the 32 inode number
|
| 1442 | * is suitable for locating an inode, and that the generation number
|
| 1443 | * can be used to check that it is still valid. It places them in the
|
| 1444 | * filehandle fragment where export_decode_fh expects to find them.
|
| 1445 |
|
| 1446 | int generic_encode_ino32_fh(struct inode *inode, __u32 *fh, int *max_len,
|
| 1447 | struct inode *parent)
|
| 1448 | {
|
| 1449 | struct fid *fid = (void *)fh;
|
| 1450 | int len = *max_len;
|
| 1451 | int type = FILEID_INO32_GEN;
|
| 1452 |
|
| 1453 | if (parent && (len < 4)) {
|
| 1454 | *max_len = 4;
|
| 1455 | return FILEID_INVALID;
|
| 1456 | } else if (len < 2) {
|
| 1457 | *max_len = 2;
|
| 1458 | return FILEID_INVALID;
|
| 1459 | }
|
| 1460 |
|
| 1461 | len = 2;
|
| 1462 | fid->i32.ino = inode->i_ino;
|
| 1463 | fid->i32.gen = inode->i_generation;
|
| 1464 | if (parent) {
|
| 1465 | fid->i32.parent_ino = parent->i_ino;
|
| 1466 | fid->i32.parent_gen = parent->i_generation;
|
| 1467 | len = 4;
|
| 1468 | type = FILEID_INO32_GEN_PARENT;
|
| 1469 | }
|
| 1470 | *max_len = len;
|
| 1471 | return type;
|
| 1472 | }
|
| 1473 | EXPORT_SYMBOL_GPL(generic_encode_ino32_fh);
|
| 1474 |
|
| 1475 | |
| 1476 | * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
|
| 1477 | * @sb: filesystem to do the file handle conversion on
|
| 1478 | * @fid: file handle to convert
|
| 1479 | * @fh_len: length of the file handle in bytes
|
| 1480 | * @fh_type: type of file handle
|
| 1481 | * @get_inode: filesystem callback to retrieve inode
|
| 1482 | *
|
| 1483 | * This function decodes @fid as long as it has one of the well-known
|
| 1484 | * Linux filehandle types and calls @get_inode on it to retrieve the
|
| 1485 | * inode for the object specified in the file handle.
|
| 1486 |
|
| 1487 | struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
|
| 1488 | int fh_len, int fh_type, struct inode *(*get_inode)
|
| 1489 | (struct super_block *sb, u64 ino, u32 gen))
|
| 1490 | {
|
| 1491 | struct inode *inode = NULL;
|
| 1492 |
|
| 1493 | if (fh_len < 2)
|
| 1494 | return NULL;
|
| 1495 |
|
| 1496 | switch (fh_type) {
|
| 1497 | case FILEID_INO32_GEN:
|
| 1498 | case FILEID_INO32_GEN_PARENT:
|
| 1499 | inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
|
| 1500 | break;
|
| 1501 | }
|
| 1502 |
|
| 1503 | return d_obtain_alias(inode);
|
| 1504 | }
|
| 1505 | EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
|
| 1506 |
|
| 1507 | |
| 1508 | * generic_fh_to_parent - generic helper for the fh_to_parent export operation
|
| 1509 | * @sb: filesystem to do the file handle conversion on
|
| 1510 | * @fid: file handle to convert
|
| 1511 | * @fh_len: length of the file handle in bytes
|
| 1512 | * @fh_type: type of file handle
|
| 1513 | * @get_inode: filesystem callback to retrieve inode
|
| 1514 | *
|
| 1515 | * This function decodes @fid as long as it has one of the well-known
|
| 1516 | * Linux filehandle types and calls @get_inode on it to retrieve the
|
| 1517 | * inode for the _parent_ object specified in the file handle if it
|
| 1518 | * is specified in the file handle, or NULL otherwise.
|
| 1519 |
|
| 1520 | struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
|
| 1521 | int fh_len, int fh_type, struct inode *(*get_inode)
|
| 1522 | (struct super_block *sb, u64 ino, u32 gen))
|
| 1523 | {
|
| 1524 | struct inode *inode = NULL;
|
| 1525 |
|
| 1526 | if (fh_len <= 2)
|
| 1527 | return NULL;
|
| 1528 |
|
| 1529 | switch (fh_type) {
|
| 1530 | case FILEID_INO32_GEN_PARENT:
|
| 1531 | inode = get_inode(sb, fid->i32.parent_ino,
|
| 1532 | (fh_len > 3 ? fid->i32.parent_gen : 0));
|
| 1533 | break;
|
| 1534 | }
|
| 1535 |
|
| 1536 | return d_obtain_alias(inode);
|
| 1537 | }
|
| 1538 | EXPORT_SYMBOL_GPL(generic_fh_to_parent);
|
| 1539 |
|
| 1540 | |
| 1541 | * simple_fsync_noflush - generic fsync implementation for simple filesystems
|
| 1542 | *
|
| 1543 | * @file: file to synchronize
|
| 1544 | * @start: start offset in bytes
|
| 1545 | * @end: end offset in bytes (inclusive)
|
| 1546 | * @datasync: only synchronize essential metadata if true
|
| 1547 | *
|
| 1548 | * This function is an fsync handler for simple filesystems. It writes out
|
| 1549 | * dirty data, inode (if dirty), but does not issue a cache flush.
|
| 1550 |
|
| 1551 | int simple_fsync_noflush(struct file *file, loff_t start, loff_t end,
|
| 1552 | int datasync)
|
| 1553 | {
|
| 1554 | struct inode *inode = file->f_mapping->host;
|
| 1555 | int err;
|
| 1556 | int ret = 0;
|
| 1557 |
|
| 1558 | err = file_write_and_wait_range(file, start, end);
|
| 1559 | if (err)
|
| 1560 | return err;
|
| 1561 |
|
| 1562 | if (!(inode_state_read_once(inode) &
|
| 1563 | (I_DIRTY_ALL | I_SYNC | I_METADATA_WRITEBACK)))
|
| 1564 | goto out;
|
| 1565 | if (datasync &&
|
| 1566 | !(inode_state_read_once(inode) &
|
| 1567 | (I_DIRTY_DATASYNC | I_SYNC | I_METADATA_WRITEBACK)))
|
| 1568 | goto out;
|
| 1569 |
|
| 1570 | ret = sync_inode_metadata(inode, 1);
|
| 1571 | out:
|
| 1572 |
|
| 1573 | err = file_check_and_advance_wb_err(file);
|
| 1574 | if (ret == 0)
|
| 1575 | ret = err;
|
| 1576 | return ret;
|
| 1577 | }
|
| 1578 | EXPORT_SYMBOL(simple_fsync_noflush);
|
| 1579 |
|
| 1580 | |
| 1581 | * simple_fsync - fsync implementation for simple filesystems with flush
|
| 1582 | * @file: file to synchronize
|
| 1583 | * @start: start offset in bytes
|
| 1584 | * @end: end offset in bytes (inclusive)
|
| 1585 | * @datasync: only synchronize essential metadata if true
|
| 1586 | *
|
| 1587 | * This function is an fsync handler for simple filesystems. It writes out
|
| 1588 | * dirty data, inode (if dirty), and issues a cache flush.
|
| 1589 |
|
| 1590 | int simple_fsync(struct file *file, loff_t start, loff_t end, int datasync)
|
| 1591 | {
|
| 1592 | struct inode *inode = file->f_mapping->host;
|
| 1593 | int err;
|
| 1594 |
|
| 1595 | err = simple_fsync_noflush(file, start, end, datasync);
|
| 1596 | if (err)
|
| 1597 | return err;
|
| 1598 | return blkdev_issue_flush(inode->i_sb->s_bdev);
|
| 1599 | }
|
| 1600 | EXPORT_SYMBOL(simple_fsync);
|
| 1601 |
|
| 1602 | |
| 1603 | * generic_check_addressable - Check addressability of file system
|
| 1604 | * @blocksize_bits: log of file system block size
|
| 1605 | * @num_blocks: number of blocks in file system
|
| 1606 | *
|
| 1607 | * Determine whether a file system with @num_blocks blocks (and a
|
| 1608 | * block size of 2**@blocksize_bits) is addressable by the sector_t
|
| 1609 | * and page cache of the system. Return 0 if so and -EFBIG otherwise.
|
| 1610 |
|
| 1611 | int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
|
| 1612 | {
|
| 1613 | u64 last_fs_block = num_blocks - 1;
|
| 1614 | u64 last_fs_page, max_bytes;
|
| 1615 |
|
| 1616 | if (check_shl_overflow(num_blocks, blocksize_bits, &max_bytes))
|
| 1617 | return -EFBIG;
|
| 1618 |
|
| 1619 | last_fs_page = (max_bytes >> PAGE_SHIFT) - 1;
|
| 1620 |
|
| 1621 | if (unlikely(num_blocks == 0))
|
| 1622 | return 0;
|
| 1623 |
|
| 1624 | if (blocksize_bits < 9)
|
| 1625 | return -EINVAL;
|
| 1626 |
|
| 1627 | if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
|
| 1628 | (last_fs_page > (pgoff_t)(~0ULL))) {
|
| 1629 | return -EFBIG;
|
| 1630 | }
|
| 1631 | return 0;
|
| 1632 | }
|
| 1633 | EXPORT_SYMBOL(generic_check_addressable);
|
| 1634 |
|
| 1635 | |
| 1636 | * No-op implementation of ->fsync for in-memory filesystems.
|
| 1637 |
|
| 1638 | int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
|
| 1639 | {
|
| 1640 | return 0;
|
| 1641 | }
|
| 1642 | EXPORT_SYMBOL(noop_fsync);
|
| 1643 |
|
| 1644 | ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
|
| 1645 | {
|
| 1646 | |
| 1647 | * iomap based filesystems support direct I/O without need for
|
| 1648 | * this callback. However, it still needs to be set in
|
| 1649 | * inode->a_ops so that open/fcntl know that direct I/O is
|
| 1650 | * generally supported.
|
| 1651 |
|
| 1652 | return -EINVAL;
|
| 1653 | }
|
| 1654 | EXPORT_SYMBOL_GPL(noop_direct_IO);
|
| 1655 |
|
| 1656 |
|
| 1657 | void kfree_link(void *p)
|
| 1658 | {
|
| 1659 | kfree(p);
|
| 1660 | }
|
| 1661 | EXPORT_SYMBOL(kfree_link);
|
| 1662 |
|
| 1663 | struct inode *alloc_anon_inode(struct super_block *s)
|
| 1664 | {
|
| 1665 | static const struct address_space_operations anon_aops = {
|
| 1666 | .dirty_folio = noop_dirty_folio,
|
| 1667 | };
|
| 1668 | struct inode *inode = new_inode_pseudo(s);
|
| 1669 |
|
| 1670 | if (!inode)
|
| 1671 | return ERR_PTR(-ENOMEM);
|
| 1672 |
|
| 1673 | inode->i_ino = get_next_ino();
|
| 1674 | inode->i_mapping->a_ops = &anon_aops;
|
| 1675 |
|
| 1676 | |
| 1677 | * Mark the inode dirty from the very beginning,
|
| 1678 | * that way it will never be moved to the dirty
|
| 1679 | * list because mark_inode_dirty() will think
|
| 1680 | * that it already _is_ on the dirty list.
|
| 1681 |
|
| 1682 | inode_state_assign_raw(inode, I_DIRTY);
|
| 1683 | |
| 1684 | * Historically anonymous inodes don't have a type at all and
|
| 1685 | * userspace has come to rely on this.
|
| 1686 |
|
| 1687 | inode->i_mode = S_IRUSR | S_IWUSR;
|
| 1688 | inode->i_uid = current_fsuid();
|
| 1689 | inode->i_gid = current_fsgid();
|
| 1690 | inode->i_flags |= S_PRIVATE | S_ANON_INODE;
|
| 1691 | simple_inode_init_ts(inode);
|
| 1692 | return inode;
|
| 1693 | }
|
| 1694 | EXPORT_SYMBOL(alloc_anon_inode);
|
| 1695 |
|
| 1696 | |
| 1697 | * simple_get_link - generic helper to get the target of "fast" symlinks
|
| 1698 | * @dentry: not used here
|
| 1699 | * @inode: the symlink inode
|
| 1700 | * @done: not used here
|
| 1701 | *
|
| 1702 | * Generic helper for filesystems to use for symlink inodes where a pointer to
|
| 1703 | * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
|
| 1704 | * since as an optimization the path lookup code uses any non-NULL ->i_link
|
| 1705 | * directly, without calling ->get_link(). But ->get_link() still must be set,
|
| 1706 | * to mark the inode_operations as being for a symlink.
|
| 1707 | *
|
| 1708 | * Return: the symlink target
|
| 1709 |
|
| 1710 | const char *simple_get_link(struct dentry *dentry, struct inode *inode,
|
| 1711 | struct delayed_call *done)
|
| 1712 | {
|
| 1713 | return inode->i_link;
|
| 1714 | }
|
| 1715 | EXPORT_SYMBOL(simple_get_link);
|
| 1716 |
|
| 1717 | const struct inode_operations simple_symlink_inode_operations = {
|
| 1718 | .get_link = simple_get_link,
|
| 1719 | };
|
| 1720 | EXPORT_SYMBOL(simple_symlink_inode_operations);
|
| 1721 |
|
| 1722 | |
| 1723 | * Operations for a permanently empty directory.
|
| 1724 |
|
| 1725 | static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
|
| 1726 | {
|
| 1727 | return ERR_PTR(-ENOENT);
|
| 1728 | }
|
| 1729 |
|
| 1730 | static int empty_dir_setattr(struct mnt_idmap *idmap,
|
| 1731 | struct dentry *dentry, struct iattr *attr)
|
| 1732 | {
|
| 1733 | return -EPERM;
|
| 1734 | }
|
| 1735 |
|
| 1736 | static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
|
| 1737 | {
|
| 1738 | return -EOPNOTSUPP;
|
| 1739 | }
|
| 1740 |
|
| 1741 | static const struct inode_operations empty_dir_inode_operations = {
|
| 1742 | .lookup = empty_dir_lookup,
|
| 1743 | .setattr = empty_dir_setattr,
|
| 1744 | .listxattr = empty_dir_listxattr,
|
| 1745 | };
|
| 1746 |
|
| 1747 | static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
|
| 1748 | {
|
| 1749 |
|
| 1750 | return generic_file_llseek_size(file, offset, whence, 2, 2);
|
| 1751 | }
|
| 1752 |
|
| 1753 | static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
|
| 1754 | {
|
| 1755 | dir_emit_dots(file, ctx);
|
| 1756 | return 0;
|
| 1757 | }
|
| 1758 |
|
| 1759 | static const struct file_operations empty_dir_operations = {
|
| 1760 | .llseek = empty_dir_llseek,
|
| 1761 | .read = generic_read_dir,
|
| 1762 | .iterate_shared = empty_dir_readdir,
|
| 1763 | .fsync = noop_fsync,
|
| 1764 | };
|
| 1765 |
|
| 1766 |
|
| 1767 | void make_empty_dir_inode(struct inode *inode)
|
| 1768 | {
|
| 1769 | set_nlink(inode, 2);
|
| 1770 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
|
| 1771 | inode->i_uid = GLOBAL_ROOT_UID;
|
| 1772 | inode->i_gid = GLOBAL_ROOT_GID;
|
| 1773 | inode->i_rdev = 0;
|
| 1774 | inode->i_size = 0;
|
| 1775 | inode->i_blkbits = PAGE_SHIFT;
|
| 1776 | inode->i_blocks = 0;
|
| 1777 |
|
| 1778 | inode->i_op = &empty_dir_inode_operations;
|
| 1779 | inode->i_opflags &= ~IOP_XATTR;
|
| 1780 | inode->i_fop = &empty_dir_operations;
|
| 1781 | }
|
| 1782 |
|
| 1783 | bool is_empty_dir_inode(struct inode *inode)
|
| 1784 | {
|
| 1785 | return (inode->i_fop == &empty_dir_operations) &&
|
| 1786 | (inode->i_op == &empty_dir_inode_operations);
|
| 1787 | }
|
| 1788 |
|
| 1789 | #if IS_ENABLED(CONFIG_UNICODE)
|
| 1790 | |
| 1791 | * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
|
| 1792 | * @dentry: dentry whose name we are checking against
|
| 1793 | * @len: len of name of dentry
|
| 1794 | * @str: str pointer to name of dentry
|
| 1795 | * @name: Name to compare against
|
| 1796 | *
|
| 1797 | * Return: 0 if names match, 1 if mismatch, or -ERRNO
|
| 1798 |
|
| 1799 | int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
|
| 1800 | const char *str, const struct qstr *name)
|
| 1801 | {
|
| 1802 | const struct dentry *parent;
|
| 1803 | const struct inode *dir;
|
| 1804 | union shortname_store strbuf;
|
| 1805 | struct qstr qstr;
|
| 1806 |
|
| 1807 | |
| 1808 | * Attempt a case-sensitive match first. It is cheaper and
|
| 1809 | * should cover most lookups, including all the sane
|
| 1810 | * applications that expect a case-sensitive filesystem.
|
| 1811 | *
|
| 1812 | * This comparison is safe under RCU because the caller
|
| 1813 | * guarantees the consistency between str and len. See
|
| 1814 | * __d_lookup_rcu_op_compare() for details.
|
| 1815 |
|
| 1816 | if (len == name->len && !memcmp(str, name->name, len))
|
| 1817 | return 0;
|
| 1818 |
|
| 1819 | parent = READ_ONCE(dentry->d_parent);
|
| 1820 | dir = READ_ONCE(parent->d_inode);
|
| 1821 | if (!dir || !IS_CASEFOLDED(dir))
|
| 1822 | return 1;
|
| 1823 |
|
| 1824 | qstr.len = len;
|
| 1825 | qstr.name = str;
|
| 1826 | |
| 1827 | * If the dentry name is stored in-line, then it may be concurrently
|
| 1828 | * modified by a rename. If this happens, the VFS will eventually retry
|
| 1829 | * the lookup, so it doesn't matter what ->d_compare() returns.
|
| 1830 | * However, it's unsafe to call utf8_strncasecmp() with an unstable
|
| 1831 | * string. Therefore, we have to copy the name into a temporary buffer.
|
| 1832 | * As above, len is guaranteed to match str, so the shortname case
|
| 1833 | * is exactly when str points to ->d_shortname.
|
| 1834 |
|
| 1835 | if (qstr.name == dentry->d_shortname.string) {
|
| 1836 | strbuf = dentry->d_shortname; |
| 1837 | qstr.name = strbuf.string;
|
| 1838 |
|
| 1839 | barrier();
|
| 1840 | }
|
| 1841 |
|
| 1842 | return utf8_strncasecmp(dentry->d_sb->s_encoding, name, &qstr);
|
| 1843 | }
|
| 1844 | EXPORT_SYMBOL(generic_ci_d_compare);
|
| 1845 |
|
| 1846 | |
| 1847 | * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
|
| 1848 | * @dentry: dentry of the parent directory
|
| 1849 | * @str: qstr of name whose hash we should fill in
|
| 1850 | *
|
| 1851 | * Return: 0 if hash was successful or unchanged, and -EINVAL on error
|
| 1852 |
|
| 1853 | int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
|
| 1854 | {
|
| 1855 | const struct inode *dir = READ_ONCE(dentry->d_inode);
|
| 1856 | struct super_block *sb = dentry->d_sb;
|
| 1857 | const struct unicode_map *um = sb->s_encoding;
|
| 1858 | int ret;
|
| 1859 |
|
| 1860 | if (!dir || !IS_CASEFOLDED(dir))
|
| 1861 | return 0;
|
| 1862 |
|
| 1863 | ret = utf8_casefold_hash(um, dentry, str);
|
| 1864 | if (ret < 0 && sb_has_strict_encoding(sb))
|
| 1865 | return -EINVAL;
|
| 1866 | return 0;
|
| 1867 | }
|
| 1868 | EXPORT_SYMBOL(generic_ci_d_hash);
|
| 1869 |
|
| 1870 | static const struct dentry_operations generic_ci_dentry_ops = {
|
| 1871 | .d_hash = generic_ci_d_hash,
|
| 1872 | .d_compare = generic_ci_d_compare,
|
| 1873 | #ifdef CONFIG_FS_ENCRYPTION
|
| 1874 | .d_revalidate = fscrypt_d_revalidate,
|
| 1875 | #endif
|
| 1876 | };
|
| 1877 |
|
| 1878 | |
| 1879 | * generic_ci_match() - Match a name (case-insensitively) with a dirent.
|
| 1880 | * This is a filesystem helper for comparison with directory entries.
|
| 1881 | * generic_ci_d_compare should be used in VFS' ->d_compare instead.
|
| 1882 | *
|
| 1883 | * @parent: Inode of the parent of the dirent under comparison
|
| 1884 | * @name: name under lookup.
|
| 1885 | * @folded_name: Optional pre-folded name under lookup
|
| 1886 | * @de_name: Dirent name.
|
| 1887 | * @de_name_len: dirent name length.
|
| 1888 | *
|
| 1889 | * Test whether a case-insensitive directory entry matches the filename
|
| 1890 | * being searched. If @folded_name is provided, it is used instead of
|
| 1891 | * recalculating the casefold of @name.
|
| 1892 | *
|
| 1893 | * Return: > 0 if the directory entry matches, 0 if it doesn't match, or
|
| 1894 | * < 0 on error.
|
| 1895 |
|
| 1896 | int generic_ci_match(const struct inode *parent,
|
| 1897 | const struct qstr *name,
|
| 1898 | const struct qstr *folded_name,
|
| 1899 | const u8 *de_name, u32 de_name_len)
|
| 1900 | {
|
| 1901 | const struct super_block *sb = parent->i_sb;
|
| 1902 | const struct unicode_map *um = sb->s_encoding;
|
| 1903 | struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
|
| 1904 | struct qstr dirent = QSTR_INIT(de_name, de_name_len);
|
| 1905 | int res = 0;
|
| 1906 |
|
| 1907 | if (IS_ENCRYPTED(parent)) {
|
| 1908 | const struct fscrypt_str encrypted_name =
|
| 1909 | FSTR_INIT((u8 *) de_name, de_name_len);
|
| 1910 |
|
| 1911 | if (WARN_ON_ONCE(!fscrypt_has_encryption_key(parent)))
|
| 1912 | return -EINVAL;
|
| 1913 |
|
| 1914 | decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
|
| 1915 | if (!decrypted_name.name)
|
| 1916 | return -ENOMEM;
|
| 1917 | res = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
|
| 1918 | &decrypted_name);
|
| 1919 | if (res < 0) {
|
| 1920 | kfree(decrypted_name.name);
|
| 1921 | return res;
|
| 1922 | }
|
| 1923 | dirent.name = decrypted_name.name;
|
| 1924 | dirent.len = decrypted_name.len;
|
| 1925 | }
|
| 1926 |
|
| 1927 | |
| 1928 | * Attempt a case-sensitive match first. It is cheaper and
|
| 1929 | * should cover most lookups, including all the sane
|
| 1930 | * applications that expect a case-sensitive filesystem.
|
| 1931 |
|
| 1932 |
|
| 1933 | if (dirent.len == name->len &&
|
| 1934 | !memcmp(name->name, dirent.name, dirent.len))
|
| 1935 | goto out;
|
| 1936 |
|
| 1937 | if (folded_name->name)
|
| 1938 | res = utf8_strncasecmp_folded(um, folded_name, &dirent);
|
| 1939 | else
|
| 1940 | res = utf8_strncasecmp(um, name, &dirent);
|
| 1941 |
|
| 1942 | out:
|
| 1943 | kfree(decrypted_name.name);
|
| 1944 | if (res < 0 && sb_has_strict_encoding(sb)) {
|
| 1945 | pr_err_ratelimited("Directory contains filename that is invalid UTF-8");
|
| 1946 | return 0;
|
| 1947 | }
|
| 1948 | return !res;
|
| 1949 | }
|
| 1950 | EXPORT_SYMBOL(generic_ci_match);
|
| 1951 | #endif
|
| 1952 |
|
| 1953 | #ifdef CONFIG_FS_ENCRYPTION
|
| 1954 | static const struct dentry_operations generic_encrypted_dentry_ops = {
|
| 1955 | .d_revalidate = fscrypt_d_revalidate,
|
| 1956 | };
|
| 1957 | #endif
|
| 1958 |
|
| 1959 | |
| 1960 | * generic_set_sb_d_ops - helper for choosing the set of
|
| 1961 | * filesystem-wide dentry operations for the enabled features
|
| 1962 | * @sb: superblock to be configured
|
| 1963 | *
|
| 1964 | * Filesystems supporting casefolding and/or fscrypt can call this
|
| 1965 | * helper at mount-time to configure default dentry_operations to the
|
| 1966 | * best set of dentry operations required for the enabled features.
|
| 1967 | * The helper must be called after these have been configured, but
|
| 1968 | * before the root dentry is created.
|
| 1969 |
|
| 1970 | void generic_set_sb_d_ops(struct super_block *sb)
|
| 1971 | {
|
| 1972 | #if IS_ENABLED(CONFIG_UNICODE)
|
| 1973 | if (sb->s_encoding) {
|
| 1974 | set_default_d_op(sb, &generic_ci_dentry_ops);
|
| 1975 | return;
|
| 1976 | }
|
| 1977 | #endif
|
| 1978 | #ifdef CONFIG_FS_ENCRYPTION
|
| 1979 | if (sb->s_cop) {
|
| 1980 | set_default_d_op(sb, &generic_encrypted_dentry_ops);
|
| 1981 | return;
|
| 1982 | }
|
| 1983 | #endif
|
| 1984 | }
|
| 1985 | EXPORT_SYMBOL(generic_set_sb_d_ops);
|
| 1986 |
|
| 1987 | |
| 1988 | * inode_maybe_inc_iversion - increments i_version
|
| 1989 | * @inode: inode with the i_version that should be updated
|
| 1990 | * @force: increment the counter even if it's not necessary?
|
| 1991 | *
|
| 1992 | * Every time the inode is modified, the i_version field must be seen to have
|
| 1993 | * changed by any observer.
|
| 1994 | *
|
| 1995 | * If "force" is set or the QUERIED flag is set, then ensure that we increment
|
| 1996 | * the value, and clear the queried flag.
|
| 1997 | *
|
| 1998 | * In the common case where neither is set, then we can return "false" without
|
| 1999 | * updating i_version.
|
| 2000 | *
|
| 2001 | * If this function returns false, and no other metadata has changed, then we
|
| 2002 | * can avoid logging the metadata.
|
| 2003 |
|
| 2004 | bool inode_maybe_inc_iversion(struct inode *inode, bool force)
|
| 2005 | {
|
| 2006 | u64 cur, new;
|
| 2007 |
|
| 2008 | |
| 2009 | * The i_version field is not strictly ordered with any other inode
|
| 2010 | * information, but the legacy inode_inc_iversion code used a spinlock
|
| 2011 | * to serialize increments.
|
| 2012 | *
|
| 2013 | * We add a full memory barrier to ensure that any de facto ordering
|
| 2014 | * with other state is preserved (either implicitly coming from cmpxchg
|
| 2015 | * or explicitly from smp_mb if we don't know upfront if we will execute
|
| 2016 | * the former).
|
| 2017 | *
|
| 2018 | * These barriers pair with inode_query_iversion().
|
| 2019 |
|
| 2020 | cur = inode_peek_iversion_raw(inode);
|
| 2021 | if (!force && !(cur & I_VERSION_QUERIED)) {
|
| 2022 | smp_mb();
|
| 2023 | cur = inode_peek_iversion_raw(inode);
|
| 2024 | }
|
| 2025 |
|
| 2026 | do {
|
| 2027 |
|
| 2028 | if (!force && !(cur & I_VERSION_QUERIED))
|
| 2029 | return false;
|
| 2030 |
|
| 2031 |
|
| 2032 | new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT;
|
| 2033 | } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
|
| 2034 | return true;
|
| 2035 | }
|
| 2036 | EXPORT_SYMBOL(inode_maybe_inc_iversion);
|
| 2037 |
|
| 2038 | |
| 2039 | * inode_query_iversion - read i_version for later use
|
| 2040 | * @inode: inode from which i_version should be read
|
| 2041 | *
|
| 2042 | * Read the inode i_version counter. This should be used by callers that wish
|
| 2043 | * to store the returned i_version for later comparison. This will guarantee
|
| 2044 | * that a later query of the i_version will result in a different value if
|
| 2045 | * anything has changed.
|
| 2046 | *
|
| 2047 | * In this implementation, we fetch the current value, set the QUERIED flag and
|
| 2048 | * then try to swap it into place with a cmpxchg, if it wasn't already set. If
|
| 2049 | * that fails, we try again with the newly fetched value from the cmpxchg.
|
| 2050 |
|
| 2051 | u64 inode_query_iversion(struct inode *inode)
|
| 2052 | {
|
| 2053 | u64 cur, new;
|
| 2054 | bool fenced = false;
|
| 2055 |
|
| 2056 | |
| 2057 | * Memory barriers (implicit in cmpxchg, explicit in smp_mb) pair with
|
| 2058 | * inode_maybe_inc_iversion(), see that routine for more details.
|
| 2059 |
|
| 2060 | cur = inode_peek_iversion_raw(inode);
|
| 2061 | do {
|
| 2062 |
|
| 2063 | if (cur & I_VERSION_QUERIED) {
|
| 2064 | if (!fenced)
|
| 2065 | smp_mb();
|
| 2066 | break;
|
| 2067 | }
|
| 2068 |
|
| 2069 | fenced = true;
|
| 2070 | new = cur | I_VERSION_QUERIED;
|
| 2071 | } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
|
| 2072 | return cur >> I_VERSION_QUERIED_SHIFT;
|
| 2073 | }
|
| 2074 | EXPORT_SYMBOL(inode_query_iversion);
|
| 2075 |
|
| 2076 | ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
|
| 2077 | ssize_t direct_written, ssize_t buffered_written)
|
| 2078 | {
|
| 2079 | struct address_space *mapping = iocb->ki_filp->f_mapping;
|
| 2080 | loff_t pos = iocb->ki_pos - buffered_written;
|
| 2081 | loff_t end = iocb->ki_pos - 1;
|
| 2082 | int err;
|
| 2083 |
|
| 2084 | |
| 2085 | * If the buffered write fallback returned an error, we want to return
|
| 2086 | * the number of bytes which were written by direct I/O, or the error
|
| 2087 | * code if that was zero.
|
| 2088 | *
|
| 2089 | * Note that this differs from normal direct-io semantics, which will
|
| 2090 | * return -EFOO even if some bytes were written.
|
| 2091 |
|
| 2092 | if (unlikely(buffered_written < 0)) {
|
| 2093 | if (direct_written)
|
| 2094 | return direct_written;
|
| 2095 | return buffered_written;
|
| 2096 | }
|
| 2097 |
|
| 2098 | |
| 2099 | * We need to ensure that the page cache pages are written to disk and
|
| 2100 | * invalidated to preserve the expected O_DIRECT semantics.
|
| 2101 |
|
| 2102 | err = filemap_write_and_wait_range(mapping, pos, end);
|
| 2103 | if (err < 0) {
|
| 2104 | |
| 2105 | * We don't know how much we wrote, so just return the number of
|
| 2106 | * bytes which were direct-written
|
| 2107 |
|
| 2108 | iocb->ki_pos -= buffered_written;
|
| 2109 | if (direct_written)
|
| 2110 | return direct_written;
|
| 2111 | return err;
|
| 2112 | }
|
| 2113 | invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
|
| 2114 | return direct_written + buffered_written;
|
| 2115 | }
|
| 2116 | EXPORT_SYMBOL_GPL(direct_write_fallback);
|
| 2117 |
|
| 2118 | |
| 2119 | * simple_inode_init_ts - initialize the timestamps for a new inode
|
| 2120 | * @inode: inode to be initialized
|
| 2121 | *
|
| 2122 | * When a new inode is created, most filesystems set the timestamps to the
|
| 2123 | * current time. Add a helper to do this.
|
| 2124 |
|
| 2125 | struct timespec64 simple_inode_init_ts(struct inode *inode)
|
| 2126 | {
|
| 2127 | struct timespec64 ts = inode_set_ctime_current(inode);
|
| 2128 |
|
| 2129 | inode_set_atime_to_ts(inode, ts);
|
| 2130 | inode_set_mtime_to_ts(inode, ts);
|
| 2131 | return ts;
|
| 2132 | }
|
| 2133 | EXPORT_SYMBOL(simple_inode_init_ts);
|
| 2134 |
|
| 2135 | struct dentry *stashed_dentry_get(struct dentry **stashed)
|
| 2136 | {
|
| 2137 | struct dentry *dentry;
|
| 2138 |
|
| 2139 | guard(rcu)();
|
| 2140 | dentry = rcu_dereference(*stashed);
|
| 2141 | if (!dentry)
|
| 2142 | return NULL;
|
| 2143 | if (IS_ERR(dentry))
|
| 2144 | return dentry;
|
| 2145 | if (!lockref_get_not_dead(&dentry->d_lockref))
|
| 2146 | return NULL;
|
| 2147 | return dentry;
|
| 2148 | }
|
| 2149 |
|
| 2150 | static struct dentry *prepare_anon_dentry(struct dentry **stashed,
|
| 2151 | struct super_block *sb,
|
| 2152 | void *data)
|
| 2153 | {
|
| 2154 | struct dentry *dentry;
|
| 2155 | struct inode *inode;
|
| 2156 | const struct stashed_operations *sops = sb->s_fs_info;
|
| 2157 | int ret;
|
| 2158 |
|
| 2159 | inode = new_inode_pseudo(sb);
|
| 2160 | if (!inode) {
|
| 2161 | sops->put_data(data);
|
| 2162 | return ERR_PTR(-ENOMEM);
|
| 2163 | }
|
| 2164 |
|
| 2165 | inode->i_flags |= S_IMMUTABLE;
|
| 2166 | inode->i_mode = S_IFREG;
|
| 2167 | simple_inode_init_ts(inode);
|
| 2168 |
|
| 2169 | ret = sops->init_inode(inode, data);
|
| 2170 | if (ret < 0) {
|
| 2171 | iput(inode);
|
| 2172 | return ERR_PTR(ret);
|
| 2173 | }
|
| 2174 |
|
| 2175 |
|
| 2176 | WARN_ON_ONCE(!S_ISREG(inode->i_mode));
|
| 2177 |
|
| 2178 | dentry = d_alloc_anon(sb);
|
| 2179 | if (!dentry) {
|
| 2180 | iput(inode);
|
| 2181 | return ERR_PTR(-ENOMEM);
|
| 2182 | }
|
| 2183 |
|
| 2184 |
|
| 2185 | dentry->d_fsdata = stashed;
|
| 2186 |
|
| 2187 |
|
| 2188 | d_instantiate(dentry, inode);
|
| 2189 | return dentry;
|
| 2190 | }
|
| 2191 |
|
| 2192 | struct dentry *stash_dentry(struct dentry **stashed, struct dentry *dentry)
|
| 2193 | {
|
| 2194 | guard(rcu)();
|
| 2195 | for (;;) {
|
| 2196 | struct dentry *old;
|
| 2197 |
|
| 2198 |
|
| 2199 | old = cmpxchg(stashed, NULL, dentry);
|
| 2200 | if (likely(!old))
|
| 2201 | return dentry;
|
| 2202 |
|
| 2203 |
|
| 2204 | if (lockref_get_not_dead(&old->d_lockref))
|
| 2205 | return old;
|
| 2206 |
|
| 2207 |
|
| 2208 | if (likely(try_cmpxchg(stashed, &old, dentry)))
|
| 2209 | return dentry;
|
| 2210 | }
|
| 2211 | }
|
| 2212 |
|
| 2213 | |
| 2214 | * path_from_stashed - create path from stashed or new dentry
|
| 2215 | * @stashed: where to retrieve or stash dentry
|
| 2216 | * @mnt: mnt of the filesystems to use
|
| 2217 | * @data: data to store in inode->i_private
|
| 2218 | * @path: path to create
|
| 2219 | *
|
| 2220 | * The function tries to retrieve a stashed dentry from @stashed. If the dentry
|
| 2221 | * is still valid then it will be reused. If the dentry isn't able the function
|
| 2222 | * will allocate a new dentry and inode. It will then check again whether it
|
| 2223 | * can reuse an existing dentry in case one has been added in the meantime or
|
| 2224 | * update @stashed with the newly added dentry.
|
| 2225 | *
|
| 2226 | * Special-purpose helper for nsfs and pidfs.
|
| 2227 | *
|
| 2228 | * Return: On success zero and on failure a negative error is returned.
|
| 2229 |
|
| 2230 | int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,
|
| 2231 | struct path *path)
|
| 2232 | {
|
| 2233 | struct dentry *dentry, *res;
|
| 2234 | const struct stashed_operations *sops = mnt->mnt_sb->s_fs_info;
|
| 2235 |
|
| 2236 |
|
| 2237 | res = stashed_dentry_get(stashed);
|
| 2238 | if (IS_ERR(res))
|
| 2239 | return PTR_ERR(res);
|
| 2240 | if (res) {
|
| 2241 | sops->put_data(data);
|
| 2242 | goto make_path;
|
| 2243 | }
|
| 2244 |
|
| 2245 |
|
| 2246 | dentry = prepare_anon_dentry(stashed, mnt->mnt_sb, data);
|
| 2247 | if (IS_ERR(dentry))
|
| 2248 | return PTR_ERR(dentry);
|
| 2249 |
|
| 2250 |
|
| 2251 | if (sops->stash_dentry)
|
| 2252 | res = sops->stash_dentry(stashed, dentry);
|
| 2253 | else
|
| 2254 | res = stash_dentry(stashed, dentry);
|
| 2255 | if (IS_ERR(res)) {
|
| 2256 | dput(dentry);
|
| 2257 | return PTR_ERR(res);
|
| 2258 | }
|
| 2259 | if (res != dentry)
|
| 2260 | dput(dentry);
|
| 2261 |
|
| 2262 | make_path:
|
| 2263 | path->dentry = res;
|
| 2264 | path->mnt = mntget(mnt);
|
| 2265 | VFS_WARN_ON_ONCE(path->dentry->d_fsdata != stashed);
|
| 2266 | VFS_WARN_ON_ONCE(d_inode(path->dentry)->i_private != data);
|
| 2267 | return 0;
|
| 2268 | }
|
| 2269 |
|
| 2270 | void stashed_dentry_prune(struct dentry *dentry)
|
| 2271 | {
|
| 2272 | struct dentry **stashed = dentry->d_fsdata;
|
| 2273 | struct inode *inode = d_inode(dentry);
|
| 2274 |
|
| 2275 | if (WARN_ON_ONCE(!stashed))
|
| 2276 | return;
|
| 2277 |
|
| 2278 | if (!inode)
|
| 2279 | return;
|
| 2280 |
|
| 2281 | |
| 2282 | * Only replace our own @dentry as someone else might've
|
| 2283 | * already cleared out @dentry and stashed their own
|
| 2284 | * dentry in there.
|
| 2285 |
|
| 2286 | cmpxchg(stashed, dentry, NULL);
|
| 2287 | }
|
| 2288 |
|
| 2289 | |
| 2290 | * simple_start_creating - prepare to create a given name
|
| 2291 | * @parent: directory in which to prepare to create the name
|
| 2292 | * @name: the name to be created
|
| 2293 | *
|
| 2294 | * Required lock is taken and a lookup in performed prior to creating an
|
| 2295 | * object in a directory. No permission checking is performed.
|
| 2296 | *
|
| 2297 | * Returns: a negative dentry on which vfs_create() or similar may
|
| 2298 | * be attempted, or an error.
|
| 2299 |
|
| 2300 | struct dentry *simple_start_creating(struct dentry *parent, const char *name)
|
| 2301 | {
|
| 2302 | struct qstr qname = QSTR(name);
|
| 2303 | int err;
|
| 2304 |
|
| 2305 | err = lookup_noperm_common(&qname, parent);
|
| 2306 | if (err)
|
| 2307 | return ERR_PTR(err);
|
| 2308 | return start_dirop(parent, &qname, LOOKUP_CREATE | LOOKUP_EXCL);
|
| 2309 | }
|
| 2310 | EXPORT_SYMBOL(simple_start_creating);
|
| 2311 |
|
| 2312 |
|
| 2313 | void simple_done_creating(struct dentry *child)
|
| 2314 | {
|
| 2315 | end_creating(child);
|
| 2316 | }
|
| 2317 | EXPORT_SYMBOL(simple_done_creating);
|