Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
f28fc20
Documentation
LICENSES
arch
block
certs
crypto
drivers
fs
9p
adfs
affs
afs
Kconfig
Makefile
addr_list.c
addr_prefs.c
afs.h
afs_cm.h
afs_fs.h
afs_vl.h
callback.c
cell.c
cmservice.c
dir.c
dir_edit.c
dir_silly.c
dynroot.c
file.c
flock.c
fs_operation.c
fs_probe.c
fsclient.c
inode.c
internal.h
main.c
misc.c
mntpt.c
proc.c
protocol_afs.h
protocol_uae.h
protocol_yfs.h
rotate.c
rxrpc.c
security.c
server.c
server_list.c
super.c
validation.c
vl_alias.c
vl_list.c
vl_probe.c
vl_rotate.c
vlclient.c
volume.c
write.c
xattr.c
xdr_fs.h
yfsclient.c
autofs
bcachefs
befs
bfs
btrfs
cachefiles
ceph
coda
configfs
cramfs
crypto
debugfs
devpts
dlm
ecryptfs
efivarfs
efs
erofs
exfat
exportfs
ext2
ext4
f2fs
fat
freevxfs
fuse
gfs2
hfs
hfsplus
hostfs
hpfs
hugetlbfs
iomap
isofs
jbd2
jffs2
jfs
kernfs
lockd
minix
netfs
nfs
nfs_common
nfsd
nilfs2
nls
notify
ntfs3
ocfs2
omfs
openpromfs
orangefs
overlayfs
proc
pstore
qnx4
qnx6
quota
ramfs
romfs
smb
squashfs
sysfs
sysv
tests
tracefs
ubifs
udf
ufs
unicode
vboxsf
verity
xfs
zonefs
Kconfig
Kconfig.binfmt
Makefile
aio.c
anon_inodes.c
attr.c
backing-file.c
bad_inode.c
binfmt_elf.c
binfmt_elf_fdpic.c
binfmt_flat.c
binfmt_misc.c
binfmt_script.c
bpf_fs_kfuncs.c
buffer.c
char_dev.c
compat_binfmt_elf.c
coredump.c
d_path.c
dax.c
dcache.c
direct-io.c
drop_caches.c
eventfd.c
eventpoll.c
exec.c
fcntl.c
fhandle.c
file.c
file_table.c
filesystems.c
fs-writeback.c
fs_context.c
fs_parser.c
fs_pin.c
fs_struct.c
fs_types.c
fsopen.c
init.c
inode.c
internal.h
ioctl.c
kernel_read_file.c
libfs.c
locks.c
mbcache.c
mnt_idmapping.c
mount.h
mpage.c
namei.c
namespace.c
nsfs.c
open.c
pidfs.c
pipe.c
pnode.c
pnode.h
posix_acl.c
proc_namespace.c
read_write.c
readdir.c
remap_range.c
select.c
seq_file.c
signalfd.c
splice.c
stack.c
stat.c
statfs.c
super.c
sync.c
sysctls.c
timerfd.c
userfaultfd.c
utimes.c
xattr.c
include
init
io_uring
ipc
kernel
lib
mm
net
rust
samples
scripts
security
sound
tools
usr
virt
.clang-format
.clippy.toml
.cocciconfig
.editorconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
.rustfmt.toml
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
fs
/
afs
/
file.c
Copy path
Blame
Blame
Latest commit
History
History
467 lines (393 loc) · 11.3 KB
Breadcrumbs
linux
/
fs
/
afs
/
file.c
Top
File metadata and controls
Code
Blame
467 lines (393 loc) · 11.3 KB
Raw
// SPDX-License-Identifier: GPL-2.0-or-later /* AFS filesystem file handling * * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/pagemap.h> #include <linux/writeback.h> #include <linux/gfp.h> #include <linux/task_io_accounting_ops.h> #include <linux/mm.h> #include <linux/swap.h> #include <linux/netfs.h> #include <trace/events/netfs.h> #include "internal.h" static int afs_file_mmap(struct file *file, struct vm_area_struct *vma); static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter); static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags); static void afs_vm_open(struct vm_area_struct *area); static void afs_vm_close(struct vm_area_struct *area); static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); const struct file_operations afs_file_operations = { .open = afs_open, .release = afs_release, .llseek = generic_file_llseek, .read_iter = afs_file_read_iter, .write_iter = netfs_file_write_iter, .mmap = afs_file_mmap, .splice_read = afs_file_splice_read, .splice_write = iter_file_splice_write, .fsync = afs_fsync, .lock = afs_lock, .flock = afs_flock, }; const struct inode_operations afs_file_inode_operations = { .getattr = afs_getattr, .setattr = afs_setattr, .permission = afs_permission, }; const struct address_space_operations afs_file_aops = { .direct_IO = noop_direct_IO, .read_folio = netfs_read_folio, .readahead = netfs_readahead, .dirty_folio = netfs_dirty_folio, .release_folio = netfs_release_folio, .invalidate_folio = netfs_invalidate_folio, .migrate_folio = filemap_migrate_folio, .writepages = afs_writepages, }; static const struct vm_operations_struct afs_vm_ops = { .open = afs_vm_open, .close = afs_vm_close, .fault = filemap_fault, .map_pages = afs_vm_map_pages, .page_mkwrite = afs_page_mkwrite, }; /* * Discard a pin on a writeback key. */ void afs_put_wb_key(struct afs_wb_key *wbk) { if (wbk && refcount_dec_and_test(&wbk->usage)) { key_put(wbk->key); kfree(wbk); } } /* * Cache key for writeback. */ int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) { struct afs_wb_key *wbk, *p; wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); if (!wbk) return -ENOMEM; refcount_set(&wbk->usage, 2); wbk->key = af->key; spin_lock(&vnode->wb_lock); list_for_each_entry(p, &vnode->wb_keys, vnode_link) { if (p->key == wbk->key) goto found; } key_get(wbk->key); list_add_tail(&wbk->vnode_link, &vnode->wb_keys); spin_unlock(&vnode->wb_lock); af->wb = wbk; return 0; found: refcount_inc(&p->usage); spin_unlock(&vnode->wb_lock); af->wb = p; kfree(wbk); return 0; } /* * open an AFS file or directory and attach a key to it */ int afs_open(struct inode *inode, struct file *file) { struct afs_vnode *vnode = AFS_FS_I(inode); struct afs_file *af; struct key *key; int ret; _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); key = afs_request_key(vnode->volume->cell); if (IS_ERR(key)) { ret = PTR_ERR(key); goto error; } af = kzalloc(sizeof(*af), GFP_KERNEL); if (!af) { ret = -ENOMEM; goto error_key; } af->key = key; ret = afs_validate(vnode, key); if (ret < 0) goto error_af; if (file->f_mode & FMODE_WRITE) { ret = afs_cache_wb_key(vnode, af); if (ret < 0) goto error_af; } if (file->f_flags & O_TRUNC) set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE); file->private_data = af; _leave(" = 0"); return 0; error_af: kfree(af); error_key: key_put(key); error: _leave(" = %d", ret); return ret; } /* * release an AFS file or directory and discard its key */ int afs_release(struct inode *inode, struct file *file) { struct afs_vnode_cache_aux aux; struct afs_vnode *vnode = AFS_FS_I(inode); struct afs_file *af = file->private_data; loff_t i_size; int ret = 0; _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); if ((file->f_mode & FMODE_WRITE)) ret = vfs_fsync(file, 0); file->private_data = NULL; if (af->wb) afs_put_wb_key(af->wb); if ((file->f_mode & FMODE_WRITE)) { i_size = i_size_read(&vnode->netfs.inode); afs_set_cache_aux(vnode, &aux); fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size); } else { fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL); } key_put(af->key); kfree(af); afs_prune_wb_keys(vnode); _leave(" = %d", ret); return ret; } static void afs_fetch_data_notify(struct afs_operation *op) { struct netfs_io_subrequest *subreq = op->fetch.subreq; subreq->error = afs_op_error(op); netfs_read_subreq_terminated(subreq); } static void afs_fetch_data_success(struct afs_operation *op) { struct afs_vnode *vnode = op->file[0].vnode; _enter("op=%08x", op->debug_id); afs_vnode_commit_status(op, &op->file[0]); afs_stat_v(vnode, n_fetches); atomic_long_add(op->fetch.subreq->transferred, &op->net->n_fetch_bytes); afs_fetch_data_notify(op); } static void afs_fetch_data_aborted(struct afs_operation *op) { afs_check_for_remote_deletion(op); afs_fetch_data_notify(op); } static void afs_fetch_data_put(struct afs_operation *op) { op->fetch.subreq->error = afs_op_error(op); } const struct afs_operation_ops afs_fetch_data_operation = { .issue_afs_rpc = afs_fs_fetch_data, .issue_yfs_rpc = yfs_fs_fetch_data, .success = afs_fetch_data_success, .aborted = afs_fetch_data_aborted, .failed = afs_fetch_data_notify, .put = afs_fetch_data_put, }; /* * Fetch file data from the volume. */ static void afs_read_worker(struct work_struct *work) { struct netfs_io_subrequest *subreq = container_of(work, struct netfs_io_subrequest, work); struct afs_operation *op; struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode); struct key *key = subreq->rreq->netfs_priv; _enter("%s{%llx:%llu.%u},%x,,,", vnode->volume->name, vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique, key_serial(key)); op = afs_alloc_operation(key, vnode->volume); if (IS_ERR(op)) { subreq->error = PTR_ERR(op); netfs_read_subreq_terminated(subreq); return; } afs_op_set_vnode(op, 0, vnode); op->fetch.subreq = subreq; op->ops = &afs_fetch_data_operation; trace_netfs_sreq(subreq, netfs_sreq_trace_submit); afs_do_sync_operation(op); } static void afs_issue_read(struct netfs_io_subrequest *subreq) { INIT_WORK(&subreq->work, afs_read_worker); queue_work(system_long_wq, &subreq->work); } static int afs_init_request(struct netfs_io_request *rreq, struct file *file) { struct afs_vnode *vnode = AFS_FS_I(rreq->inode); if (file) rreq->netfs_priv = key_get(afs_file_key(file)); rreq->rsize = 256 * 1024; rreq->wsize = 256 * 1024 * 1024; switch (rreq->origin) { case NETFS_READ_SINGLE: if (!file) { struct key *key = afs_request_key(vnode->volume->cell); if (IS_ERR(key)) return PTR_ERR(key); rreq->netfs_priv = key; } break; case NETFS_WRITEBACK: case NETFS_WRITETHROUGH: case NETFS_UNBUFFERED_WRITE: case NETFS_DIO_WRITE: if (S_ISREG(rreq->inode->i_mode)) rreq->io_streams[0].avail = true; break; case NETFS_WRITEBACK_SINGLE: default: break; } return 0; } static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len, struct folio **foliop, void **_fsdata) { struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0; } static void afs_free_request(struct netfs_io_request *rreq) { key_put(rreq->netfs_priv); afs_put_wb_key(rreq->netfs_priv2); } static void afs_update_i_size(struct inode *inode, loff_t new_i_size) { struct afs_vnode *vnode = AFS_FS_I(inode); loff_t i_size; write_seqlock(&vnode->cb_lock); i_size = i_size_read(&vnode->netfs.inode); if (new_i_size > i_size) { i_size_write(&vnode->netfs.inode, new_i_size); inode_set_bytes(&vnode->netfs.inode, new_i_size); } write_sequnlock(&vnode->cb_lock); fscache_update_cookie(afs_vnode_cache(vnode), NULL, &new_i_size); } static void afs_netfs_invalidate_cache(struct netfs_io_request *wreq) { struct afs_vnode *vnode = AFS_FS_I(wreq->inode); afs_invalidate_cache(vnode, 0); } const struct netfs_request_ops afs_req_ops = { .init_request = afs_init_request, .free_request = afs_free_request, .check_write_begin = afs_check_write_begin, .issue_read = afs_issue_read, .update_i_size = afs_update_i_size, .invalidate_cache = afs_netfs_invalidate_cache, .begin_writeback = afs_begin_writeback, .prepare_write = afs_prepare_write, .issue_write = afs_issue_write, .retry_request = afs_retry_request, }; static void afs_add_open_mmap(struct afs_vnode *vnode) { if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) { down_write(&vnode->volume->open_mmaps_lock); if (list_empty(&vnode->cb_mmap_link)) list_add_tail(&vnode->cb_mmap_link, &vnode->volume->open_mmaps); up_write(&vnode->volume->open_mmaps_lock); } } static void afs_drop_open_mmap(struct afs_vnode *vnode) { if (atomic_add_unless(&vnode->cb_nr_mmap, -1, 1)) return; down_write(&vnode->volume->open_mmaps_lock); read_seqlock_excl(&vnode->cb_lock); // the only place where ->cb_nr_mmap may hit 0 // see __afs_break_callback() for the other side... if (atomic_dec_and_test(&vnode->cb_nr_mmap)) list_del_init(&vnode->cb_mmap_link); read_sequnlock_excl(&vnode->cb_lock); up_write(&vnode->volume->open_mmaps_lock); flush_work(&vnode->cb_work); } /* * Handle setting up a memory mapping on an AFS file. */ static int afs_file_mmap(struct file *file, struct vm_area_struct *vma) { struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); int ret; afs_add_open_mmap(vnode); ret = generic_file_mmap(file, vma); if (ret == 0) vma->vm_ops = &afs_vm_ops; else afs_drop_open_mmap(vnode); return ret; } static void afs_vm_open(struct vm_area_struct *vma) { afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); } static void afs_vm_close(struct vm_area_struct *vma) { afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); } static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff) { struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file)); if (afs_check_validity(vnode)) return filemap_map_pages(vmf, start_pgoff, end_pgoff); return 0; } static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) { struct inode *inode = file_inode(iocb->ki_filp); struct afs_vnode *vnode = AFS_FS_I(inode); struct afs_file *af = iocb->ki_filp->private_data; ssize_t ret; if (iocb->ki_flags & IOCB_DIRECT) return netfs_unbuffered_read_iter(iocb, iter); ret = netfs_start_io_read(inode); if (ret < 0) return ret; ret = afs_validate(vnode, af->key); if (ret == 0) ret = filemap_read(iocb, iter, 0); netfs_end_io_read(inode); return ret; } static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { struct inode *inode = file_inode(in); struct afs_vnode *vnode = AFS_FS_I(inode); struct afs_file *af = in->private_data; ssize_t ret; ret = netfs_start_io_read(inode); if (ret < 0) return ret; ret = afs_validate(vnode, af->key); if (ret == 0) ret = filemap_splice_read(in, ppos, pipe, len, flags); netfs_end_io_read(inode); return ret; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
You can’t perform that action at this time.