file.c 13 KB

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