1 /* AFS filesystem file handling
2 *
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/fs.h>
16 #include <linux/pagemap.h>
17 #include <linux/writeback.h>
18 #include <linux/gfp.h>
19 #include <linux/task_io_accounting_ops.h>
20 #include "internal.h"
21
22 static int afs_readpage(struct file *file, struct page *page);
23 static void afs_invalidatepage(struct page *page, unsigned int offset,
24 unsigned int length);
25 static int afs_releasepage(struct page *page, gfp_t gfp_flags);
26 static int afs_launder_page(struct page *page);
27
28 static int afs_readpages(struct file *filp, struct address_space *mapping,
29 struct list_head *pages, unsigned nr_pages);
30
31 const struct file_operations afs_file_operations = {
32 .open = afs_open,
33 .flush = afs_flush,
34 .release = afs_release,
35 .llseek = generic_file_llseek,
36 .read_iter = generic_file_read_iter,
37 .write_iter = afs_file_write,
38 .mmap = generic_file_readonly_mmap,
39 .splice_read = generic_file_splice_read,
40 .fsync = afs_fsync,
41 .lock = afs_lock,
42 .flock = afs_flock,
43 };
44
45 const struct inode_operations afs_file_inode_operations = {
46 .getattr = afs_getattr,
47 .setattr = afs_setattr,
48 .permission = afs_permission,
49 .listxattr = afs_listxattr,
50 };
51
52 const struct address_space_operations afs_fs_aops = {
53 .readpage = afs_readpage,
54 .readpages = afs_readpages,
55 .set_page_dirty = afs_set_page_dirty,
56 .launder_page = afs_launder_page,
57 .releasepage = afs_releasepage,
58 .invalidatepage = afs_invalidatepage,
59 .write_begin = afs_write_begin,
60 .write_end = afs_write_end,
61 .writepage = afs_writepage,
62 .writepages = afs_writepages,
63 };
64
65 /*
66 * open an AFS file or directory and attach a key to it
67 */
afs_open(struct inode * inode,struct file * file)68 int afs_open(struct inode *inode, struct file *file)
69 {
70 struct afs_vnode *vnode = AFS_FS_I(inode);
71 struct key *key;
72 int ret;
73
74 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
75
76 key = afs_request_key(vnode->volume->cell);
77 if (IS_ERR(key)) {
78 _leave(" = %ld [key]", PTR_ERR(key));
79 return PTR_ERR(key);
80 }
81
82 ret = afs_validate(vnode, key);
83 if (ret < 0) {
84 _leave(" = %d [val]", ret);
85 return ret;
86 }
87
88 file->private_data = key;
89 _leave(" = 0");
90 return 0;
91 }
92
93 /*
94 * release an AFS file or directory and discard its key
95 */
afs_release(struct inode * inode,struct file * file)96 int afs_release(struct inode *inode, struct file *file)
97 {
98 struct afs_vnode *vnode = AFS_FS_I(inode);
99
100 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
101
102 key_put(file->private_data);
103 _leave(" = 0");
104 return 0;
105 }
106
107 /*
108 * Dispose of a ref to a read record.
109 */
afs_put_read(struct afs_read * req)110 void afs_put_read(struct afs_read *req)
111 {
112 int i;
113
114 if (atomic_dec_and_test(&req->usage)) {
115 for (i = 0; i < req->nr_pages; i++)
116 if (req->pages[i])
117 put_page(req->pages[i]);
118 kfree(req);
119 }
120 }
121
122 #ifdef CONFIG_AFS_FSCACHE
123 /*
124 * deal with notification that a page was read from the cache
125 */
afs_file_readpage_read_complete(struct page * page,void * data,int error)126 static void afs_file_readpage_read_complete(struct page *page,
127 void *data,
128 int error)
129 {
130 _enter("%p,%p,%d", page, data, error);
131
132 /* if the read completes with an error, we just unlock the page and let
133 * the VM reissue the readpage */
134 if (!error)
135 SetPageUptodate(page);
136 unlock_page(page);
137 }
138 #endif
139
140 /*
141 * read page from file, directory or symlink, given a key to use
142 */
__afs_page_filler(struct key * key,struct page * page)143 static int __afs_page_filler(struct key *key, struct page *page)
144 {
145 struct inode *inode = page->mapping->host;
146 struct afs_vnode *vnode = AFS_FS_I(inode);
147 struct afs_read *req;
148 int ret;
149
150 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
151
152 BUG_ON(!PageLocked(page));
153
154 ret = -ESTALE;
155 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
156 goto error;
157
158 /* is it cached? */
159 #ifdef CONFIG_AFS_FSCACHE
160 ret = fscache_read_or_alloc_page(vnode->cache,
161 page,
162 afs_file_readpage_read_complete,
163 NULL,
164 GFP_KERNEL);
165 #else
166 ret = -ENOBUFS;
167 #endif
168 switch (ret) {
169 /* read BIO submitted (page in cache) */
170 case 0:
171 break;
172
173 /* page not yet cached */
174 case -ENODATA:
175 _debug("cache said ENODATA");
176 goto go_on;
177
178 /* page will not be cached */
179 case -ENOBUFS:
180 _debug("cache said ENOBUFS");
181 default:
182 go_on:
183 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
184 GFP_KERNEL);
185 if (!req)
186 goto enomem;
187
188 /* We request a full page. If the page is a partial one at the
189 * end of the file, the server will return a short read and the
190 * unmarshalling code will clear the unfilled space.
191 */
192 atomic_set(&req->usage, 1);
193 req->pos = (loff_t)page->index << PAGE_SHIFT;
194 req->len = PAGE_SIZE;
195 req->nr_pages = 1;
196 req->pages[0] = page;
197 get_page(page);
198
199 /* read the contents of the file from the server into the
200 * page */
201 ret = afs_vnode_fetch_data(vnode, key, req);
202 afs_put_read(req);
203 if (ret < 0) {
204 if (ret == -ENOENT) {
205 _debug("got NOENT from server"
206 " - marking file deleted and stale");
207 set_bit(AFS_VNODE_DELETED, &vnode->flags);
208 ret = -ESTALE;
209 }
210
211 #ifdef CONFIG_AFS_FSCACHE
212 fscache_uncache_page(vnode->cache, page);
213 #endif
214 BUG_ON(PageFsCache(page));
215
216 if (ret == -EINTR ||
217 ret == -ENOMEM ||
218 ret == -ERESTARTSYS ||
219 ret == -EAGAIN)
220 goto error;
221 goto io_error;
222 }
223
224 SetPageUptodate(page);
225
226 /* send the page to the cache */
227 #ifdef CONFIG_AFS_FSCACHE
228 if (PageFsCache(page) &&
229 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
230 fscache_uncache_page(vnode->cache, page);
231 BUG_ON(PageFsCache(page));
232 }
233 #endif
234 unlock_page(page);
235 }
236
237 _leave(" = 0");
238 return 0;
239
240 io_error:
241 SetPageError(page);
242 goto error;
243 enomem:
244 ret = -ENOMEM;
245 error:
246 unlock_page(page);
247 _leave(" = %d", ret);
248 return ret;
249 }
250
afs_page_filler(struct file * data,struct page * page)251 int afs_page_filler(struct file *data, struct page *page)
252 {
253 struct key *key = (struct key *)data;
254
255 return __afs_page_filler(key, page);
256 }
257
258 /*
259 * read page from file, directory or symlink, given a file to nominate the key
260 * to be used
261 */
afs_readpage(struct file * file,struct page * page)262 static int afs_readpage(struct file *file, struct page *page)
263 {
264 struct key *key;
265 int ret;
266
267 if (file) {
268 key = file->private_data;
269 ASSERT(key != NULL);
270 ret = __afs_page_filler(key, page);
271 } else {
272 struct inode *inode = page->mapping->host;
273 key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell);
274 if (IS_ERR(key)) {
275 ret = PTR_ERR(key);
276 } else {
277 ret = __afs_page_filler(key, page);
278 key_put(key);
279 }
280 }
281 return ret;
282 }
283
284 /*
285 * Make pages available as they're filled.
286 */
afs_readpages_page_done(struct afs_call * call,struct afs_read * req)287 static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req)
288 {
289 #ifdef CONFIG_AFS_FSCACHE
290 struct afs_vnode *vnode = call->reply;
291 #endif
292 struct page *page = req->pages[req->index];
293
294 req->pages[req->index] = NULL;
295 SetPageUptodate(page);
296
297 /* send the page to the cache */
298 #ifdef CONFIG_AFS_FSCACHE
299 if (PageFsCache(page) &&
300 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
301 fscache_uncache_page(vnode->cache, page);
302 BUG_ON(PageFsCache(page));
303 }
304 #endif
305 unlock_page(page);
306 put_page(page);
307 }
308
309 /*
310 * Read a contiguous set of pages.
311 */
afs_readpages_one(struct file * file,struct address_space * mapping,struct list_head * pages)312 static int afs_readpages_one(struct file *file, struct address_space *mapping,
313 struct list_head *pages)
314 {
315 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
316 struct afs_read *req;
317 struct list_head *p;
318 struct page *first, *page;
319 struct key *key = file->private_data;
320 pgoff_t index;
321 int ret, n, i;
322
323 /* Count the number of contiguous pages at the front of the list. Note
324 * that the list goes prev-wards rather than next-wards.
325 */
326 first = list_entry(pages->prev, struct page, lru);
327 index = first->index + 1;
328 n = 1;
329 for (p = first->lru.prev; p != pages; p = p->prev) {
330 page = list_entry(p, struct page, lru);
331 if (page->index != index)
332 break;
333 index++;
334 n++;
335 }
336
337 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
338 GFP_NOFS);
339 if (!req)
340 return -ENOMEM;
341
342 atomic_set(&req->usage, 1);
343 req->page_done = afs_readpages_page_done;
344 req->pos = first->index;
345 req->pos <<= PAGE_SHIFT;
346
347 /* Transfer the pages to the request. We add them in until one fails
348 * to add to the LRU and then we stop (as that'll make a hole in the
349 * contiguous run.
350 *
351 * Note that it's possible for the file size to change whilst we're
352 * doing this, but we rely on the server returning less than we asked
353 * for if the file shrank. We also rely on this to deal with a partial
354 * page at the end of the file.
355 */
356 do {
357 page = list_entry(pages->prev, struct page, lru);
358 list_del(&page->lru);
359 index = page->index;
360 if (add_to_page_cache_lru(page, mapping, index,
361 readahead_gfp_mask(mapping))) {
362 #ifdef CONFIG_AFS_FSCACHE
363 fscache_uncache_page(vnode->cache, page);
364 #endif
365 put_page(page);
366 break;
367 }
368
369 req->pages[req->nr_pages++] = page;
370 req->len += PAGE_SIZE;
371 } while (req->nr_pages < n);
372
373 if (req->nr_pages == 0) {
374 kfree(req);
375 return 0;
376 }
377
378 ret = afs_vnode_fetch_data(vnode, key, req);
379 if (ret < 0)
380 goto error;
381
382 task_io_account_read(PAGE_SIZE * req->nr_pages);
383 afs_put_read(req);
384 return 0;
385
386 error:
387 if (ret == -ENOENT) {
388 _debug("got NOENT from server"
389 " - marking file deleted and stale");
390 set_bit(AFS_VNODE_DELETED, &vnode->flags);
391 ret = -ESTALE;
392 }
393
394 for (i = 0; i < req->nr_pages; i++) {
395 page = req->pages[i];
396 if (page) {
397 #ifdef CONFIG_AFS_FSCACHE
398 fscache_uncache_page(vnode->cache, page);
399 #endif
400 SetPageError(page);
401 unlock_page(page);
402 }
403 }
404
405 afs_put_read(req);
406 return ret;
407 }
408
409 /*
410 * read a set of pages
411 */
afs_readpages(struct file * file,struct address_space * mapping,struct list_head * pages,unsigned nr_pages)412 static int afs_readpages(struct file *file, struct address_space *mapping,
413 struct list_head *pages, unsigned nr_pages)
414 {
415 struct key *key = file->private_data;
416 struct afs_vnode *vnode;
417 int ret = 0;
418
419 _enter("{%d},{%lu},,%d",
420 key_serial(key), mapping->host->i_ino, nr_pages);
421
422 ASSERT(key != NULL);
423
424 vnode = AFS_FS_I(mapping->host);
425 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
426 _leave(" = -ESTALE");
427 return -ESTALE;
428 }
429
430 /* attempt to read as many of the pages as possible */
431 #ifdef CONFIG_AFS_FSCACHE
432 ret = fscache_read_or_alloc_pages(vnode->cache,
433 mapping,
434 pages,
435 &nr_pages,
436 afs_file_readpage_read_complete,
437 NULL,
438 mapping_gfp_mask(mapping));
439 #else
440 ret = -ENOBUFS;
441 #endif
442
443 switch (ret) {
444 /* all pages are being read from the cache */
445 case 0:
446 BUG_ON(!list_empty(pages));
447 BUG_ON(nr_pages != 0);
448 _leave(" = 0 [reading all]");
449 return 0;
450
451 /* there were pages that couldn't be read from the cache */
452 case -ENODATA:
453 case -ENOBUFS:
454 break;
455
456 /* other error */
457 default:
458 _leave(" = %d", ret);
459 return ret;
460 }
461
462 while (!list_empty(pages)) {
463 ret = afs_readpages_one(file, mapping, pages);
464 if (ret < 0)
465 break;
466 }
467
468 _leave(" = %d [netting]", ret);
469 return ret;
470 }
471
472 /*
473 * write back a dirty page
474 */
afs_launder_page(struct page * page)475 static int afs_launder_page(struct page *page)
476 {
477 _enter("{%lu}", page->index);
478
479 return 0;
480 }
481
482 /*
483 * invalidate part or all of a page
484 * - release a page and clean up its private data if offset is 0 (indicating
485 * the entire page)
486 */
afs_invalidatepage(struct page * page,unsigned int offset,unsigned int length)487 static void afs_invalidatepage(struct page *page, unsigned int offset,
488 unsigned int length)
489 {
490 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
491
492 _enter("{%lu},%u,%u", page->index, offset, length);
493
494 BUG_ON(!PageLocked(page));
495
496 /* we clean up only if the entire page is being invalidated */
497 if (offset == 0 && length == PAGE_SIZE) {
498 #ifdef CONFIG_AFS_FSCACHE
499 if (PageFsCache(page)) {
500 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
501 fscache_wait_on_page_write(vnode->cache, page);
502 fscache_uncache_page(vnode->cache, page);
503 }
504 #endif
505
506 if (PagePrivate(page)) {
507 if (wb && !PageWriteback(page)) {
508 set_page_private(page, 0);
509 afs_put_writeback(wb);
510 }
511
512 if (!page_private(page))
513 ClearPagePrivate(page);
514 }
515 }
516
517 _leave("");
518 }
519
520 /*
521 * release a page and clean up its private state if it's not busy
522 * - return true if the page can now be released, false if not
523 */
afs_releasepage(struct page * page,gfp_t gfp_flags)524 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
525 {
526 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
527 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
528
529 _enter("{{%x:%u}[%lu],%lx},%x",
530 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
531 gfp_flags);
532
533 /* deny if page is being written to the cache and the caller hasn't
534 * elected to wait */
535 #ifdef CONFIG_AFS_FSCACHE
536 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
537 _leave(" = F [cache busy]");
538 return 0;
539 }
540 #endif
541
542 if (PagePrivate(page)) {
543 if (wb) {
544 set_page_private(page, 0);
545 afs_put_writeback(wb);
546 }
547 ClearPagePrivate(page);
548 }
549
550 /* indicate that the page can be released */
551 _leave(" = T");
552 return 1;
553 }
554