Linux Audio

Check our new training course

Loading...
v5.9
  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 "internal.h"
 18
 19static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
 20static int afs_readpage(struct file *file, struct page *page);
 21static void afs_invalidatepage(struct page *page, unsigned int offset,
 22			       unsigned int length);
 23static int afs_releasepage(struct page *page, gfp_t gfp_flags);
 24
 25static int afs_readpages(struct file *filp, struct address_space *mapping,
 26			 struct list_head *pages, unsigned nr_pages);
 
 
 
 
 
 27
 28const struct file_operations afs_file_operations = {
 29	.open		= afs_open,
 30	.release	= afs_release,
 31	.llseek		= generic_file_llseek,
 32	.read_iter	= generic_file_read_iter,
 33	.write_iter	= afs_file_write,
 34	.mmap		= afs_file_mmap,
 35	.splice_read	= generic_file_splice_read,
 
 36	.fsync		= afs_fsync,
 37	.lock		= afs_lock,
 38	.flock		= afs_flock,
 39};
 40
 41const struct inode_operations afs_file_inode_operations = {
 42	.getattr	= afs_getattr,
 43	.setattr	= afs_setattr,
 44	.permission	= afs_permission,
 45	.listxattr	= afs_listxattr,
 46};
 47
 48const struct address_space_operations afs_fs_aops = {
 49	.readpage	= afs_readpage,
 50	.readpages	= afs_readpages,
 51	.set_page_dirty	= afs_set_page_dirty,
 52	.launder_page	= afs_launder_page,
 53	.releasepage	= afs_releasepage,
 54	.invalidatepage	= afs_invalidatepage,
 55	.write_begin	= afs_write_begin,
 56	.write_end	= afs_write_end,
 57	.writepage	= afs_writepage,
 58	.writepages	= afs_writepages,
 59};
 60
 
 
 
 
 
 
 
 61static const struct vm_operations_struct afs_vm_ops = {
 
 
 62	.fault		= filemap_fault,
 63	.map_pages	= filemap_map_pages,
 64	.page_mkwrite	= afs_page_mkwrite,
 65};
 66
 67/*
 68 * Discard a pin on a writeback key.
 69 */
 70void afs_put_wb_key(struct afs_wb_key *wbk)
 71{
 72	if (wbk && refcount_dec_and_test(&wbk->usage)) {
 73		key_put(wbk->key);
 74		kfree(wbk);
 75	}
 76}
 77
 78/*
 79 * Cache key for writeback.
 80 */
 81int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
 82{
 83	struct afs_wb_key *wbk, *p;
 84
 85	wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
 86	if (!wbk)
 87		return -ENOMEM;
 88	refcount_set(&wbk->usage, 2);
 89	wbk->key = af->key;
 90
 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
 97	key_get(wbk->key);
 98	list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
 99	spin_unlock(&vnode->wb_lock);
100	af->wb = wbk;
101	return 0;
102
103found:
104	refcount_inc(&p->usage);
105	spin_unlock(&vnode->wb_lock);
106	af->wb = p;
107	kfree(wbk);
108	return 0;
109}
110
111/*
112 * open an AFS file or directory and attach a key to it
113 */
114int afs_open(struct inode *inode, struct file *file)
115{
116	struct afs_vnode *vnode = AFS_FS_I(inode);
117	struct afs_file *af;
118	struct key *key;
119	int ret;
120
121	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
122
123	key = afs_request_key(vnode->volume->cell);
124	if (IS_ERR(key)) {
125		ret = PTR_ERR(key);
126		goto error;
127	}
128
129	af = kzalloc(sizeof(*af), GFP_KERNEL);
130	if (!af) {
131		ret = -ENOMEM;
132		goto error_key;
133	}
134	af->key = key;
135
136	ret = afs_validate(vnode, key);
137	if (ret < 0)
138		goto error_af;
139
140	if (file->f_mode & FMODE_WRITE) {
141		ret = afs_cache_wb_key(vnode, af);
142		if (ret < 0)
143			goto error_af;
144	}
145
146	if (file->f_flags & O_TRUNC)
147		set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
148	
 
 
149	file->private_data = af;
150	_leave(" = 0");
151	return 0;
152
153error_af:
154	kfree(af);
155error_key:
156	key_put(key);
157error:
158	_leave(" = %d", ret);
159	return ret;
160}
161
162/*
163 * release an AFS file or directory and discard its key
164 */
165int afs_release(struct inode *inode, struct file *file)
166{
 
167	struct afs_vnode *vnode = AFS_FS_I(inode);
168	struct afs_file *af = file->private_data;
 
169	int ret = 0;
170
171	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
172
173	if ((file->f_mode & FMODE_WRITE))
174		ret = vfs_fsync(file, 0);
175
176	file->private_data = NULL;
177	if (af->wb)
178		afs_put_wb_key(af->wb);
 
 
 
 
 
 
 
 
 
179	key_put(af->key);
180	kfree(af);
181	afs_prune_wb_keys(vnode);
182	_leave(" = %d", ret);
183	return ret;
184}
185
186/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187 * Dispose of a ref to a read record.
188 */
189void afs_put_read(struct afs_read *req)
190{
191	int i;
192
193	if (refcount_dec_and_test(&req->usage)) {
194		if (req->pages) {
195			for (i = 0; i < req->nr_pages; i++)
196				if (req->pages[i])
197					put_page(req->pages[i]);
198			if (req->pages != req->array)
199				kfree(req->pages);
200		}
201		kfree(req);
202	}
203}
204
205#ifdef CONFIG_AFS_FSCACHE
206/*
207 * deal with notification that a page was read from the cache
208 */
209static void afs_file_readpage_read_complete(struct page *page,
210					    void *data,
211					    int error)
212{
213	_enter("%p,%p,%d", page, data, error);
214
215	/* if the read completes with an error, we just unlock the page and let
216	 * the VM reissue the readpage */
217	if (!error)
218		SetPageUptodate(page);
219	unlock_page(page);
220}
221#endif
222
223static void afs_fetch_data_success(struct afs_operation *op)
224{
225	struct afs_vnode *vnode = op->file[0].vnode;
226
227	_enter("op=%08x", op->debug_id);
228	afs_vnode_commit_status(op, &op->file[0]);
229	afs_stat_v(vnode, n_fetches);
230	atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
 
231}
232
233static void afs_fetch_data_put(struct afs_operation *op)
234{
 
235	afs_put_read(op->fetch.req);
236}
237
238static 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_check_for_remote_deletion,
 
243	.put		= afs_fetch_data_put,
244};
245
246/*
247 * Fetch file data from the volume.
248 */
249int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *req)
250{
251	struct afs_operation *op;
252
253	_enter("%s{%llx:%llu.%u},%x,,,",
254	       vnode->volume->name,
255	       vnode->fid.vid,
256	       vnode->fid.vnode,
257	       vnode->fid.unique,
258	       key_serial(key));
259
260	op = afs_alloc_operation(key, vnode->volume);
261	if (IS_ERR(op))
 
 
262		return PTR_ERR(op);
 
263
264	afs_op_set_vnode(op, 0, vnode);
265
266	op->fetch.req	= afs_get_read(req);
267	op->ops		= &afs_fetch_data_operation;
268	return afs_do_sync_operation(op);
269}
270
271/*
272 * read page from file, directory or symlink, given a key to use
273 */
274int afs_page_filler(void *data, struct page *page)
275{
276	struct inode *inode = page->mapping->host;
277	struct afs_vnode *vnode = AFS_FS_I(inode);
278	struct afs_read *req;
279	struct key *key = data;
280	int ret;
281
282	_enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
 
 
283
284	BUG_ON(!PageLocked(page));
 
 
 
 
 
285
286	ret = -ESTALE;
287	if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
288		goto error;
289
290	/* is it cached? */
291#ifdef CONFIG_AFS_FSCACHE
292	ret = fscache_read_or_alloc_page(vnode->cache,
293					 page,
294					 afs_file_readpage_read_complete,
295					 NULL,
296					 GFP_KERNEL);
297#else
298	ret = -ENOBUFS;
299#endif
300	switch (ret) {
301		/* read BIO submitted (page in cache) */
302	case 0:
303		break;
304
305		/* page not yet cached */
306	case -ENODATA:
307		_debug("cache said ENODATA");
308		goto go_on;
309
310		/* page will not be cached */
311	case -ENOBUFS:
312		_debug("cache said ENOBUFS");
313
314		fallthrough;
315	default:
316	go_on:
317		req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
318		if (!req)
319			goto enomem;
320
321		/* We request a full page.  If the page is a partial one at the
322		 * end of the file, the server will return a short read and the
323		 * unmarshalling code will clear the unfilled space.
324		 */
325		refcount_set(&req->usage, 1);
326		req->pos = (loff_t)page->index << PAGE_SHIFT;
327		req->len = PAGE_SIZE;
328		req->nr_pages = 1;
329		req->pages = req->array;
330		req->pages[0] = page;
331		get_page(page);
332
333		/* read the contents of the file from the server into the
334		 * page */
335		ret = afs_fetch_data(vnode, key, req);
336		afs_put_read(req);
337
338		if (ret < 0) {
339			if (ret == -ENOENT) {
340				_debug("got NOENT from server"
341				       " - marking file deleted and stale");
342				set_bit(AFS_VNODE_DELETED, &vnode->flags);
343				ret = -ESTALE;
344			}
345
346#ifdef CONFIG_AFS_FSCACHE
347			fscache_uncache_page(vnode->cache, page);
348#endif
349			BUG_ON(PageFsCache(page));
350
351			if (ret == -EINTR ||
352			    ret == -ENOMEM ||
353			    ret == -ERESTARTSYS ||
354			    ret == -EAGAIN)
355				goto error;
356			goto io_error;
357		}
358
359		SetPageUptodate(page);
360
361		/* send the page to the cache */
362#ifdef CONFIG_AFS_FSCACHE
363		if (PageFsCache(page) &&
364		    fscache_write_page(vnode->cache, page, vnode->status.size,
365				       GFP_KERNEL) != 0) {
366			fscache_uncache_page(vnode->cache, page);
367			BUG_ON(PageFsCache(page));
368		}
369#endif
370		unlock_page(page);
371	}
372
373	_leave(" = 0");
374	return 0;
 
375
376io_error:
377	SetPageError(page);
378	goto error;
379enomem:
380	ret = -ENOMEM;
381error:
382	unlock_page(page);
383	_leave(" = %d", ret);
 
 
 
384	return ret;
385}
386
387/*
388 * read page from file, directory or symlink, given a file to nominate the key
389 * to be used
390 */
391static int afs_readpage(struct file *file, struct page *page)
392{
393	struct key *key;
394	int ret;
 
 
 
 
395
396	if (file) {
397		key = afs_file_key(file);
398		ASSERT(key != NULL);
399		ret = afs_page_filler(key, page);
400	} else {
401		struct inode *inode = page->mapping->host;
402		key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
403		if (IS_ERR(key)) {
404			ret = PTR_ERR(key);
405		} else {
406			ret = afs_page_filler(key, page);
407			key_put(key);
408		}
409	}
410	return ret;
411}
412
413/*
414 * Make pages available as they're filled.
415 */
416static void afs_readpages_page_done(struct afs_read *req)
417{
418#ifdef CONFIG_AFS_FSCACHE
419	struct afs_vnode *vnode = req->vnode;
420#endif
421	struct page *page = req->pages[req->index];
422
423	req->pages[req->index] = NULL;
424	SetPageUptodate(page);
425
426	/* send the page to the cache */
427#ifdef CONFIG_AFS_FSCACHE
428	if (PageFsCache(page) &&
429	    fscache_write_page(vnode->cache, page, vnode->status.size,
430			       GFP_KERNEL) != 0) {
431		fscache_uncache_page(vnode->cache, page);
432		BUG_ON(PageFsCache(page));
433	}
434#endif
435	unlock_page(page);
436	put_page(page);
437}
438
439/*
440 * Read a contiguous set of pages.
441 */
442static int afs_readpages_one(struct file *file, struct address_space *mapping,
443			     struct list_head *pages)
444{
445	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
446	struct afs_read *req;
447	struct list_head *p;
448	struct page *first, *page;
449	struct key *key = afs_file_key(file);
450	pgoff_t index;
451	int ret, n, i;
452
453	/* Count the number of contiguous pages at the front of the list.  Note
454	 * that the list goes prev-wards rather than next-wards.
455	 */
456	first = lru_to_page(pages);
457	index = first->index + 1;
458	n = 1;
459	for (p = first->lru.prev; p != pages; p = p->prev) {
460		page = list_entry(p, struct page, lru);
461		if (page->index != index)
462			break;
463		index++;
464		n++;
465	}
 
 
 
466
467	req = kzalloc(struct_size(req, array, n), GFP_NOFS);
468	if (!req)
469		return -ENOMEM;
470
471	refcount_set(&req->usage, 1);
472	req->vnode = vnode;
473	req->page_done = afs_readpages_page_done;
474	req->pos = first->index;
475	req->pos <<= PAGE_SHIFT;
476	req->pages = req->array;
477
478	/* Transfer the pages to the request.  We add them in until one fails
479	 * to add to the LRU and then we stop (as that'll make a hole in the
480	 * contiguous run.
481	 *
482	 * Note that it's possible for the file size to change whilst we're
483	 * doing this, but we rely on the server returning less than we asked
484	 * for if the file shrank.  We also rely on this to deal with a partial
485	 * page at the end of the file.
486	 */
487	do {
488		page = lru_to_page(pages);
489		list_del(&page->lru);
490		index = page->index;
491		if (add_to_page_cache_lru(page, mapping, index,
492					  readahead_gfp_mask(mapping))) {
493#ifdef CONFIG_AFS_FSCACHE
494			fscache_uncache_page(vnode->cache, page);
495#endif
496			put_page(page);
497			break;
498		}
499
500		req->pages[req->nr_pages++] = page;
501		req->len += PAGE_SIZE;
502	} while (req->nr_pages < n);
503
504	if (req->nr_pages == 0) {
505		kfree(req);
506		return 0;
507	}
 
 
 
 
 
508
509	ret = afs_fetch_data(vnode, key, req);
510	if (ret < 0)
511		goto error;
 
512
513	task_io_account_read(PAGE_SIZE * req->nr_pages);
514	afs_put_read(req);
515	return 0;
516
517error:
518	if (ret == -ENOENT) {
519		_debug("got NOENT from server"
520		       " - marking file deleted and stale");
521		set_bit(AFS_VNODE_DELETED, &vnode->flags);
522		ret = -ESTALE;
523	}
524
525	for (i = 0; i < req->nr_pages; i++) {
526		page = req->pages[i];
527		if (page) {
528#ifdef CONFIG_AFS_FSCACHE
529			fscache_uncache_page(vnode->cache, page);
530#endif
531			SetPageError(page);
532			unlock_page(page);
533		}
534	}
535
536	afs_put_read(req);
537	return ret;
538}
539
540/*
541 * read a set of pages
542 */
543static int afs_readpages(struct file *file, struct address_space *mapping,
544			 struct list_head *pages, unsigned nr_pages)
545{
546	struct key *key = afs_file_key(file);
547	struct afs_vnode *vnode;
548	int ret = 0;
549
550	_enter("{%d},{%lu},,%d",
551	       key_serial(key), mapping->host->i_ino, nr_pages);
552
553	ASSERT(key != NULL);
 
 
 
 
 
554
555	vnode = AFS_FS_I(mapping->host);
556	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
557		_leave(" = -ESTALE");
558		return -ESTALE;
559	}
560
561	/* attempt to read as many of the pages as possible */
562#ifdef CONFIG_AFS_FSCACHE
563	ret = fscache_read_or_alloc_pages(vnode->cache,
564					  mapping,
565					  pages,
566					  &nr_pages,
567					  afs_file_readpage_read_complete,
568					  NULL,
569					  mapping_gfp_mask(mapping));
570#else
571	ret = -ENOBUFS;
572#endif
573
574	switch (ret) {
575		/* all pages are being read from the cache */
576	case 0:
577		BUG_ON(!list_empty(pages));
578		BUG_ON(nr_pages != 0);
579		_leave(" = 0 [reading all]");
580		return 0;
581
582		/* there were pages that couldn't be read from the cache */
583	case -ENODATA:
584	case -ENOBUFS:
585		break;
586
587		/* other error */
588	default:
589		_leave(" = %d", ret);
590		return ret;
591	}
592
593	while (!list_empty(pages)) {
594		ret = afs_readpages_one(file, mapping, pages);
595		if (ret < 0)
596			break;
597	}
 
 
 
 
598
599	_leave(" = %d [netting]", ret);
 
 
 
 
600	return ret;
601}
602
603/*
604 * invalidate part or all of a page
605 * - release a page and clean up its private data if offset is 0 (indicating
606 *   the entire page)
607 */
608static void afs_invalidatepage(struct page *page, unsigned int offset,
609			       unsigned int length)
610{
611	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
612	unsigned long priv;
613
614	_enter("{%lu},%u,%u", page->index, offset, length);
 
 
 
615
616	BUG_ON(!PageLocked(page));
617
618	/* we clean up only if the entire page is being invalidated */
619	if (offset == 0 && length == PAGE_SIZE) {
620#ifdef CONFIG_AFS_FSCACHE
621		if (PageFsCache(page)) {
622			struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
623			fscache_wait_on_page_write(vnode->cache, page);
624			fscache_uncache_page(vnode->cache, page);
625		}
626#endif
627
628		if (PagePrivate(page)) {
629			priv = page_private(page);
630			trace_afs_page_dirty(vnode, tracepoint_string("inval"),
631					     page->index, priv);
632			set_page_private(page, 0);
633			ClearPagePrivate(page);
634		}
635	}
636
637	_leave("");
 
 
638}
639
640/*
641 * release a page and clean up its private state if it's not busy
642 * - return true if the page can now be released, false if not
643 */
644static int afs_releasepage(struct page *page, gfp_t gfp_flags)
645{
646	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
647	unsigned long priv;
 
 
 
 
 
648
649	_enter("{{%llx:%llu}[%lu],%lx},%x",
650	       vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
651	       gfp_flags);
652
653	/* deny if page is being written to the cache and the caller hasn't
654	 * elected to wait */
655#ifdef CONFIG_AFS_FSCACHE
656	if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
657		_leave(" = F [cache busy]");
658		return 0;
659	}
660#endif
661
662	if (PagePrivate(page)) {
663		priv = page_private(page);
664		trace_afs_page_dirty(vnode, tracepoint_string("rel"),
665				     page->index, priv);
666		set_page_private(page, 0);
667		ClearPagePrivate(page);
668	}
669
670	/* indicate that the page can be released */
671	_leave(" = T");
672	return 1;
673}
674
675/*
676 * Handle setting up a memory mapping on an AFS file.
677 */
678static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
679{
680	int ret;
 
 
 
681
682	ret = generic_file_mmap(file, vma);
 
 
 
683	if (ret == 0)
684		vma->vm_ops = &afs_vm_ops;
 
685	return ret;
686}
v6.8
  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 "internal.h"
 20
 21static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
 22static int afs_symlink_read_folio(struct file *file, struct folio *folio);
 
 
 
 23
 24static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
 25static 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);
 28static void afs_vm_open(struct vm_area_struct *area);
 29static void afs_vm_close(struct vm_area_struct *area);
 30static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
 31
 32const struct file_operations afs_file_operations = {
 33	.open		= afs_open,
 34	.release	= afs_release,
 35	.llseek		= generic_file_llseek,
 36	.read_iter	= afs_file_read_iter,
 37	.write_iter	= netfs_file_write_iter,
 38	.mmap		= afs_file_mmap,
 39	.splice_read	= afs_file_splice_read,
 40	.splice_write	= iter_file_splice_write,
 41	.fsync		= afs_fsync,
 42	.lock		= afs_lock,
 43	.flock		= afs_flock,
 44};
 45
 46const struct inode_operations afs_file_inode_operations = {
 47	.getattr	= afs_getattr,
 48	.setattr	= afs_setattr,
 49	.permission	= afs_permission,
 
 50};
 51
 52const struct address_space_operations afs_file_aops = {
 53	.direct_IO	= noop_direct_IO,
 54	.read_folio	= netfs_read_folio,
 55	.readahead	= netfs_readahead,
 56	.dirty_folio	= netfs_dirty_folio,
 57	.launder_folio	= netfs_launder_folio,
 58	.release_folio	= netfs_release_folio,
 59	.invalidate_folio = netfs_invalidate_folio,
 60	.migrate_folio	= filemap_migrate_folio,
 
 61	.writepages	= afs_writepages,
 62};
 63
 64const struct address_space_operations afs_symlink_aops = {
 65	.read_folio	= afs_symlink_read_folio,
 66	.release_folio	= netfs_release_folio,
 67	.invalidate_folio = netfs_invalidate_folio,
 68	.migrate_folio	= filemap_migrate_folio,
 69};
 70
 71static const struct vm_operations_struct afs_vm_ops = {
 72	.open		= afs_vm_open,
 73	.close		= afs_vm_close,
 74	.fault		= filemap_fault,
 75	.map_pages	= afs_vm_map_pages,
 76	.page_mkwrite	= afs_page_mkwrite,
 77};
 78
 79/*
 80 * Discard a pin on a writeback key.
 81 */
 82void afs_put_wb_key(struct afs_wb_key *wbk)
 83{
 84	if (wbk && refcount_dec_and_test(&wbk->usage)) {
 85		key_put(wbk->key);
 86		kfree(wbk);
 87	}
 88}
 89
 90/*
 91 * Cache key for writeback.
 92 */
 93int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
 94{
 95	struct afs_wb_key *wbk, *p;
 96
 97	wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
 98	if (!wbk)
 99		return -ENOMEM;
100	refcount_set(&wbk->usage, 2);
101	wbk->key = af->key;
102
103	spin_lock(&vnode->wb_lock);
104	list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
105		if (p->key == wbk->key)
106			goto found;
107	}
108
109	key_get(wbk->key);
110	list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
111	spin_unlock(&vnode->wb_lock);
112	af->wb = wbk;
113	return 0;
114
115found:
116	refcount_inc(&p->usage);
117	spin_unlock(&vnode->wb_lock);
118	af->wb = p;
119	kfree(wbk);
120	return 0;
121}
122
123/*
124 * open an AFS file or directory and attach a key to it
125 */
126int afs_open(struct inode *inode, struct file *file)
127{
128	struct afs_vnode *vnode = AFS_FS_I(inode);
129	struct afs_file *af;
130	struct key *key;
131	int ret;
132
133	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
134
135	key = afs_request_key(vnode->volume->cell);
136	if (IS_ERR(key)) {
137		ret = PTR_ERR(key);
138		goto error;
139	}
140
141	af = kzalloc(sizeof(*af), GFP_KERNEL);
142	if (!af) {
143		ret = -ENOMEM;
144		goto error_key;
145	}
146	af->key = key;
147
148	ret = afs_validate(vnode, key);
149	if (ret < 0)
150		goto error_af;
151
152	if (file->f_mode & FMODE_WRITE) {
153		ret = afs_cache_wb_key(vnode, af);
154		if (ret < 0)
155			goto error_af;
156	}
157
158	if (file->f_flags & O_TRUNC)
159		set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
160
161	fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
162
163	file->private_data = af;
164	_leave(" = 0");
165	return 0;
166
167error_af:
168	kfree(af);
169error_key:
170	key_put(key);
171error:
172	_leave(" = %d", ret);
173	return ret;
174}
175
176/*
177 * release an AFS file or directory and discard its key
178 */
179int afs_release(struct inode *inode, struct file *file)
180{
181	struct afs_vnode_cache_aux aux;
182	struct afs_vnode *vnode = AFS_FS_I(inode);
183	struct afs_file *af = file->private_data;
184	loff_t i_size;
185	int ret = 0;
186
187	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
188
189	if ((file->f_mode & FMODE_WRITE))
190		ret = vfs_fsync(file, 0);
191
192	file->private_data = NULL;
193	if (af->wb)
194		afs_put_wb_key(af->wb);
195
196	if ((file->f_mode & FMODE_WRITE)) {
197		i_size = i_size_read(&vnode->netfs.inode);
198		afs_set_cache_aux(vnode, &aux);
199		fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
200	} else {
201		fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
202	}
203
204	key_put(af->key);
205	kfree(af);
206	afs_prune_wb_keys(vnode);
207	_leave(" = %d", ret);
208	return ret;
209}
210
211/*
212 * Allocate a new read record.
213 */
214struct afs_read *afs_alloc_read(gfp_t gfp)
215{
216	struct afs_read *req;
217
218	req = kzalloc(sizeof(struct afs_read), gfp);
219	if (req)
220		refcount_set(&req->usage, 1);
221
222	return req;
223}
224
225/*
226 * Dispose of a ref to a read record.
227 */
228void afs_put_read(struct afs_read *req)
229{
 
 
230	if (refcount_dec_and_test(&req->usage)) {
231		if (req->cleanup)
232			req->cleanup(req);
233		key_put(req->key);
 
 
 
 
234		kfree(req);
235	}
236}
237
238static void afs_fetch_data_notify(struct afs_operation *op)
239{
240	struct afs_read *req = op->fetch.req;
241	struct netfs_io_subrequest *subreq = req->subreq;
242	int error = afs_op_error(op);
243
244	req->error = error;
245	if (subreq) {
246		__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
247		netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
248		req->subreq = NULL;
249	} else if (req->done) {
250		req->done(req);
251	}
 
252}
 
253
254static void afs_fetch_data_success(struct afs_operation *op)
255{
256	struct afs_vnode *vnode = op->file[0].vnode;
257
258	_enter("op=%08x", op->debug_id);
259	afs_vnode_commit_status(op, &op->file[0]);
260	afs_stat_v(vnode, n_fetches);
261	atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
262	afs_fetch_data_notify(op);
263}
264
265static void afs_fetch_data_put(struct afs_operation *op)
266{
267	op->fetch.req->error = afs_op_error(op);
268	afs_put_read(op->fetch.req);
269}
270
271static const struct afs_operation_ops afs_fetch_data_operation = {
272	.issue_afs_rpc	= afs_fs_fetch_data,
273	.issue_yfs_rpc	= yfs_fs_fetch_data,
274	.success	= afs_fetch_data_success,
275	.aborted	= afs_check_for_remote_deletion,
276	.failed		= afs_fetch_data_notify,
277	.put		= afs_fetch_data_put,
278};
279
280/*
281 * Fetch file data from the volume.
282 */
283int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
284{
285	struct afs_operation *op;
286
287	_enter("%s{%llx:%llu.%u},%x,,,",
288	       vnode->volume->name,
289	       vnode->fid.vid,
290	       vnode->fid.vnode,
291	       vnode->fid.unique,
292	       key_serial(req->key));
293
294	op = afs_alloc_operation(req->key, vnode->volume);
295	if (IS_ERR(op)) {
296		if (req->subreq)
297			netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
298		return PTR_ERR(op);
299	}
300
301	afs_op_set_vnode(op, 0, vnode);
302
303	op->fetch.req	= afs_get_read(req);
304	op->ops		= &afs_fetch_data_operation;
305	return afs_do_sync_operation(op);
306}
307
308static void afs_issue_read(struct netfs_io_subrequest *subreq)
 
 
 
309{
310	struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
311	struct afs_read *fsreq;
 
 
 
312
313	fsreq = afs_alloc_read(GFP_NOFS);
314	if (!fsreq)
315		return netfs_subreq_terminated(subreq, -ENOMEM, false);
316
317	fsreq->subreq	= subreq;
318	fsreq->pos	= subreq->start + subreq->transferred;
319	fsreq->len	= subreq->len   - subreq->transferred;
320	fsreq->key	= key_get(subreq->rreq->netfs_priv);
321	fsreq->vnode	= vnode;
322	fsreq->iter	= &subreq->io_iter;
323
324	afs_fetch_data(fsreq->vnode, fsreq);
325	afs_put_read(fsreq);
326}
327
328static int afs_symlink_read_folio(struct file *file, struct folio *folio)
329{
330	struct afs_vnode *vnode = AFS_FS_I(folio->mapping->host);
331	struct afs_read *fsreq;
332	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
334	fsreq = afs_alloc_read(GFP_NOFS);
335	if (!fsreq)
336		return -ENOMEM;
337
338	fsreq->pos	= folio_pos(folio);
339	fsreq->len	= folio_size(folio);
340	fsreq->vnode	= vnode;
341	fsreq->iter	= &fsreq->def_iter;
342	iov_iter_xarray(&fsreq->def_iter, ITER_DEST, &folio->mapping->i_pages,
343			fsreq->pos, fsreq->len);
344
345	ret = afs_fetch_data(fsreq->vnode, fsreq);
346	if (ret == 0)
347		folio_mark_uptodate(folio);
348	folio_unlock(folio);
349	return ret;
350}
351
352static int afs_init_request(struct netfs_io_request *rreq, struct file *file)
 
 
 
 
353{
354	if (file)
355		rreq->netfs_priv = key_get(afs_file_key(file));
356	rreq->rsize = 256 * 1024;
357	rreq->wsize = 256 * 1024;
358	return 0;
359}
360
361static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
362				 struct folio **foliop, void **_fsdata)
363{
364	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
365
366	return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
 
 
 
 
 
 
 
 
 
367}
368
369static void afs_free_request(struct netfs_io_request *rreq)
 
 
 
370{
371	key_put(rreq->netfs_priv);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372}
373
374static void afs_update_i_size(struct inode *inode, loff_t new_i_size)
 
 
 
 
375{
376	struct afs_vnode *vnode = AFS_FS_I(inode);
377	loff_t i_size;
378
379	write_seqlock(&vnode->cb_lock);
380	i_size = i_size_read(&vnode->netfs.inode);
381	if (new_i_size > i_size) {
382		i_size_write(&vnode->netfs.inode, new_i_size);
383		inode_set_bytes(&vnode->netfs.inode, new_i_size);
 
 
 
 
 
 
 
 
 
 
 
 
384	}
385	write_sequnlock(&vnode->cb_lock);
386	fscache_update_cookie(afs_vnode_cache(vnode), NULL, &new_i_size);
387}
388
389static void afs_netfs_invalidate_cache(struct netfs_io_request *wreq)
390{
391	struct afs_vnode *vnode = AFS_FS_I(wreq->inode);
392
393	afs_invalidate_cache(vnode, 0);
394}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
396const struct netfs_request_ops afs_req_ops = {
397	.init_request		= afs_init_request,
398	.free_request		= afs_free_request,
399	.check_write_begin	= afs_check_write_begin,
400	.issue_read		= afs_issue_read,
401	.update_i_size		= afs_update_i_size,
402	.invalidate_cache	= afs_netfs_invalidate_cache,
403	.create_write_requests	= afs_create_write_requests,
404};
405
406static void afs_add_open_mmap(struct afs_vnode *vnode)
407{
408	if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
409		down_write(&vnode->volume->open_mmaps_lock);
410
411		if (list_empty(&vnode->cb_mmap_link))
412			list_add_tail(&vnode->cb_mmap_link, &vnode->volume->open_mmaps);
 
413
414		up_write(&vnode->volume->open_mmaps_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415	}
 
 
 
416}
417
418static void afs_drop_open_mmap(struct afs_vnode *vnode)
 
 
 
 
419{
420	if (atomic_add_unless(&vnode->cb_nr_mmap, -1, 1))
421		return;
 
422
423	down_write(&vnode->volume->open_mmaps_lock);
 
424
425	read_seqlock_excl(&vnode->cb_lock);
426	// the only place where ->cb_nr_mmap may hit 0
427	// see __afs_break_callback() for the other side...
428	if (atomic_dec_and_test(&vnode->cb_nr_mmap))
429		list_del_init(&vnode->cb_mmap_link);
430	read_sequnlock_excl(&vnode->cb_lock);
431
432	up_write(&vnode->volume->open_mmaps_lock);
433	flush_work(&vnode->cb_work);
434}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435
436/*
437 * Handle setting up a memory mapping on an AFS file.
438 */
439static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
440{
441	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
442	int ret;
443
444	afs_add_open_mmap(vnode);
445
446	ret = generic_file_mmap(file, vma);
447	if (ret == 0)
448		vma->vm_ops = &afs_vm_ops;
449	else
450		afs_drop_open_mmap(vnode);
451	return ret;
452}
453
454static void afs_vm_open(struct vm_area_struct *vma)
 
 
 
 
 
 
455{
456	afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
457}
458
459static void afs_vm_close(struct vm_area_struct *vma)
460{
461	afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
462}
463
464static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
465{
466	struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467
468	if (afs_check_validity(vnode))
469		return filemap_map_pages(vmf, start_pgoff, end_pgoff);
470	return 0;
471}
472
473static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 
 
 
 
474{
475	struct inode *inode = file_inode(iocb->ki_filp);
476	struct afs_vnode *vnode = AFS_FS_I(inode);
477	struct afs_file *af = iocb->ki_filp->private_data;
478	ssize_t ret;
479
480	if (iocb->ki_flags & IOCB_DIRECT)
481		return netfs_unbuffered_read_iter(iocb, iter);
482
483	ret = netfs_start_io_read(inode);
484	if (ret < 0)
485		return ret;
486	ret = afs_validate(vnode, af->key);
487	if (ret == 0)
488		ret = filemap_read(iocb, iter, 0);
489	netfs_end_io_read(inode);
490	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
491}
492
493static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,
494				    struct pipe_inode_info *pipe,
495				    size_t len, unsigned int flags)
 
496{
497	struct inode *inode = file_inode(in);
498	struct afs_vnode *vnode = AFS_FS_I(inode);
499	struct afs_file *af = in->private_data;
500	ssize_t ret;
501
502	ret = netfs_start_io_read(inode);
503	if (ret < 0)
504		return ret;
505	ret = afs_validate(vnode, af->key);
506	if (ret == 0)
507		ret = filemap_splice_read(in, ppos, pipe, len, flags);
508	netfs_end_io_read(inode);
509	return ret;
510}