1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* AFS filesystem file handling 3 * 4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/init.h> 11 #include <linux/fs.h> 12 #include <linux/pagemap.h> 13 #include <linux/writeback.h> 14 #include <linux/gfp.h> 15 #include <linux/task_io_accounting_ops.h> 16 #include <linux/mm.h> 17 #include <linux/swap.h> 18 #include <linux/netfs.h> 19 #include <trace/events/netfs.h> 20 #include "internal.h" 21 22 static int afs_file_mmap_prepare(struct vm_area_desc *desc); 23 24 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter); 25 static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos, 26 struct pipe_inode_info *pipe, 27 size_t len, unsigned int flags); 28 static void afs_vm_open(struct vm_area_struct *area); 29 static void afs_vm_close(struct vm_area_struct *area); 30 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); 31 static int afs_mapped(unsigned long start, unsigned long end, pgoff_t pgoff, 32 const struct file *file, void **vm_private_data); 33 34 const struct file_operations afs_file_operations = { 35 .open = afs_open, 36 .release = afs_release, 37 .llseek = generic_file_llseek, 38 .read_iter = afs_file_read_iter, 39 .write_iter = netfs_file_write_iter, 40 .mmap_prepare = afs_file_mmap_prepare, 41 .splice_read = afs_file_splice_read, 42 .splice_write = iter_file_splice_write, 43 .fsync = afs_fsync, 44 .lock = afs_lock, 45 .flock = afs_flock, 46 }; 47 48 const struct inode_operations afs_file_inode_operations = { 49 .getattr = afs_getattr, 50 .setattr = afs_setattr, 51 .permission = afs_permission, 52 }; 53 54 const struct address_space_operations afs_file_aops = { 55 .direct_IO = noop_direct_IO, 56 .read_folio = netfs_read_folio, 57 .readahead = netfs_readahead, 58 .dirty_folio = netfs_dirty_folio, 59 .release_folio = netfs_release_folio, 60 .invalidate_folio = netfs_invalidate_folio, 61 .migrate_folio = filemap_migrate_folio, 62 .writepages = afs_writepages, 63 }; 64 65 static const struct vm_operations_struct afs_vm_ops = { 66 .mapped = afs_mapped, 67 .open = afs_vm_open, 68 .close = afs_vm_close, 69 .fault = filemap_fault, 70 .map_pages = afs_vm_map_pages, 71 .page_mkwrite = afs_page_mkwrite, 72 }; 73 74 /* 75 * Discard a pin on a writeback key. 76 */ 77 void afs_put_wb_key(struct afs_wb_key *wbk) 78 { 79 if (wbk && refcount_dec_and_test(&wbk->usage)) { 80 key_put(wbk->key); 81 kfree(wbk); 82 } 83 } 84 85 /* 86 * Cache key for writeback. 87 */ 88 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) 89 { 90 struct afs_wb_key *wbk, *p; 91 92 wbk = kzalloc_obj(struct afs_wb_key); 93 if (!wbk) 94 return -ENOMEM; 95 refcount_set(&wbk->usage, 2); 96 wbk->key = af->key; 97 98 spin_lock(&vnode->wb_lock); 99 list_for_each_entry(p, &vnode->wb_keys, vnode_link) { 100 if (p->key == wbk->key) 101 goto found; 102 } 103 104 key_get(wbk->key); 105 list_add_tail(&wbk->vnode_link, &vnode->wb_keys); 106 spin_unlock(&vnode->wb_lock); 107 af->wb = wbk; 108 return 0; 109 110 found: 111 refcount_inc(&p->usage); 112 spin_unlock(&vnode->wb_lock); 113 af->wb = p; 114 kfree(wbk); 115 return 0; 116 } 117 118 /* 119 * open an AFS file or directory and attach a key to it 120 */ 121 int afs_open(struct inode *inode, struct file *file) 122 { 123 struct afs_vnode *vnode = AFS_FS_I(inode); 124 struct afs_file *af; 125 struct key *key; 126 int ret; 127 128 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 129 130 key = afs_request_key(vnode->volume->cell); 131 if (IS_ERR(key)) { 132 ret = PTR_ERR(key); 133 goto error; 134 } 135 136 af = kzalloc_obj(*af); 137 if (!af) { 138 ret = -ENOMEM; 139 goto error_key; 140 } 141 af->key = key; 142 143 ret = afs_validate(vnode, key); 144 if (ret < 0) 145 goto error_af; 146 147 if (file->f_mode & FMODE_WRITE) { 148 ret = afs_cache_wb_key(vnode, af); 149 if (ret < 0) 150 goto error_af; 151 } 152 153 if (file->f_flags & O_TRUNC) 154 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 155 156 fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE); 157 158 file->private_data = af; 159 _leave(" = 0"); 160 return 0; 161 162 error_af: 163 kfree(af); 164 error_key: 165 key_put(key); 166 error: 167 _leave(" = %d", ret); 168 return ret; 169 } 170 171 /* 172 * release an AFS file or directory and discard its key 173 */ 174 int afs_release(struct inode *inode, struct file *file) 175 { 176 struct afs_vnode_cache_aux aux; 177 struct afs_vnode *vnode = AFS_FS_I(inode); 178 struct afs_file *af = file->private_data; 179 loff_t i_size; 180 int ret = 0; 181 182 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 183 184 if ((file->f_mode & FMODE_WRITE)) 185 ret = vfs_fsync(file, 0); 186 187 file->private_data = NULL; 188 if (af->wb) 189 afs_put_wb_key(af->wb); 190 191 if ((file->f_mode & FMODE_WRITE)) { 192 i_size = i_size_read(&vnode->netfs.inode); 193 afs_set_cache_aux(vnode, &aux); 194 fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size); 195 } else { 196 fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL); 197 } 198 199 key_put(af->key); 200 kfree(af); 201 afs_prune_wb_keys(vnode); 202 _leave(" = %d", ret); 203 return ret; 204 } 205 206 static void afs_fetch_data_notify(struct afs_operation *op) 207 { 208 struct netfs_io_subrequest *subreq = op->fetch.subreq; 209 210 subreq->error = afs_op_error(op); 211 netfs_read_subreq_terminated(subreq); 212 } 213 214 static void afs_fetch_data_success(struct afs_operation *op) 215 { 216 struct afs_vnode *vnode = op->file[0].vnode; 217 218 _enter("op=%08x", op->debug_id); 219 afs_vnode_commit_status(op, &op->file[0]); 220 afs_stat_v(vnode, n_fetches); 221 atomic_long_add(op->fetch.subreq->transferred, &op->net->n_fetch_bytes); 222 afs_fetch_data_notify(op); 223 } 224 225 static void afs_fetch_data_aborted(struct afs_operation *op) 226 { 227 afs_check_for_remote_deletion(op); 228 afs_fetch_data_notify(op); 229 } 230 231 const struct afs_operation_ops afs_fetch_data_operation = { 232 .issue_afs_rpc = afs_fs_fetch_data, 233 .issue_yfs_rpc = yfs_fs_fetch_data, 234 .success = afs_fetch_data_success, 235 .aborted = afs_fetch_data_aborted, 236 .failed = afs_fetch_data_notify, 237 }; 238 239 static void afs_issue_read_call(struct afs_operation *op) 240 { 241 op->call_responded = false; 242 op->call_error = 0; 243 op->call_abort_code = 0; 244 if (test_bit(AFS_SERVER_FL_IS_YFS, &op->server->flags)) 245 yfs_fs_fetch_data(op); 246 else 247 afs_fs_fetch_data(op); 248 } 249 250 static void afs_end_read(struct afs_operation *op) 251 { 252 if (op->call_responded && op->server) 253 set_bit(AFS_SERVER_FL_RESPONDING, &op->server->flags); 254 255 if (!afs_op_error(op)) 256 afs_fetch_data_success(op); 257 else if (op->cumul_error.aborted) 258 afs_fetch_data_aborted(op); 259 else 260 afs_fetch_data_notify(op); 261 262 afs_end_vnode_operation(op); 263 afs_put_operation(op); 264 } 265 266 /* 267 * Perform I/O processing on an asynchronous call. The work item carries a ref 268 * to the call struct that we either need to release or to pass on. 269 */ 270 static void afs_read_receive(struct afs_call *call) 271 { 272 struct afs_operation *op = call->op; 273 enum afs_call_state state; 274 275 _enter(""); 276 277 state = READ_ONCE(call->state); 278 if (state == AFS_CALL_COMPLETE) 279 return; 280 trace_afs_read_recv(op, call); 281 282 while (state < AFS_CALL_COMPLETE && READ_ONCE(call->need_attention)) { 283 WRITE_ONCE(call->need_attention, false); 284 afs_deliver_to_call(call); 285 state = READ_ONCE(call->state); 286 } 287 288 if (state < AFS_CALL_COMPLETE) { 289 netfs_read_subreq_progress(op->fetch.subreq); 290 if (rxrpc_kernel_check_life(call->net->socket, call->rxcall)) 291 return; 292 /* rxrpc terminated the call. */ 293 afs_set_call_complete(call, call->error, call->abort_code); 294 } 295 296 op->call_abort_code = call->abort_code; 297 op->call_error = call->error; 298 op->call_responded = call->responded; 299 op->call = NULL; 300 call->op = NULL; 301 afs_put_call(call); 302 303 /* If the call failed, then we need to crank the server rotation 304 * handle and try the next. 305 */ 306 if (afs_select_fileserver(op)) { 307 afs_issue_read_call(op); 308 return; 309 } 310 311 afs_end_read(op); 312 } 313 314 void afs_fetch_data_async_rx(struct work_struct *work) 315 { 316 struct afs_call *call = container_of(work, struct afs_call, async_work); 317 318 afs_read_receive(call); 319 afs_put_call(call); 320 } 321 322 void afs_fetch_data_immediate_cancel(struct afs_call *call) 323 { 324 if (call->async) { 325 afs_get_call(call, afs_call_trace_wake); 326 if (!queue_work(afs_async_calls, &call->async_work)) 327 afs_deferred_put_call(call); 328 flush_work(&call->async_work); 329 } 330 } 331 332 /* 333 * Fetch file data from the volume. 334 */ 335 static void afs_issue_read(struct netfs_io_subrequest *subreq) 336 { 337 struct afs_operation *op; 338 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode); 339 struct key *key = subreq->rreq->netfs_priv; 340 341 _enter("%s{%llx:%llu.%u},%x,,,", 342 vnode->volume->name, 343 vnode->fid.vid, 344 vnode->fid.vnode, 345 vnode->fid.unique, 346 key_serial(key)); 347 348 op = afs_alloc_operation(key, vnode->volume); 349 if (IS_ERR(op)) { 350 subreq->error = PTR_ERR(op); 351 netfs_read_subreq_terminated(subreq); 352 return; 353 } 354 355 afs_op_set_vnode(op, 0, vnode); 356 357 op->fetch.subreq = subreq; 358 op->ops = &afs_fetch_data_operation; 359 360 trace_netfs_sreq(subreq, netfs_sreq_trace_submit); 361 362 if (subreq->rreq->origin == NETFS_READAHEAD || 363 subreq->rreq->iocb) { 364 op->flags |= AFS_OPERATION_ASYNC; 365 366 if (!afs_begin_vnode_operation(op)) { 367 subreq->error = afs_put_operation(op); 368 netfs_read_subreq_terminated(subreq); 369 return; 370 } 371 372 if (!afs_select_fileserver(op)) { 373 afs_end_read(op); 374 return; 375 } 376 377 afs_issue_read_call(op); 378 } else { 379 afs_do_sync_operation(op); 380 } 381 } 382 383 static int afs_init_request(struct netfs_io_request *rreq, struct file *file) 384 { 385 struct afs_vnode *vnode = AFS_FS_I(rreq->inode); 386 387 if (file) 388 rreq->netfs_priv = key_get(afs_file_key(file)); 389 rreq->rsize = 256 * 1024; 390 rreq->wsize = 256 * 1024 * 1024; 391 392 switch (rreq->origin) { 393 case NETFS_READ_SINGLE: 394 if (!file) { 395 struct key *key = afs_request_key(vnode->volume->cell); 396 397 if (IS_ERR(key)) 398 return PTR_ERR(key); 399 rreq->netfs_priv = key; 400 } 401 break; 402 case NETFS_WRITEBACK: 403 case NETFS_WRITETHROUGH: 404 case NETFS_UNBUFFERED_WRITE: 405 case NETFS_DIO_WRITE: 406 if (S_ISREG(rreq->inode->i_mode)) 407 rreq->io_streams[0].avail = true; 408 break; 409 case NETFS_WRITEBACK_SINGLE: 410 default: 411 break; 412 } 413 return 0; 414 } 415 416 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len, 417 struct folio **foliop, void **_fsdata) 418 { 419 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 420 421 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0; 422 } 423 424 static void afs_free_request(struct netfs_io_request *rreq) 425 { 426 key_put(rreq->netfs_priv); 427 afs_put_wb_key(rreq->netfs_priv2); 428 } 429 430 static void afs_update_i_size(struct inode *inode, loff_t new_i_size) 431 { 432 struct afs_vnode *vnode = AFS_FS_I(inode); 433 loff_t i_size; 434 435 write_seqlock(&vnode->cb_lock); 436 i_size = i_size_read(&vnode->netfs.inode); 437 if (new_i_size > i_size) { 438 i_size_write(&vnode->netfs.inode, new_i_size); 439 inode_set_bytes(&vnode->netfs.inode, new_i_size); 440 } 441 write_sequnlock(&vnode->cb_lock); 442 fscache_update_cookie(afs_vnode_cache(vnode), NULL, &new_i_size); 443 } 444 445 static void afs_netfs_invalidate_cache(struct netfs_io_request *wreq) 446 { 447 struct afs_vnode *vnode = AFS_FS_I(wreq->inode); 448 449 afs_invalidate_cache(vnode, 0); 450 } 451 452 const struct netfs_request_ops afs_req_ops = { 453 .init_request = afs_init_request, 454 .free_request = afs_free_request, 455 .check_write_begin = afs_check_write_begin, 456 .issue_read = afs_issue_read, 457 .update_i_size = afs_update_i_size, 458 .invalidate_cache = afs_netfs_invalidate_cache, 459 .begin_writeback = afs_begin_writeback, 460 .prepare_write = afs_prepare_write, 461 .issue_write = afs_issue_write, 462 .retry_request = afs_retry_request, 463 }; 464 465 static void afs_add_open_mmap(struct afs_vnode *vnode) 466 { 467 if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) { 468 down_write(&vnode->volume->open_mmaps_lock); 469 470 if (list_empty(&vnode->cb_mmap_link)) 471 list_add_tail(&vnode->cb_mmap_link, &vnode->volume->open_mmaps); 472 473 up_write(&vnode->volume->open_mmaps_lock); 474 } 475 } 476 477 static void afs_drop_open_mmap(struct afs_vnode *vnode) 478 { 479 if (atomic_add_unless(&vnode->cb_nr_mmap, -1, 1)) 480 return; 481 482 down_write(&vnode->volume->open_mmaps_lock); 483 484 read_seqlock_excl(&vnode->cb_lock); 485 // the only place where ->cb_nr_mmap may hit 0 486 // see __afs_break_callback() for the other side... 487 if (atomic_dec_and_test(&vnode->cb_nr_mmap)) 488 list_del_init(&vnode->cb_mmap_link); 489 read_sequnlock_excl(&vnode->cb_lock); 490 491 up_write(&vnode->volume->open_mmaps_lock); 492 flush_work(&vnode->cb_work); 493 } 494 495 /* 496 * Handle setting up a memory mapping on an AFS file. 497 */ 498 static int afs_file_mmap_prepare(struct vm_area_desc *desc) 499 { 500 int ret; 501 502 ret = generic_file_mmap_prepare(desc); 503 if (ret) 504 return ret; 505 506 desc->vm_ops = &afs_vm_ops; 507 return ret; 508 } 509 510 static int afs_mapped(unsigned long start, unsigned long end, pgoff_t pgoff, 511 const struct file *file, void **vm_private_data) 512 { 513 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 514 515 afs_add_open_mmap(vnode); 516 return 0; 517 } 518 519 static void afs_vm_open(struct vm_area_struct *vma) 520 { 521 struct file *file = vma->vm_file; 522 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 523 524 afs_add_open_mmap(vnode); 525 } 526 527 static void afs_vm_close(struct vm_area_struct *vma) 528 { 529 struct file *file = vma->vm_file; 530 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 531 532 afs_drop_open_mmap(vnode); 533 } 534 535 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff) 536 { 537 struct file *file = vmf->vma->vm_file; 538 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 539 540 if (afs_check_validity(vnode)) 541 return filemap_map_pages(vmf, start_pgoff, end_pgoff); 542 return 0; 543 } 544 545 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) 546 { 547 struct inode *inode = file_inode(iocb->ki_filp); 548 struct afs_vnode *vnode = AFS_FS_I(inode); 549 struct afs_file *af = iocb->ki_filp->private_data; 550 ssize_t ret; 551 552 if (iocb->ki_flags & IOCB_DIRECT) 553 return netfs_unbuffered_read_iter(iocb, iter); 554 555 ret = netfs_start_io_read(inode); 556 if (ret < 0) 557 return ret; 558 ret = afs_validate(vnode, af->key); 559 if (ret == 0) 560 ret = filemap_read(iocb, iter, 0); 561 netfs_end_io_read(inode); 562 return ret; 563 } 564 565 static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos, 566 struct pipe_inode_info *pipe, 567 size_t len, unsigned int flags) 568 { 569 struct inode *inode = file_inode(in); 570 struct afs_vnode *vnode = AFS_FS_I(inode); 571 struct afs_file *af = in->private_data; 572 ssize_t ret; 573 574 ret = netfs_start_io_read(inode); 575 if (ret < 0) 576 return ret; 577 ret = afs_validate(vnode, af->key); 578 if (ret == 0) 579 ret = filemap_splice_read(in, ppos, pipe, len, flags); 580 netfs_end_io_read(inode); 581 return ret; 582 } 583