file.c 13 KB

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