1 /* AFS filesystem file handling
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
17 #include <linux/pagemap.h>
20 static int afs_readpage(struct file
*file
, struct page
*page
);
21 static void afs_invalidatepage(struct page
*page
, unsigned long offset
);
22 static int afs_releasepage(struct page
*page
, gfp_t gfp_flags
);
24 const struct file_operations afs_file_operations
= {
26 .release
= afs_release
,
27 .llseek
= generic_file_llseek
,
29 .aio_read
= generic_file_aio_read
,
30 .mmap
= generic_file_readonly_mmap
,
31 .sendfile
= generic_file_sendfile
,
34 const struct inode_operations afs_file_inode_operations
= {
35 .getattr
= afs_getattr
,
36 .permission
= afs_permission
,
39 const struct address_space_operations afs_fs_aops
= {
40 .readpage
= afs_readpage
,
41 .set_page_dirty
= __set_page_dirty_nobuffers
,
42 .releasepage
= afs_releasepage
,
43 .invalidatepage
= afs_invalidatepage
,
47 * open an AFS file or directory and attach a key to it
49 int afs_open(struct inode
*inode
, struct file
*file
)
51 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
55 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
57 key
= afs_request_key(vnode
->volume
->cell
);
59 _leave(" = %ld [key]", PTR_ERR(key
));
63 ret
= afs_validate(vnode
, key
);
65 _leave(" = %d [val]", ret
);
69 file
->private_data
= key
;
75 * release an AFS file or directory and discard its key
77 int afs_release(struct inode
*inode
, struct file
*file
)
79 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
81 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
83 key_put(file
->private_data
);
89 * deal with notification that a page was read from the cache
91 #ifdef AFS_CACHING_SUPPORT
92 static void afs_readpage_read_complete(void *cookie_data
,
97 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
102 SetPageUptodate(page
);
109 * deal with notification that a page was written to the cache
111 #ifdef AFS_CACHING_SUPPORT
112 static void afs_readpage_write_complete(void *cookie_data
,
117 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
124 * AFS read page from file, directory or symlink
126 static int afs_readpage(struct file
*file
, struct page
*page
)
128 struct afs_vnode
*vnode
;
135 inode
= page
->mapping
->host
;
137 ASSERT(file
!= NULL
);
138 key
= file
->private_data
;
141 _enter("{%x},{%lu},{%lu}", key_serial(key
), inode
->i_ino
, page
->index
);
143 vnode
= AFS_FS_I(inode
);
145 BUG_ON(!PageLocked(page
));
148 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
151 #ifdef AFS_CACHING_SUPPORT
153 ret
= cachefs_read_or_alloc_page(vnode
->cache
,
155 afs_file_readpage_read_complete
,
163 /* read BIO submitted and wb-journal entry found */
165 BUG(); // TODO - handle wb-journal match
167 /* read BIO submitted (page in cache) */
171 /* no page available in cache */
175 offset
= page
->index
<< PAGE_CACHE_SHIFT
;
176 len
= min_t(size_t, i_size_read(inode
) - offset
, PAGE_SIZE
);
178 /* read the contents of the file from the server into the
180 ret
= afs_vnode_fetch_data(vnode
, key
, offset
, len
, page
);
182 if (ret
== -ENOENT
) {
183 _debug("got NOENT from server"
184 " - marking file deleted and stale");
185 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
188 #ifdef AFS_CACHING_SUPPORT
189 cachefs_uncache_page(vnode
->cache
, page
);
194 SetPageUptodate(page
);
196 #ifdef AFS_CACHING_SUPPORT
197 if (cachefs_write_page(vnode
->cache
,
199 afs_file_readpage_write_complete
,
203 cachefs_uncache_page(vnode
->cache
, page
);
217 _leave(" = %d", ret
);
222 * invalidate part or all of a page
224 static void afs_invalidatepage(struct page
*page
, unsigned long offset
)
228 kenter("{%lu},%lu", page
->index
, offset
);
230 BUG_ON(!PageLocked(page
));
232 if (PagePrivate(page
)) {
233 #ifdef AFS_CACHING_SUPPORT
234 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
235 cachefs_uncache_page(vnode
->cache
,page
);
238 /* We release buffers only if the entire page is being
240 * The get_block cached value has been unconditionally
241 * invalidated, so real IO is not possible anymore.
244 BUG_ON(!PageLocked(page
));
247 if (!PageWriteback(page
))
248 ret
= page
->mapping
->a_ops
->releasepage(page
,
250 /* possibly should BUG_ON(!ret); - neilb */
254 _leave(" = %d", ret
);
258 * release a page and cleanup its private data
260 static int afs_releasepage(struct page
*page
, gfp_t gfp_flags
)
262 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
264 _enter("{{%x:%u}[%lu],%lx},%x",
265 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
, page
->flags
,
268 if (PagePrivate(page
)) {
269 set_page_private(page
, 0);
270 ClearPagePrivate(page
);