2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Linux specific vnodeops. Also includes the glue routines required to call
14 * So far the only truly scary part is that Linux relies on the inode cache
15 * to be up to date. Don't you dare break a callback and expect an fstat
16 * to give you meaningful information. This appears to be fixed in the 2.1
17 * development kernels. As it is we can fix this now by intercepting the
21 #include <afsconfig.h>
22 #include "afs/param.h"
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"
29 #include <linux/buffer_head.h>
30 #ifdef HAVE_MM_INLINE_H
31 #include <linux/mm_inline.h>
33 #include <linux/pagemap.h>
34 #include <linux/writeback.h>
35 #if defined(HAVE_LINUX_FOLIO_ADD_LRU) || defined(HAVE_LINUX_LRU_CACHE_ADD_FILE)
36 # include <linux/swap.h>
38 # include <linux/pagevec.h>
40 #include <linux/aio.h>
42 #include "afs/afs_bypasscache.h"
44 #include "osi_compat.h"
45 #include "osi_pagecopy.h"
48 #define MAX_ERRNO 1000L
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
52 /* Enable our workaround for a race with d_splice_alias. The race was fixed in
53 * 2.6.34, so don't do it after that point. */
54 # define D_SPLICE_ALIAS_RACE
57 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE_SHARED)
58 # define USE_FOP_ITERATE 1
59 #elif defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE) && !defined(FMODE_KABI_ITERATE)
60 /* Workaround for RH 7.5 which introduced file operation iterate() but requires
61 * each file->f_mode to be marked with FMODE_KABI_ITERATE. Instead OpenAFS will
62 * continue to use file opearation readdir() in this case.
64 # define USE_FOP_ITERATE 1
66 # undef USE_FOP_ITERATE
69 /* Kernels from before 2.6.19 may not be able to return errors from
71 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
72 # define ERRORS_FROM_D_REVALIDATE
75 int cachefs_noreadpage = 0;
77 extern struct backing_dev_info *afs_backing_dev_info;
79 extern struct vcache *afs_globalVp;
81 /* Handle interfacing with Linux's pagevec/lru facilities */
83 #if defined(HAVE_LINUX_FOLIO_ADD_LRU) || \
84 defined(HAVE_LINUX_LRU_CACHE_ADD_FILE) || defined(HAVE_LINUX_LRU_CACHE_ADD)
87 * Linux's lru_cache_add_file provides a simplified LRU interface without
90 struct afs_lru_pages {
95 afs_lru_cache_init(struct afs_lru_pages *alrupages)
101 afs_lru_cache_add(struct afs_lru_pages *alrupages, struct page *page)
103 # if defined(HAVE_LINUX_FOLIO_ADD_LRU)
104 struct folio *folio = page_folio(page);
105 folio_add_lru(folio);
106 # elif defined(HAVE_LINUX_LRU_CACHE_ADD)
108 # elif defined(HAVE_LINUX_LRU_CACHE_ADD_FILE)
109 lru_cache_add_file(page);
111 # error need a kernel function to add a page to the kernel lru cache
116 afs_lru_cache_finalize(struct afs_lru_pages *alrupages)
122 /* Linux's pagevec/lru interfaces require a pagevec */
123 struct afs_lru_pages {
124 struct pagevec lrupv;
128 afs_lru_cache_init(struct afs_lru_pages *alrupages)
130 # if defined(PAGEVEC_INIT_COLD_ARG)
131 pagevec_init(&alrupages->lrupv, 0);
133 pagevec_init(&alrupages->lrupv);
137 # ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
138 # define __pagevec_lru_add_file __pagevec_lru_add
142 afs_lru_cache_add(struct afs_lru_pages *alrupages, struct page *page)
145 if (!pagevec_add(&alrupages->lrupv, page))
146 __pagevec_lru_add_file(&alrupages->lrupv);
150 afs_lru_cache_finalize(struct afs_lru_pages *alrupages)
152 if (pagevec_count(&alrupages->lrupv))
153 __pagevec_lru_add_file(&alrupages->lrupv);
155 #endif /* !HAVE_LINUX_LRU_ADD_FILE */
158 afs_add_to_page_cache_lru(struct afs_lru_pages *alrupages, struct page *page,
159 struct address_space *mapping,
160 pgoff_t index, gfp_t gfp)
162 #if defined(HAVE_LINUX_ADD_TO_PAGE_CACHE_LRU)
163 return add_to_page_cache_lru(page, mapping, index, gfp);
166 code = add_to_page_cache(page, mapping, index, gfp);
168 afs_lru_cache_add(alrupages, page);
174 /* This function converts a positive error code from AFS into a negative
175 * code suitable for passing into the Linux VFS layer. It checks that the
176 * error code is within the permissable bounds for the ERR_PTR mechanism.
178 * _All_ error codes which come from the AFS layer should be passed through
179 * this function before being returned to the kernel.
183 afs_convert_code(int code) {
184 if ((code >= 0) && (code <= MAX_ERRNO))
190 /* Linux doesn't require a credp for many functions, and crref is an expensive
191 * operation. This helper function avoids obtaining it for VerifyVCache calls
195 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
196 cred_t *credp = NULL;
197 struct vrequest *treq = NULL;
200 if (avc->f.states & CStatd) {
208 code = afs_CreateReq(&treq, credp);
210 code = afs_VerifyVCache(avc, treq);
211 afs_DestroyReq(treq);
219 return afs_convert_code(code);
222 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
223 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
225 afs_linux_read_iter(struct kiocb *iocb, struct iov_iter *iter)
226 # elif defined(LINUX_HAS_NONVECTOR_AIO)
228 afs_linux_aio_read(struct kiocb *iocb, char __user *buf, size_t bufsize,
232 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *buf,
233 unsigned long bufsize, loff_t pos)
236 struct file *fp = iocb->ki_filp;
238 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
239 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
240 loff_t pos = iocb->ki_pos;
241 unsigned long bufsize = iter->nr_segs;
246 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
247 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
248 (afs_int32)bufsize, ICL_TYPE_INT32, 99999);
249 code = afs_linux_VerifyVCache(vcp, NULL);
252 /* Linux's FlushPages implementation doesn't ever use credp,
253 * so we optimise by not using it */
254 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
256 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
257 code = generic_file_read_iter(iocb, iter);
259 code = generic_file_aio_read(iocb, buf, bufsize, pos);
264 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
265 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
266 (afs_int32)bufsize, ICL_TYPE_INT32, code);
272 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
275 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
278 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
279 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
281 code = afs_linux_VerifyVCache(vcp, NULL);
284 /* Linux's FlushPages implementation doesn't ever use credp,
285 * so we optimise by not using it */
286 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
288 code = do_sync_read(fp, buf, count, offp);
292 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
293 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
301 /* Now we have integrated VM for writes as well as reads. the generic write operations
302 * also take care of re-positioning the pointer if file is open in append
303 * mode. Call fake open/close to ensure we do writes of core dumps.
305 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
306 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
308 afs_linux_write_iter(struct kiocb *iocb, struct iov_iter *iter)
309 # elif defined(LINUX_HAS_NONVECTOR_AIO)
311 afs_linux_aio_write(struct kiocb *iocb, const char __user *buf, size_t bufsize,
315 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *buf,
316 unsigned long bufsize, loff_t pos)
320 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
322 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
323 loff_t pos = iocb->ki_pos;
324 unsigned long bufsize = iter->nr_segs;
329 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
330 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
331 (afs_int32)bufsize, ICL_TYPE_INT32,
332 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
334 code = afs_linux_VerifyVCache(vcp, &credp);
336 ObtainWriteLock(&vcp->lock, 529);
338 ReleaseWriteLock(&vcp->lock);
341 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
342 code = generic_file_write_iter(iocb, iter);
344 code = generic_file_aio_write(iocb, buf, bufsize, pos);
349 ObtainWriteLock(&vcp->lock, 530);
351 if (vcp->execsOrWriters == 1 && !credp)
354 afs_FakeClose(vcp, credp);
355 ReleaseWriteLock(&vcp->lock);
357 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
358 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
359 (afs_int32)bufsize, ICL_TYPE_INT32, code);
368 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
371 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
376 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
377 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
378 (fp->f_flags & O_APPEND) ? 99998 : 99999);
380 code = afs_linux_VerifyVCache(vcp, &credp);
382 ObtainWriteLock(&vcp->lock, 529);
384 ReleaseWriteLock(&vcp->lock);
387 code = do_sync_write(fp, buf, count, offp);
391 ObtainWriteLock(&vcp->lock, 530);
393 if (vcp->execsOrWriters == 1 && !credp)
396 afs_FakeClose(vcp, credp);
397 ReleaseWriteLock(&vcp->lock);
399 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
400 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
410 /* This is a complete rewrite of afs_readdir, since we can make use of
411 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
412 * handling and use of bulkstats will need to be reflected here as well.
415 #if defined(USE_FOP_ITERATE)
416 afs_linux_readdir(struct file *fp, struct dir_context *ctx)
418 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
421 struct vcache *avc = VTOAFS(FILE_INODE(fp));
422 struct vrequest *treq = NULL;
427 struct DirEntryFlex *de;
428 struct DirBuffer entry;
431 afs_size_t origOffset, tlen;
432 cred_t *credp = crref();
433 struct afs_fakestat_state fakestat;
436 AFS_STATCNT(afs_readdir);
438 code = afs_convert_code(afs_CreateReq(&treq, credp));
443 afs_InitFakeStat(&fakestat);
444 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, treq));
448 /* update the cache entry */
450 code = afs_convert_code(afs_VerifyVCache(avc, treq));
454 /* get a reference to the entire directory */
455 tdc = afs_GetDCache(avc, (afs_size_t) 0, treq, &origOffset, &tlen, 1);
461 ObtainWriteLock(&avc->lock, 811);
462 ObtainReadLock(&tdc->lock);
464 * Make sure that the data in the cache is current. There are two
465 * cases we need to worry about:
466 * 1. The cache data is being fetched by another process.
467 * 2. The cache data is no longer valid
469 while ((avc->f.states & CStatd)
470 && (tdc->dflags & DFFetching)
471 && afs_IsDCacheFresh(tdc, avc)) {
472 ReleaseReadLock(&tdc->lock);
473 ReleaseWriteLock(&avc->lock);
474 afs_osi_Sleep(&tdc->validPos);
475 ObtainWriteLock(&avc->lock, 812);
476 ObtainReadLock(&tdc->lock);
478 if (!(avc->f.states & CStatd)
479 || !afs_IsDCacheFresh(tdc, avc)) {
480 ReleaseReadLock(&tdc->lock);
481 ReleaseWriteLock(&avc->lock);
486 /* Set the readdir-in-progress flag, and downgrade the lock
487 * to shared so others will be able to acquire a read lock.
489 avc->f.states |= CReadDir;
490 avc->dcreaddir = tdc;
491 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
492 ConvertWToSLock(&avc->lock);
494 /* Fill in until we get an error or we're done. This implementation
495 * takes an offset in units of blobs, rather than bytes.
498 #if defined(USE_FOP_ITERATE)
501 offset = (int) fp->f_pos;
505 code = BlobScan(tdc, offset, &dirpos);
506 if (code == 0 && dirpos == 0) {
507 /* We've reached EOF of the dir blob, so we can stop looking for
513 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
516 if (!(avc->f.states & CCorrupt)) {
517 struct cell *tc = afs_GetCellStale(avc->f.fid.Cell, READ_LOCK);
518 afs_warn("afs: Corrupt directory (%d.%d.%d.%d [%s] @%lx, pos %d)\n",
519 avc->f.fid.Cell, avc->f.fid.Fid.Volume,
520 avc->f.fid.Fid.Vnode, avc->f.fid.Fid.Unique,
521 tc ? tc->cellName : "",
522 (unsigned long)&tdc->f.inode, dirpos);
524 afs_PutCell(tc, READ_LOCK);
525 UpgradeSToWLock(&avc->lock, 814);
526 avc->f.states |= CCorrupt;
533 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
534 ntohl(de->fid.vnode));
535 len = strlen(de->name);
537 /* filldir returns -EINVAL when the buffer is full. */
539 unsigned int type = DT_UNKNOWN;
540 struct VenusFid afid;
543 afid.Cell = avc->f.fid.Cell;
544 afid.Fid.Volume = avc->f.fid.Fid.Volume;
545 afid.Fid.Vnode = ntohl(de->fid.vnode);
546 afid.Fid.Unique = ntohl(de->fid.vunique);
547 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
549 } else if ((tvc = afs_FindVCache(&afid, 0))) {
550 if (tvc->mvstat != AFS_MVSTAT_FILE) {
552 } else if (((tvc->f.states) & (CStatd | CTruth))) {
553 /* CTruth will be set if the object has
558 else if (vtype == VREG)
560 /* Don't do this until we're sure it can't be a mtpt */
561 /* else if (vtype == VLNK)
563 /* what other types does AFS support? */
565 /* clean up from afs_FindVCache */
569 * If this is NFS readdirplus, then the filler is going to
570 * call getattr on this inode, which will deadlock if we're
574 #if defined(USE_FOP_ITERATE)
575 /* dir_emit returns a bool - true when it succeeds.
576 * Inverse the result to fit with how we check "code" */
577 code = !dir_emit(ctx, de->name, len, ino, type);
579 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
586 offset = dirpos + 1 + ((len + 16) >> 5);
588 /* If filldir didn't fill in the last one this is still pointing to that
594 #if defined(USE_FOP_ITERATE)
595 ctx->pos = (loff_t) offset;
597 fp->f_pos = (loff_t) offset;
599 ReleaseReadLock(&tdc->lock);
601 UpgradeSToWLock(&avc->lock, 813);
602 avc->f.states &= ~CReadDir;
604 avc->readdir_pid = 0;
605 ReleaseSharedLock(&avc->lock);
608 afs_PutFakeStat(&fakestat);
609 afs_DestroyReq(treq);
616 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
618 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
624 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
626 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
630 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
631 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_LONG,
632 vmap->vm_end - vmap->vm_start, ICL_TYPE_LONG, 0);
634 /* get a validated vcache entry */
635 code = afs_linux_VerifyVCache(vcp, NULL);
638 /* Linux's Flushpage implementation doesn't use credp, so optimise
639 * our code to not need to crref() it */
640 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
642 code = generic_file_mmap(fp, vmap);
645 vcp->f.states |= CMAPPED;
653 afs_linux_open(struct inode *ip, struct file *fp)
655 struct vcache *vcp = VTOAFS(ip);
656 cred_t *credp = crref();
660 code = afs_open(&vcp, fp->f_flags, credp);
664 return afs_convert_code(code);
668 afs_linux_release(struct inode *ip, struct file *fp)
670 struct vcache *vcp = VTOAFS(ip);
671 cred_t *credp = crref();
675 code = afs_close(vcp, fp->f_flags, credp);
676 ObtainWriteLock(&vcp->lock, 807);
681 ReleaseWriteLock(&vcp->lock);
685 return afs_convert_code(code);
689 #if defined(FOP_FSYNC_TAKES_DENTRY)
690 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
691 #elif defined(FOP_FSYNC_TAKES_RANGE)
692 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
694 afs_linux_fsync(struct file *fp, int datasync)
698 struct inode *ip = FILE_INODE(fp);
699 cred_t *credp = crref();
701 #if defined(FOP_FSYNC_TAKES_RANGE)
702 afs_linux_lock_inode(ip);
705 code = afs_fsync(VTOAFS(ip), credp);
707 #if defined(FOP_FSYNC_TAKES_RANGE)
708 afs_linux_unlock_inode(ip);
711 return afs_convert_code(code);
717 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
720 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
721 cred_t *credp = crref();
722 struct AFS_FLOCK flock;
724 /* Convert to a lock format afs_lockctl understands. */
725 memset(&flock, 0, sizeof(flock));
726 flock.l_type = flp->fl_type;
727 flock.l_pid = flp->fl_pid;
729 flock.l_start = flp->fl_start;
730 if (flp->fl_end == OFFSET_MAX)
731 flock.l_len = 0; /* Lock to end of file */
733 flock.l_len = flp->fl_end - flp->fl_start + 1;
735 /* Safe because there are no large files, yet */
736 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
737 if (cmd == F_GETLK64)
739 else if (cmd == F_SETLK64)
741 else if (cmd == F_SETLKW64)
743 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
746 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
749 if ((code == 0 || flp->fl_type == F_UNLCK) &&
750 (cmd == F_SETLK || cmd == F_SETLKW)) {
751 code = afs_posix_lock_file(fp, flp);
752 if (code && flp->fl_type != F_UNLCK) {
753 struct AFS_FLOCK flock2;
755 flock2.l_type = F_UNLCK;
757 afs_lockctl(vcp, &flock2, F_SETLK, credp);
761 /* If lockctl says there are no conflicting locks, then also check with the
762 * kernel, as lockctl knows nothing about byte range locks
764 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
765 afs_posix_test_lock(fp, flp);
766 /* If we found a lock in the kernel's structure, return it */
767 if (flp->fl_type != F_UNLCK) {
773 /* Convert flock back to Linux's file_lock */
774 flp->fl_type = flock.l_type;
775 flp->fl_pid = flock.l_pid;
776 flp->fl_start = flock.l_start;
777 if (flock.l_len == 0)
778 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
780 flp->fl_end = flock.l_start + flock.l_len - 1;
786 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
788 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
790 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
791 cred_t *credp = crref();
792 struct AFS_FLOCK flock;
793 /* Convert to a lock format afs_lockctl understands. */
794 memset(&flock, 0, sizeof(flock));
795 flock.l_type = flp->fl_type;
796 flock.l_pid = flp->fl_pid;
801 /* Safe because there are no large files, yet */
802 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
803 if (cmd == F_GETLK64)
805 else if (cmd == F_SETLK64)
807 else if (cmd == F_SETLKW64)
809 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
812 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
815 if ((code == 0 || flp->fl_type == F_UNLCK) &&
816 (cmd == F_SETLK || cmd == F_SETLKW)) {
817 flp->fl_flags &=~ FL_SLEEP;
818 code = flock_lock_file_wait(fp, flp);
819 if (code && flp->fl_type != F_UNLCK) {
820 struct AFS_FLOCK flock2;
822 flock2.l_type = F_UNLCK;
824 afs_lockctl(vcp, &flock2, F_SETLK, credp);
828 /* Convert flock back to Linux's file_lock */
829 flp->fl_type = flock.l_type;
830 flp->fl_pid = flock.l_pid;
838 * essentially the same as afs_fsync() but we need to get the return
839 * code for the sys_close() here, not afs_linux_release(), so call
840 * afs_StoreAllSegments() with AFS_LASTSTORE
843 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
844 afs_linux_flush(struct file *fp, fl_owner_t id)
846 afs_linux_flush(struct file *fp)
849 struct vrequest *treq = NULL;
857 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
865 vcp = VTOAFS(FILE_INODE(fp));
867 code = afs_CreateReq(&treq, credp);
870 /* If caching is bypassed for this file, or globally, just return 0 */
871 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
874 ObtainReadLock(&vcp->lock);
875 if (vcp->cachingStates & FCSBypass)
877 ReleaseReadLock(&vcp->lock);
880 /* future proof: don't rely on 0 return from afs_InitReq */
885 ObtainSharedLock(&vcp->lock, 535);
886 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
887 UpgradeSToWLock(&vcp->lock, 536);
888 if (!AFS_IS_DISCONNECTED) {
889 code = afs_StoreAllSegments(vcp,
891 AFS_SYNC | AFS_LASTSTORE);
893 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
895 ConvertWToSLock(&vcp->lock);
897 code = afs_CheckCode(code, treq, 54);
898 ReleaseSharedLock(&vcp->lock);
901 afs_DestroyReq(treq);
906 return afs_convert_code(code);
909 struct file_operations afs_dir_fops = {
910 .read = generic_read_dir,
911 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE_SHARED)
912 .iterate_shared = afs_linux_readdir,
913 #elif defined(USE_FOP_ITERATE)
914 .iterate = afs_linux_readdir,
916 .readdir = afs_linux_readdir,
918 .unlocked_ioctl = afs_unlocked_xioctl,
919 .compat_ioctl = afs_unlocked_xioctl,
920 .open = afs_linux_open,
921 .release = afs_linux_release,
922 .llseek = default_llseek,
923 #ifdef HAVE_LINUX_NOOP_FSYNC
926 .fsync = simple_sync_file,
930 struct file_operations afs_file_fops = {
931 #ifdef STRUCT_FILE_OPERATIONS_HAS_READ_ITER
932 .read_iter = afs_linux_read_iter,
933 .write_iter = afs_linux_write_iter,
934 # if !defined(HAVE_LINUX___VFS_WRITE) && !defined(HAVE_LINUX_KERNEL_WRITE)
935 .read = new_sync_read,
936 .write = new_sync_write,
938 #elif defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
939 .aio_read = afs_linux_aio_read,
940 .aio_write = afs_linux_aio_write,
941 .read = do_sync_read,
942 .write = do_sync_write,
944 .read = afs_linux_read,
945 .write = afs_linux_write,
947 .unlocked_ioctl = afs_unlocked_xioctl,
948 .compat_ioctl = afs_unlocked_xioctl,
949 .mmap = afs_linux_mmap,
950 .open = afs_linux_open,
951 .flush = afs_linux_flush,
952 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
953 .sendfile = generic_file_sendfile,
955 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE) && !defined(HAVE_LINUX_DEFAULT_FILE_SPLICE_READ)
956 # if defined(HAVE_LINUX_ITER_FILE_SPLICE_WRITE)
957 .splice_write = iter_file_splice_write,
959 .splice_write = generic_file_splice_write,
961 # if LINUX_VERSION_CODE >= KERNEL_VERSION(6,5,0)
962 .splice_read = filemap_splice_read,
964 .splice_read = generic_file_splice_read,
967 .release = afs_linux_release,
968 .fsync = afs_linux_fsync,
969 .lock = afs_linux_lock,
970 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
971 .flock = afs_linux_flock,
973 .llseek = default_llseek,
976 static struct dentry *
977 canonical_dentry(struct inode *ip)
979 struct vcache *vcp = VTOAFS(ip);
980 struct dentry *first = NULL, *ret = NULL, *cur;
981 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
982 struct hlist_node *p;
986 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
987 * otherwise, use the first dentry in ip->i_dentry.
988 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
990 /* note that vcp->target_link specifies which dentry to use, but we have
991 * no reference held on that dentry. so, we cannot use or dereference
992 * vcp->target_link itself, since it may have been freed. instead, we only
993 * use it to compare to pointers in the ip->i_dentry list. */
997 afs_d_alias_lock(ip);
999 afs_d_alias_foreach_reverse(cur, ip, p) {
1000 if (!vcp->target_link || cur == vcp->target_link) {
1009 if (!ret && first) {
1013 vcp->target_link = ret;
1016 afs_linux_dget(ret);
1018 afs_d_alias_unlock(ip);
1023 /**********************************************************************
1024 * AFS Linux dentry operations
1025 **********************************************************************/
1027 /* afs_linux_revalidate
1028 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
1031 afs_linux_revalidate(struct dentry *dp)
1033 struct vattr *vattr = NULL;
1034 struct vcache *vcp = VTOAFS(dp->d_inode);
1038 if (afs_shuttingdown != AFS_RUNNING)
1043 code = afs_CreateAttr(&vattr);
1048 /* This avoids the crref when we don't have to do it. Watch for
1049 * changes in afs_getattr that don't get replicated here!
1051 if (vcp->f.states & CStatd &&
1052 (!afs_fakestat_enable || vcp->mvstat != AFS_MVSTAT_MTPT) &&
1054 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
1055 code = afs_CopyOutAttrs(vcp, vattr);
1058 code = afs_getattr(vcp, vattr, credp);
1063 afs_fill_inode(AFSTOV(vcp), vattr);
1065 afs_DestroyAttr(vattr);
1070 return afs_convert_code(code);
1074 * Set iattr data into vattr. Assume vattr cleared before call.
1077 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
1079 vattrp->va_mask = iattrp->ia_valid;
1080 if (iattrp->ia_valid & ATTR_MODE)
1081 vattrp->va_mode = iattrp->ia_mode;
1082 if (iattrp->ia_valid & ATTR_UID)
1083 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
1084 if (iattrp->ia_valid & ATTR_GID)
1085 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
1086 if (iattrp->ia_valid & ATTR_SIZE)
1087 vattrp->va_size = iattrp->ia_size;
1088 if (iattrp->ia_valid & ATTR_ATIME) {
1089 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1090 vattrp->va_atime.tv_nsec = 0;
1092 if (iattrp->ia_valid & ATTR_MTIME) {
1093 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1094 vattrp->va_mtime.tv_nsec = 0;
1096 if (iattrp->ia_valid & ATTR_CTIME) {
1097 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1098 vattrp->va_ctime.tv_nsec = 0;
1103 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1106 vattr2inode(struct inode *ip, struct vattr *vp)
1108 ip->i_ino = vp->va_nodeid;
1109 #ifdef HAVE_LINUX_SET_NLINK
1110 set_nlink(ip, vp->va_nlink);
1112 ip->i_nlink = vp->va_nlink;
1114 ip->i_blocks = vp->va_blocks;
1115 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1116 ip->i_blkbits = AFS_BLKBITS;
1118 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1119 ip->i_blksize = vp->va_blocksize;
1121 ip->i_rdev = vp->va_rdev;
1122 ip->i_mode = vp->va_mode;
1123 ip->i_uid = afs_make_kuid(vp->va_uid);
1124 ip->i_gid = afs_make_kgid(vp->va_gid);
1125 i_size_write(ip, vp->va_size);
1126 afs_inode_set_atime(ip, vp->va_atime.tv_sec, 0);
1127 /* Set the mtime nanoseconds to the sysname generation number.
1128 * This convinces NFS clients that all directories have changed
1129 * any time the sysname list changes.
1131 afs_inode_set_mtime(ip, vp->va_mtime.tv_sec, afs_sysnamegen);
1132 afs_inode_set_ctime(ip, vp->va_ctime.tv_sec, 0);
1135 /* afs_notify_change
1136 * Linux version of setattr call. What to change is in the iattr struct.
1137 * We need to set bits in both the Linux inode as well as the vcache.
1139 #if defined(IOP_TAKES_MNT_IDMAP)
1141 afs_notify_change(struct mnt_idmap *idmap, struct dentry *dp, struct iattr *iattrp)
1142 #elif defined(IOP_TAKES_USER_NAMESPACE)
1144 afs_notify_change(struct user_namespace *mnt_userns, struct dentry *dp, struct iattr *iattrp)
1147 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1150 struct vattr *vattr = NULL;
1151 cred_t *credp = crref();
1152 struct inode *ip = dp->d_inode;
1156 code = afs_CreateAttr(&vattr);
1161 iattr2vattr(vattr, iattrp); /* Convert for AFS vnodeops call. */
1163 code = afs_setattr(VTOAFS(ip), vattr, credp);
1165 afs_getattr(VTOAFS(ip), vattr, credp);
1166 vattr2inode(ip, vattr);
1168 afs_DestroyAttr(vattr);
1173 return afs_convert_code(code);
1176 #if defined(IOP_TAKES_MNT_IDMAP)
1178 afs_linux_getattr(struct mnt_idmap *idmap, const struct path *path, struct kstat *stat,
1179 u32 request_mask, unsigned int sync_mode)
1181 int err = afs_linux_revalidate(path->dentry);
1183 # if defined(GENERIC_FILLATTR_TAKES_REQUEST_MASK)
1184 generic_fillattr(afs_mnt_idmap, request_mask, path->dentry->d_inode, stat);
1186 generic_fillattr(afs_mnt_idmap, path->dentry->d_inode, stat);
1191 #elif defined(IOP_TAKES_USER_NAMESPACE)
1193 afs_linux_getattr(struct user_namespace *mnt_userns, const struct path *path, struct kstat *stat,
1194 u32 request_mask, unsigned int sync_mode)
1196 int err = afs_linux_revalidate(path->dentry);
1198 generic_fillattr(afs_ns, path->dentry->d_inode, stat);
1202 #elif defined(IOP_GETATTR_TAKES_PATH_STRUCT)
1204 afs_linux_getattr(const struct path *path, struct kstat *stat, u32 request_mask, unsigned int sync_mode)
1206 int err = afs_linux_revalidate(path->dentry);
1208 generic_fillattr(path->dentry->d_inode, stat);
1214 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1216 int err = afs_linux_revalidate(dentry);
1218 generic_fillattr(dentry->d_inode, stat);
1225 parent_vcache_dv(struct inode *inode, cred_t *credp)
1228 struct vcache *pvcp;
1231 * If parent is a mount point and we are using fakestat, we may need
1232 * to look at the fake vcache entry instead of what the vfs is giving
1233 * us. The fake entry is the one with the useful DataVersion.
1235 pvcp = VTOAFS(inode);
1236 if (pvcp->mvstat == AFS_MVSTAT_MTPT && afs_fakestat_enable) {
1237 struct vrequest treq;
1238 struct afs_fakestat_state fakestate;
1244 afs_InitReq(&treq, credp);
1245 afs_InitFakeStat(&fakestate);
1246 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1249 afs_PutFakeStat(&fakestate);
1251 return hgetlo(pvcp->f.m.DataVersion);
1255 filter_enoent(int code)
1257 #ifdef HAVE_LINUX_FATAL_SIGNAL_PENDING
1258 if (code == ENOENT && fatal_signal_pending(current)) {
1261 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
1262 # error fatal_signal_pending not available, but it should be
1267 #ifndef D_SPLICE_ALIAS_RACE
1269 static inline void dentry_race_lock(void) {}
1270 static inline void dentry_race_unlock(void) {}
1274 static DEFINE_MUTEX(dentry_race_sem);
1277 dentry_race_lock(void)
1279 mutex_lock(&dentry_race_sem);
1282 dentry_race_unlock(void)
1284 mutex_unlock(&dentry_race_sem);
1287 /* Leave some trace that this code is enabled; otherwise it's pretty hard to
1289 static __attribute__((used)) const char dentry_race_marker[] = "d_splice_alias race workaround enabled";
1292 check_dentry_race(struct dentry *dp)
1296 /* In Linux, before commit 4919c5e45a91b5db5a41695fe0357fbdff0d5767,
1297 * d_splice_alias can momentarily hash a dentry before it's fully
1298 * populated. This only happens for a moment, since it's unhashed again
1299 * right after (in d_move), but this can make the dentry be found by
1300 * __d_lookup, and then given to us.
1302 * So check if the dentry is unhashed; if it is, then the dentry is not
1303 * valid. We lock dentry_race_lock() to ensure that d_splice_alias is
1304 * no longer running. Locking d_lock is required to check the dentry's
1305 * flags, so lock that, too.
1308 spin_lock(&dp->d_lock);
1309 if (d_unhashed(dp)) {
1312 spin_unlock(&dp->d_lock);
1313 dentry_race_unlock();
1317 #endif /* D_SPLICE_ALIAS_RACE */
1319 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1320 * In kernels 2.2.10 and above, we are passed an additional flags var which
1321 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1322 * we are advised to follow the entry if it is a link or to make sure that
1323 * it is a directory. But since the kernel itself checks these possibilities
1324 * later on, we shouldn't have to do it until later. Perhaps in the future..
1326 * The code here assumes that on entry the global lock is not held
1329 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1330 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1331 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1332 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1334 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1337 cred_t *credp = NULL;
1338 struct vcache *vcp, *pvcp, *tvc = NULL;
1339 struct dentry *parent;
1341 struct afs_fakestat_state fakestate;
1343 afs_uint32 parent_dv;
1347 /* We don't support RCU path walking */
1348 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1349 if (flags & LOOKUP_RCU)
1351 if (nd->flags & LOOKUP_RCU)
1356 #ifdef D_SPLICE_ALIAS_RACE
1357 if (check_dentry_race(dp)) {
1364 afs_InitFakeStat(&fakestate);
1367 vcp = VTOAFS(dp->d_inode);
1369 if (vcp == afs_globalVp)
1372 if (vcp->mvstat == AFS_MVSTAT_MTPT) {
1373 if (vcp->mvid.target_root && (vcp->f.states & CMValid)) {
1374 int tryEvalOnly = 0;
1375 struct vrequest *treq = NULL;
1379 code = afs_CreateReq(&treq, credp);
1383 if ((strcmp(dp->d_name.name, ".directory") == 0)) {
1387 code = afs_TryEvalFakeStat(&vcp, &fakestate, treq);
1389 code = afs_EvalFakeStat(&vcp, &fakestate, treq);
1390 afs_DestroyReq(treq);
1394 if (tryEvalOnly && vcp->mvstat == AFS_MVSTAT_MTPT) {
1395 /* a mount point, not yet replaced by its directory */
1399 } else if (vcp->mvstat == AFS_MVSTAT_ROOT && *dp->d_name.name != '/') {
1400 osi_Assert(vcp->mvid.parent != NULL);
1403 parent = dget_parent(dp);
1404 pvcp = VTOAFS(parent->d_inode);
1405 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1407 /* If the parent's DataVersion has changed or the vnode
1408 * is longer valid, we need to do a full lookup. VerifyVCache
1409 * isn't enough since the vnode may have been renamed.
1412 if (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) {
1413 struct vattr *vattr = NULL;
1415 if (credp == NULL) {
1418 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1419 code = filter_enoent(code);
1420 if (code == ENOENT) {
1421 /* ENOENT is not an error here. */
1423 osi_Assert(tvc == NULL);
1427 /* We couldn't perform the lookup, so we don't know if the
1428 * dentry is valid or not. */
1434 /* We got back the same vcache, so we're good. */
1436 } else if (tvc == VTOAFS(dp->d_inode)) {
1437 /* We got back the same vcache, so we're good. This is
1438 * different from the above case, because sometimes 'vcp' is
1439 * not the same as the vcache for dp->d_inode, if 'vcp' was a
1440 * mtpt and we evaluated it to a root dir. In rare cases,
1441 * afs_lookup might not evalute the mtpt when we do, or vice
1442 * versa, so the previous case will not succeed. But this is
1443 * still 'correct', so make sure not to mark the dentry as
1444 * invalid; it still points to the same thing! */
1448 * We got back a different file, so we know this dentry is
1449 * _not_ okay. Force it to be unhashed, since the given name
1450 * doesn't point to this file anymore.
1457 code = afs_CreateAttr(&vattr);
1463 if (afs_getattr(vcp, vattr, credp)) {
1465 afs_DestroyAttr(vattr);
1470 vattr2inode(AFSTOV(vcp), vattr);
1471 dp->d_time = parent_dv;
1473 afs_DestroyAttr(vattr);
1476 /* should we always update the attributes at this point? */
1477 /* unlikely--the vcache entry hasn't changed */
1483 /* 'dp' represents a cached negative lookup. */
1485 parent = dget_parent(dp);
1486 pvcp = VTOAFS(parent->d_inode);
1487 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1489 if (parent_dv > dp->d_time || !(pvcp->f.states & CStatd)
1490 || afs_IsDynroot(pvcp)) {
1506 #ifndef D_INVALIDATE_IS_VOID
1507 /* When (v3.18) d_invalidate was converted to void, it also started
1508 * being called automatically from revalidate, and automatically
1510 * - shrink_dcache_parent
1511 * - automatic detach of submounts
1513 * Therefore, after that point, OpenAFS revalidate logic no longer needs
1514 * to do any of those things itself for invalid dentry structs. We only need
1515 * to tell VFS it's invalid (by returning 0), and VFS will handle the rest.
1517 if (have_submounts(dp))
1525 afs_PutFakeStat(&fakestate);
1530 #ifdef ERRORS_FROM_D_REVALIDATE
1533 * If code is nonzero, we don't know whether this dentry is valid or
1534 * not; we couldn't successfully perform the relevant lookup in order
1535 * to tell. So we must not return 'valid' (1) or 'not valid' (0); we
1536 * need to return an error (e.g. -EIO).
1542 #ifndef D_INVALIDATE_IS_VOID
1545 * If we had a negative lookup for the name we want to forcibly
1546 * unhash the dentry.
1547 * Otherwise use d_invalidate which will not unhash it if still in use.
1550 shrink_dcache_parent(dp);
1562 #ifdef ERRORS_FROM_D_REVALIDATE
1566 /* We can't return an error, so default to saying the dentry is invalid. */
1572 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1574 struct vcache *vcp = VTOAFS(ip);
1575 int haveGlock = ISAFS_GLOCK();
1581 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1582 (void) afs_InactiveVCache(vcp, NULL);
1589 afs_linux_clear_nfsfs_renamed(dp);
1595 #if defined(DOP_D_DELETE_TAKES_CONST)
1596 afs_dentry_delete(const struct dentry *dp)
1598 afs_dentry_delete(struct dentry *dp)
1601 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1602 return 1; /* bad inode? */
1607 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1608 static struct vfsmount *
1609 afs_dentry_automount(afs_linux_path_t *path)
1611 struct dentry *target;
1614 * Avoid symlink resolution limits when resolving; we cannot contribute to
1615 * an infinite symlink loop.
1617 * On newer kernels the field has moved to the private nameidata structure
1618 * so we can't adjust it here. This may cause ELOOP when using a path with
1619 * 40 or more directories that are not already in the dentry cache.
1621 #if defined(STRUCT_TASK_STRUCT_HAS_TOTAL_LINK_COUNT)
1622 current->total_link_count--;
1625 target = canonical_dentry(path->dentry->d_inode);
1627 if (target == path->dentry) {
1634 path->dentry = target;
1637 spin_lock(&path->dentry->d_lock);
1638 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1639 spin_unlock(&path->dentry->d_lock);
1644 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1646 struct dentry_operations afs_dentry_operations = {
1647 .d_revalidate = afs_linux_dentry_revalidate,
1648 .d_delete = afs_dentry_delete,
1649 .d_iput = afs_dentry_iput,
1650 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1651 .d_automount = afs_dentry_automount,
1652 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1655 /**********************************************************************
1656 * AFS Linux inode operations
1657 **********************************************************************/
1661 * Merely need to set enough of vattr to get us through the create. Note
1662 * that the higher level code (open_namei) will take care of any tuncation
1663 * explicitly. Exclusive open is also taken care of in open_namei.
1665 * name is in kernel space at this point.
1668 #if defined(IOP_TAKES_MNT_IDMAP)
1670 afs_linux_create(struct mnt_idmap *idmap, struct inode *dip,
1671 struct dentry *dp, umode_t mode, bool excl)
1672 #elif defined(IOP_TAKES_USER_NAMESPACE)
1674 afs_linux_create(struct user_namespace *mnt_userns, struct inode *dip,
1675 struct dentry *dp, umode_t mode, bool excl)
1676 #elif defined(IOP_CREATE_TAKES_BOOL)
1678 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1680 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1682 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1683 struct nameidata *nd)
1684 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1686 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1687 struct nameidata *nd)
1690 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1693 struct vattr *vattr = NULL;
1694 cred_t *credp = crref();
1695 const char *name = dp->d_name.name;
1701 code = afs_CreateAttr(&vattr);
1705 vattr->va_mode = mode;
1706 vattr->va_type = mode & S_IFMT;
1708 code = afs_create(VTOAFS(dip), (char *)name, vattr, NONEXCL, mode,
1712 struct inode *ip = AFSTOV(vcp);
1714 afs_getattr(vcp, vattr, credp);
1715 afs_fill_inode(ip, vattr);
1716 insert_inode_hash(ip);
1717 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1718 dp->d_op = &afs_dentry_operations;
1720 dp->d_time = parent_vcache_dv(dip, credp);
1721 d_instantiate(dp, ip);
1724 afs_DestroyAttr(vattr);
1730 return afs_convert_code(code);
1733 /* afs_linux_lookup */
1734 static struct dentry *
1735 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1736 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1738 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1739 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1740 struct nameidata *nd)
1742 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1745 cred_t *credp = crref();
1746 struct vcache *vcp = NULL;
1747 const char *comp = dp->d_name.name;
1748 struct inode *ip = NULL;
1749 struct dentry *newdp = NULL;
1754 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1755 code = filter_enoent(code);
1756 if (code == ENOENT) {
1757 /* It's ok for the file to not be found. That's noted by the caller by
1758 * seeing that the dp->d_inode field is NULL (set by d_splice_alias or
1761 osi_Assert(vcp == NULL);
1769 struct vattr *vattr = NULL;
1770 struct vcache *parent_vc = VTOAFS(dip);
1772 if (parent_vc == vcp) {
1773 /* This is possible if the parent dir is a mountpoint to a volume,
1774 * and the dir entry we looked up is a mountpoint to the same
1775 * volume. Linux cannot cope with this, so return an error instead
1776 * of risking a deadlock or panic. */
1783 code = afs_CreateAttr(&vattr);
1791 afs_getattr(vcp, vattr, credp);
1792 afs_fill_inode(ip, vattr);
1793 if (hlist_unhashed(&ip->i_hash))
1794 insert_inode_hash(ip);
1796 afs_DestroyAttr(vattr);
1798 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1799 dp->d_op = &afs_dentry_operations;
1801 dp->d_time = parent_vcache_dv(dip, credp);
1805 if (ip && S_ISDIR(ip->i_mode)) {
1806 d_prune_aliases(ip);
1808 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1809 /* Only needed if this is a volume root */
1810 if (vcp->mvstat == 2)
1811 ip->i_flags |= S_AUTOMOUNT;
1815 * Take an extra reference so the inode doesn't go away if
1816 * d_splice_alias drops our reference on error.
1819 #ifdef HAVE_LINUX_IHOLD
1826 newdp = d_splice_alias(ip, dp);
1827 dentry_race_unlock();
1832 if (IS_ERR(newdp)) {
1833 /* d_splice_alias can return an error (EIO) if there is an existing
1834 * connected directory alias for this dentry. Add our dentry manually
1835 * ourselves if this happens. */
1838 #if defined(D_SPLICE_ALIAS_LEAK_ON_ERROR)
1839 /* Depending on the kernel version, d_splice_alias may or may not drop
1840 * the inode reference on error. If it didn't, do it here. */
1849 return ERR_PTR(afs_convert_code(code));
1857 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1860 cred_t *credp = crref();
1861 const char *name = newdp->d_name.name;
1862 struct inode *oldip = olddp->d_inode;
1864 /* If afs_link returned the vnode, we could instantiate the
1865 * dentry. Since it's not, we drop this one and do a new lookup.
1870 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1874 return afs_convert_code(code);
1877 /* We have to have a Linux specific sillyrename function, because we
1878 * also have to keep the dcache up to date when we're doing a silly
1879 * rename - so we don't want the generic vnodeops doing this behind our
1884 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1887 struct vcache *tvc = VTOAFS(dentry->d_inode);
1888 struct dentry *__dp = NULL;
1889 char *__name = NULL;
1892 if (afs_linux_nfsfs_renamed(dentry))
1900 osi_FreeSmallSpace(__name);
1901 __name = afs_newname();
1904 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1907 osi_FreeSmallSpace(__name);
1910 } while (__dp->d_inode != NULL);
1913 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1914 VTOAFS(dir), (char *)__dp->d_name.name,
1917 tvc->mvid.silly_name = __name;
1920 crfree(tvc->uncred);
1922 tvc->uncred = credp;
1923 tvc->f.states |= CUnlinked;
1924 afs_linux_set_nfsfs_renamed(dentry);
1926 __dp->d_time = 0; /* force to revalidate */
1927 d_move(dentry, __dp);
1929 osi_FreeSmallSpace(__name);
1940 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1943 cred_t *credp = crref();
1944 const char *name = dp->d_name.name;
1945 struct vcache *tvc = VTOAFS(dp->d_inode);
1947 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1948 && !(tvc->f.states & CUnlinked)) {
1950 code = afs_linux_sillyrename(dip, dp, credp);
1953 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1960 return afs_convert_code(code);
1964 #if defined(IOP_TAKES_MNT_IDMAP)
1966 afs_linux_symlink(struct mnt_idmap *idmap, struct inode *dip,
1967 struct dentry *dp, const char *target)
1968 #elif defined(IOP_TAKES_USER_NAMESPACE)
1970 afs_linux_symlink(struct user_namespace *mnt_userns, struct inode *dip,
1971 struct dentry *dp, const char *target)
1974 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1978 cred_t *credp = crref();
1979 struct vattr *vattr = NULL;
1980 const char *name = dp->d_name.name;
1982 /* If afs_symlink returned the vnode, we could instantiate the
1983 * dentry. Since it's not, we drop this one and do a new lookup.
1988 code = afs_CreateAttr(&vattr);
1993 code = afs_symlink(VTOAFS(dip), (char *)name, vattr, (char *)target, NULL,
1995 afs_DestroyAttr(vattr);
2000 return afs_convert_code(code);
2003 #if defined(IOP_TAKES_MNT_IDMAP)
2005 afs_linux_mkdir(struct mnt_idmap *idmap, struct inode *dip,
2006 struct dentry *dp, umode_t mode)
2007 #elif defined(IOP_TAKES_USER_NAMESPACE)
2009 afs_linux_mkdir(struct user_namespace *mnt_userns, struct inode *dip,
2010 struct dentry *dp, umode_t mode)
2011 #elif defined(IOP_MKDIR_TAKES_UMODE_T)
2013 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
2016 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
2020 cred_t *credp = crref();
2021 struct vcache *tvcp = NULL;
2022 struct vattr *vattr = NULL;
2023 const char *name = dp->d_name.name;
2026 code = afs_CreateAttr(&vattr);
2031 vattr->va_mask = ATTR_MODE;
2032 vattr->va_mode = mode;
2034 code = afs_mkdir(VTOAFS(dip), (char *)name, vattr, &tvcp, credp);
2037 struct inode *ip = AFSTOV(tvcp);
2039 afs_getattr(tvcp, vattr, credp);
2040 afs_fill_inode(ip, vattr);
2042 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
2043 dp->d_op = &afs_dentry_operations;
2045 dp->d_time = parent_vcache_dv(dip, credp);
2046 d_instantiate(dp, ip);
2048 afs_DestroyAttr(vattr);
2054 return afs_convert_code(code);
2058 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
2061 cred_t *credp = crref();
2062 const char *name = dp->d_name.name;
2064 /* locking kernel conflicts with glock? */
2067 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
2070 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
2071 * that failed because a directory is not empty. So, we map
2072 * EEXIST to ENOTEMPTY on linux.
2074 if (code == EEXIST) {
2083 return afs_convert_code(code);
2087 #if defined(IOP_TAKES_MNT_IDMAP)
2089 afs_linux_rename(struct mnt_idmap *idmap,
2090 struct inode *oldip, struct dentry *olddp,
2091 struct inode *newip, struct dentry *newdp,
2093 #elif defined(IOP_TAKES_USER_NAMESPACE)
2095 afs_linux_rename(struct user_namespace *mnt_userns,
2096 struct inode *oldip, struct dentry *olddp,
2097 struct inode *newip, struct dentry *newdp,
2099 #elif defined(HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS)
2101 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
2102 struct inode *newip, struct dentry *newdp,
2106 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
2107 struct inode *newip, struct dentry *newdp)
2111 cred_t *credp = crref();
2112 const char *oldname = olddp->d_name.name;
2113 const char *newname = newdp->d_name.name;
2114 struct dentry *rehash = NULL;
2116 #if defined(HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS) || \
2117 defined(IOP_TAKES_MNT_IDMAP) || defined(IOP_TAKES_USER_NAMESPACE)
2119 return -EINVAL; /* no support for new flags yet */
2122 /* Prevent any new references during rename operation. */
2124 if (!d_unhashed(newdp)) {
2129 afs_maybe_shrink_dcache(olddp);
2132 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
2136 olddp->d_time = 0; /* force to revalidate */
2142 return afs_convert_code(code);
2146 /* afs_linux_ireadlink
2147 * Internal readlink which can return link contents to user or kernel space.
2148 * Note that the buffer is NOT supposed to be null-terminated.
2151 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
2154 cred_t *credp = crref();
2158 memset(&tuio, 0, sizeof(tuio));
2159 memset(&iov, 0, sizeof(iov));
2161 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
2162 code = afs_readlink(VTOAFS(ip), &tuio, credp);
2166 return maxlen - tuio.uio_resid;
2168 return afs_convert_code(code);
2171 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2172 /* afs_linux_readlink
2173 * Fill target (which is in user space) with contents of symlink.
2176 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
2179 struct inode *ip = dp->d_inode;
2182 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
2188 /* afs_linux_follow_link
2189 * a file system dependent link following routine.
2191 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2192 static const char *afs_linux_follow_link(struct dentry *dentry, void **link_data)
2194 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
2200 name = kmalloc(PATH_MAX, GFP_NOFS);
2202 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2203 return ERR_PTR(-EIO);
2210 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
2214 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2215 return ERR_PTR(code);
2222 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2223 return *link_data = name;
2225 nd_set_link(nd, name);
2230 #if defined(HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA)
2232 afs_linux_put_link(struct inode *inode, void *link_data)
2234 char *name = link_data;
2236 if (name && !IS_ERR(name))
2241 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
2243 char *name = nd_get_link(nd);
2245 if (name && !IS_ERR(name))
2248 #endif /* HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA */
2250 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2253 * Call the mapping function that reads data for a given page.
2254 * Note: When we return, it is expected that the page is unlocked. It is the
2255 * responsibility of the called function (e.g. ->readpage) to unlock the given
2256 * page, even when an error occurs.
2259 mapping_read_page(struct address_space *mapping, struct page *page)
2261 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READ_FOLIO)
2262 return mapping->a_ops->read_folio(NULL, page_folio(page));
2264 return mapping->a_ops->readpage(NULL, page);
2268 /* Populate a page by filling it from the cache file pointed at by cachefp
2269 * (which contains indicated chunk)
2270 * If task is NULL, the page copy occurs syncronously, and the routine
2271 * returns with page still locked. If task is non-NULL, then page copies
2272 * may occur in the background, and the page will be unlocked when it is
2273 * ready for use. Note that if task is non-NULL and we encounter an error
2274 * before we start the background copy, we MUST unlock 'page' before we return.
2277 afs_linux_read_cache(struct file *cachefp, struct page *page,
2278 int chunk, struct afs_lru_pages *alrupages,
2279 struct afs_pagecopy_task *task) {
2280 loff_t offset = page_offset(page);
2281 struct inode *cacheinode = cachefp->f_dentry->d_inode;
2282 struct page *newpage, *cachepage;
2283 struct address_space *cachemapping;
2287 cachemapping = cacheinode->i_mapping;
2291 /* If we're trying to read a page that's past the end of the disk
2292 * cache file, then just return a zeroed page */
2293 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
2294 zero_user_segment(page, 0, PAGE_SIZE);
2295 SetPageUptodate(page);
2301 /* From our offset, we now need to work out which page in the disk
2302 * file it corresponds to. This will be fun ... */
2303 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_SHIFT;
2305 while (cachepage == NULL) {
2306 cachepage = find_get_page(cachemapping, pageindex);
2309 newpage = page_cache_alloc(cachemapping);
2315 code = afs_add_to_page_cache_lru(alrupages, newpage, cachemapping,
2316 pageindex, GFP_KERNEL);
2318 cachepage = newpage;
2323 if (code != -EEXIST)
2327 lock_page(cachepage);
2331 if (!PageUptodate(cachepage)) {
2332 ClearPageError(cachepage);
2333 /* Note that mapping_read_page always handles unlocking the given page,
2334 * even when an error is returned. */
2335 code = mapping_read_page(cachemapping, cachepage);
2336 if (!code && !task) {
2337 wait_on_page_locked(cachepage);
2340 unlock_page(cachepage);
2344 if (PageUptodate(cachepage)) {
2345 copy_highpage(page, cachepage);
2346 flush_dcache_page(page);
2347 SetPageUptodate(page);
2352 afs_pagecopy_queue_page(task, cachepage, page);
2364 put_page(cachepage);
2370 * Return true if the file has a mapping that can read pages
2373 file_can_read_pages(struct file *fp)
2375 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READ_FOLIO)
2376 if (fp->f_dentry->d_inode->i_mapping->a_ops->read_folio != NULL)
2379 if (fp->f_dentry->d_inode->i_mapping->a_ops->readpage != NULL)
2386 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
2388 loff_t offset = page_offset(pp);
2389 struct inode *ip = FILE_INODE(fp);
2390 struct vcache *avc = VTOAFS(ip);
2392 struct file *cacheFp = NULL;
2395 struct afs_lru_pages lrupages;
2397 /* Not a UFS cache, don't do anything */
2398 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
2401 /* No readpage (ex: tmpfs) , skip */
2402 if (cachefs_noreadpage)
2405 /* Can't do anything if the vcache isn't statd , or if the read
2406 * crosses a chunk boundary.
2408 if (!(avc->f.states & CStatd) ||
2409 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
2413 ObtainWriteLock(&avc->lock, 911);
2415 /* XXX - See if hinting actually makes things faster !!! */
2417 /* See if we have a suitable entry already cached */
2421 /* We need to lock xdcache, then dcache, to handle situations where
2422 * the hint is on the free list. However, we can't safely do this
2423 * according to the locking hierarchy. So, use a non blocking lock.
2425 ObtainReadLock(&afs_xdcache);
2426 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
2428 if (dcLocked && (tdc->index != NULLIDX)
2429 && !FidCmp(&tdc->f.fid, &avc->f.fid)
2430 && tdc->f.chunk == AFS_CHUNK(offset)
2431 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2432 /* Bonus - the hint was correct */
2435 /* Only destroy the hint if its actually invalid, not if there's
2436 * just been a locking failure */
2438 ReleaseReadLock(&tdc->lock);
2445 ReleaseReadLock(&afs_xdcache);
2448 /* No hint, or hint is no longer valid - see if we can get something
2449 * directly from the dcache
2452 tdc = afs_FindDCache(avc, offset);
2455 ReleaseWriteLock(&avc->lock);
2460 ObtainReadLock(&tdc->lock);
2462 /* Is the dcache we've been given currently up to date */
2463 if (!afs_IsDCacheFresh(tdc, avc) ||
2464 (tdc->dflags & DFFetching))
2467 /* Update our hint for future abuse */
2470 /* Okay, so we've now got a cache file that is up to date */
2472 /* XXX - I suspect we should be locking the inodes before we use them! */
2474 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2475 if (cacheFp == NULL) {
2476 /* Problem getting the inode */
2481 if (!file_can_read_pages(cacheFp)) {
2482 cachefs_noreadpage = 1;
2487 afs_lru_cache_init(&lrupages);
2489 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupages, NULL);
2491 afs_lru_cache_finalize(&lrupages);
2493 filp_close(cacheFp, NULL);
2496 ReleaseReadLock(&tdc->lock);
2497 ReleaseWriteLock(&avc->lock);
2504 if (cacheFp != NULL) {
2505 filp_close(cacheFp, NULL);
2507 ReleaseWriteLock(&avc->lock);
2508 ReleaseReadLock(&tdc->lock);
2513 /* afs_linux_readpage
2515 * This function is split into two, because prepare_write/begin_write
2516 * require a readpage call which doesn't unlock the resulting page upon
2520 afs_linux_fillpage(struct file *fp, struct page *pp)
2525 struct iovec *iovecp;
2526 struct inode *ip = FILE_INODE(fp);
2527 afs_int32 cnt = page_count(pp);
2528 struct vcache *avc = VTOAFS(ip);
2529 afs_offs_t offset = page_offset(pp);
2533 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2543 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2544 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2546 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2551 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2552 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2553 99999); /* not a possible code value */
2555 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2557 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2558 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2560 AFS_DISCON_UNLOCK();
2563 /* XXX valid for no-cache also? Check last bits of files... :)
2564 * Cognate code goes in afs_NoCacheFetchProc. */
2565 if (auio->uio_resid) /* zero remainder of page */
2566 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2569 flush_dcache_page(pp);
2570 SetPageUptodate(pp);
2579 return afs_convert_code(code);
2583 afs_linux_prefetch(struct file *fp, struct page *pp)
2586 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2587 afs_offs_t offset = page_offset(pp);
2589 if (AFS_CHUNKOFFSET(offset) == 0) {
2591 struct vrequest *treq = NULL;
2596 code = afs_CreateReq(&treq, credp);
2597 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2598 tdc = afs_FindDCache(avc, offset);
2600 if (!(tdc->mflags & DFNextStarted))
2601 afs_PrefetchChunk(avc, tdc, credp, treq);
2604 ReleaseWriteLock(&avc->lock);
2606 afs_DestroyReq(treq);
2610 return afs_convert_code(code);
2614 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READAHEAD)
2616 * Bypass the cache while performing a readahead.
2617 * See the comments for afs_linux_readahead for the semantics
2621 afs_linux_bypass_readahead(struct readahead_control *rac)
2623 struct file *fp = rac->file;
2624 unsigned num_pages = readahead_count(rac);
2627 struct iovec* iovecp;
2628 struct nocache_read_request *ancr;
2633 struct inode *ip = FILE_INODE(fp);
2634 struct vcache *avc = VTOAFS(ip);
2635 afs_int32 base_index = 0;
2636 afs_int32 page_count = 0;
2639 ancr = afs_alloc_ncr(num_pages);
2643 iovecp = ancr->auio->uio_iov;
2645 for (page_ix = 0; page_ix < num_pages; ++page_ix) {
2646 pp = readahead_page(rac);
2650 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_SHIFT;
2651 if (pp->index > isize) {
2659 offset = page_offset(pp);
2660 ancr->offset = ancr->auio->uio_offset = offset;
2661 base_index = pp->index;
2663 iovecp[page_ix].iov_len = PAGE_SIZE;
2664 if (base_index != pp->index) {
2668 iovecp[page_ix].iov_base = NULL;
2670 ancr->length -= PAGE_SIZE;
2675 /* save the page for background map */
2676 iovecp[page_ix].iov_base = pp;
2679 /* If there were useful pages in the page list, schedule
2681 if (page_count > 0) {
2683 /* The background thread frees the ancr */
2684 code = afs_ReadNoCache(avc, ancr, credp);
2687 /* If there is nothing for the background thread to handle,
2688 * it won't be freeing the things that we never gave it */
2689 afs_free_ncr(&ancr);
2691 /* we do not flush, release, or unmap pages--that will be
2692 * done for us by the background thread as each page comes in
2693 * from the fileserver */
2696 /* The vfs layer will unlock/put any of the pages in the rac that were not
2700 #else /* STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READAHEAD */
2702 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2703 struct list_head *page_list, unsigned num_pages)
2707 struct iovec* iovecp;
2708 struct nocache_read_request *ancr;
2710 struct afs_lru_pages lrupages;
2714 struct inode *ip = FILE_INODE(fp);
2715 struct vcache *avc = VTOAFS(ip);
2716 afs_int32 base_index = 0;
2717 afs_int32 page_count = 0;
2720 ancr = afs_alloc_ncr(num_pages);
2722 return afs_convert_code(ENOMEM);
2723 iovecp = ancr->auio->uio_iov;
2725 afs_lru_cache_init(&lrupages);
2727 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2729 if(list_empty(page_list))
2732 pp = list_entry(page_list->prev, struct page, lru);
2733 /* If we allocate a page and don't remove it from page_list,
2734 * the page cache gets upset. */
2736 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_SHIFT;
2737 if(pp->index > isize) {
2745 offset = page_offset(pp);
2746 ancr->offset = ancr->auio->uio_offset = offset;
2747 base_index = pp->index;
2749 iovecp[page_ix].iov_len = PAGE_SIZE;
2750 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2751 if(base_index != pp->index) {
2755 iovecp[page_ix].iov_base = (void *) 0;
2757 ancr->length -= PAGE_SIZE;
2765 iovecp[page_ix].iov_base = (void *) 0;
2768 if(!PageLocked(pp)) {
2772 /* save the page for background map */
2773 iovecp[page_ix].iov_base = (void*) pp;
2775 /* and put it on the LRU cache */
2776 afs_lru_cache_add(&lrupages, pp);
2780 /* If there were useful pages in the page list, make sure all pages
2781 * are in the LRU cache, then schedule the read */
2783 afs_lru_cache_finalize(&lrupages);
2785 /* background thread frees the ancr */
2786 code = afs_ReadNoCache(avc, ancr, credp);
2789 /* If there is nothing for the background thread to handle,
2790 * it won't be freeing the things that we never gave it */
2791 afs_free_ncr(&ancr);
2793 /* we do not flush, release, or unmap pages--that will be
2794 * done for us by the background thread as each page comes in
2795 * from the fileserver */
2796 return afs_convert_code(code);
2798 #endif /* STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READAHEAD */
2801 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2803 cred_t *credp = NULL;
2805 struct iovec *iovecp;
2806 struct nocache_read_request *ancr;
2810 * Special case: if page is at or past end of file, just zero it and set
2813 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2814 zero_user_segment(pp, 0, PAGE_SIZE);
2815 SetPageUptodate(pp);
2822 /* receiver frees */
2823 ancr = afs_alloc_ncr(1);
2826 return afs_convert_code(ENOMEM);
2829 * afs_alloc_ncr has already set the auio->uio_iov, make sure setup_uio
2830 * uses the existing value when it sets auio->uio_iov.
2833 iovecp = auio->uio_iov;
2835 /* address can be NULL, because we overwrite it with 'pp', below */
2836 setup_uio(auio, iovecp, NULL, page_offset(pp),
2837 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2839 /* save the page for background map */
2840 get_page(pp); /* see above */
2841 auio->uio_iov->iov_base = (void*) pp;
2842 /* the background thread will free this */
2843 ancr->offset = page_offset(pp);
2844 ancr->length = PAGE_SIZE;
2847 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2850 return afs_convert_code(code);
2854 afs_linux_can_bypass(struct inode *ip) {
2856 switch(cache_bypass_strategy) {
2857 case NEVER_BYPASS_CACHE:
2859 case ALWAYS_BYPASS_CACHE:
2861 case LARGE_FILES_BYPASS_CACHE:
2862 if (i_size_read(ip) > cache_bypass_threshold)
2870 /* Check if a file is permitted to bypass the cache by policy, and modify
2871 * the cache bypass state recorded for that file */
2874 afs_linux_bypass_check(struct inode *ip) {
2877 int bypass = afs_linux_can_bypass(ip);
2880 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2888 afs_linux_readpage(struct file *fp, struct page *pp)
2892 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2893 code = afs_linux_bypass_readpage(fp, pp);
2895 code = afs_linux_fillpage(fp, pp);
2897 code = afs_linux_prefetch(fp, pp);
2904 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READ_FOLIO)
2906 afs_linux_read_folio(struct file *fp, struct folio *folio)
2908 struct page *pp = &folio->page;
2910 return afs_linux_readpage(fp, pp);
2915 * Updates the adc and acacheFp parameters
2918 * -1 - problem getting inode or no mapping function
2921 get_dcache_readahead(struct dcache **adc, struct file **acacheFp,
2922 struct vcache *avc, loff_t offset)
2924 struct dcache *tdc = *adc;
2925 struct file *cacheFp = *acacheFp;
2928 if (tdc != NULL && tdc->f.chunk != AFS_CHUNK(offset)) {
2930 ReleaseReadLock(&tdc->lock);
2934 if (cacheFp != NULL) {
2935 filp_close(cacheFp, NULL);
2942 tdc = afs_FindDCache(avc, offset);
2944 ObtainReadLock(&tdc->lock);
2945 if (!afs_IsDCacheFresh(tdc, avc) ||
2946 (tdc->dflags & DFFetching) != 0) {
2947 ReleaseReadLock(&tdc->lock);
2954 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2955 if (cacheFp == NULL) {
2956 /* Problem getting the inode */
2960 if (!file_can_read_pages(cacheFp)) {
2961 cachefs_noreadpage = 1;
2962 /* No mapping function */
2972 if (cacheFp != NULL) {
2973 filp_close(cacheFp, NULL);
2978 ReleaseReadLock(&tdc->lock);
2985 *acacheFp = cacheFp;
2989 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READAHEAD)
2991 * Readahead reads a number of pages for a particular file. We use
2992 * this to optimise the reading, by limiting the number of times upon which
2993 * we have to lookup, lock and open vcaches and dcaches.
2995 * Upon return, the vfs layer handles unlocking and putting any pages in the
2996 * rac that we did not process here.
2998 * Note: any errors detected during readahead are ignored at this stage by the
2999 * vfs. We just need to unlock/put the page and return. Errors will be detected
3000 * later in the vfs processing.
3003 afs_linux_readahead(struct readahead_control *rac)
3006 struct address_space *mapping = rac->mapping;
3007 struct inode *inode = mapping->host;
3008 struct vcache *avc = VTOAFS(inode);
3010 struct file *cacheFp = NULL;
3013 struct afs_lru_pages lrupages;
3014 struct afs_pagecopy_task *task;
3016 if (afs_linux_bypass_check(inode)) {
3017 afs_linux_bypass_readahead(rac);
3020 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
3023 /* No readpage (ex: tmpfs) , skip */
3024 if (cachefs_noreadpage)
3028 code = afs_linux_VerifyVCache(avc, NULL);
3034 ObtainWriteLock(&avc->lock, 912);
3037 task = afs_pagecopy_init_task();
3041 afs_lru_cache_init(&lrupages);
3043 while ((page = readahead_page(rac)) != NULL) {
3044 offset = page_offset(page);
3046 code = get_dcache_readahead(&tdc, &cacheFp, avc, offset);
3048 if (PageLocked(page)) {
3056 /* afs_linux_read_cache will unlock the page */
3057 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupages, task);
3058 } else if (PageLocked(page)) {
3065 afs_lru_cache_finalize(&lrupages);
3067 if (cacheFp != NULL)
3068 filp_close(cacheFp, NULL);
3070 afs_pagecopy_put_task(task);
3074 ReleaseReadLock(&tdc->lock);
3078 ReleaseWriteLock(&avc->lock);
3082 #else /* STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READAHEAD */
3083 /* Readpages reads a number of pages for a particular file. We use
3084 * this to optimise the reading, by limiting the number of times upon which
3085 * we have to lookup, lock and open vcaches and dcaches
3088 afs_linux_readpages(struct file *fp, struct address_space *mapping,
3089 struct list_head *page_list, unsigned int num_pages)
3091 struct inode *inode = mapping->host;
3092 struct vcache *avc = VTOAFS(inode);
3094 struct file *cacheFp = NULL;
3096 unsigned int page_idx;
3098 struct afs_lru_pages lrupages;
3099 struct afs_pagecopy_task *task;
3101 if (afs_linux_bypass_check(inode))
3102 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
3104 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
3107 /* No readpage (ex: tmpfs) , skip */
3108 if (cachefs_noreadpage)
3112 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
3117 ObtainWriteLock(&avc->lock, 912);
3120 task = afs_pagecopy_init_task();
3124 afs_lru_cache_init(&lrupages);
3126 for (page_idx = 0; page_idx < num_pages; page_idx++) {
3127 struct page *page = list_entry(page_list->prev, struct page, lru);
3128 list_del(&page->lru);
3129 offset = page_offset(page);
3131 code = get_dcache_readahead(&tdc, &cacheFp, avc, offset);
3137 if (tdc && !afs_add_to_page_cache_lru(&lrupages, page, mapping, page->index,
3139 /* Note that afs_add_to_page_cache_lru() locks the 'page'.
3140 * afs_linux_read_cache() is guaranteed to handle unlocking it. */
3141 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupages, task);
3145 afs_lru_cache_finalize(&lrupages);
3149 filp_close(cacheFp, NULL);
3151 afs_pagecopy_put_task(task);
3155 ReleaseReadLock(&tdc->lock);
3159 ReleaseWriteLock(&avc->lock);
3163 #endif /* STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READAHEAD */
3165 /* Prepare an AFS vcache for writeback. Should be called with the vcache
3168 afs_linux_prepare_writeback(struct vcache *avc) {
3170 struct pagewriter *pw;
3172 pid = MyPidxx2Pid(MyPidxx);
3173 /* Prevent recursion into the writeback code */
3174 spin_lock(&avc->pagewriter_lock);
3175 list_for_each_entry(pw, &avc->pagewriters, link) {
3176 if (pw->writer == pid) {
3177 spin_unlock(&avc->pagewriter_lock);
3178 return AOP_WRITEPAGE_ACTIVATE;
3181 spin_unlock(&avc->pagewriter_lock);
3183 /* Add ourselves to writer list */
3184 pw = osi_Alloc(sizeof(struct pagewriter));
3186 spin_lock(&avc->pagewriter_lock);
3187 list_add_tail(&pw->link, &avc->pagewriters);
3188 spin_unlock(&avc->pagewriter_lock);
3194 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
3195 struct vrequest *treq = NULL;
3198 if (!afs_CreateReq(&treq, credp)) {
3199 code = afs_DoPartialWrite(avc, treq);
3200 afs_DestroyReq(treq);
3203 return afs_convert_code(code);
3207 afs_linux_complete_writeback(struct vcache *avc) {
3208 struct pagewriter *pw, *store;
3210 struct list_head tofree;
3212 INIT_LIST_HEAD(&tofree);
3213 pid = MyPidxx2Pid(MyPidxx);
3214 /* Remove ourselves from writer list */
3215 spin_lock(&avc->pagewriter_lock);
3216 list_for_each_entry_safe(pw, store, &avc->pagewriters, link) {
3217 if (pw->writer == pid) {
3218 list_del(&pw->link);
3219 /* osi_Free may sleep so we need to defer it */
3220 list_add_tail(&pw->link, &tofree);
3223 spin_unlock(&avc->pagewriter_lock);
3224 list_for_each_entry_safe(pw, store, &tofree, link) {
3225 list_del(&pw->link);
3226 osi_Free(pw, sizeof(struct pagewriter));
3230 /* Writeback a given page syncronously. Called with no AFS locks held */
3232 afs_linux_page_writeback(struct inode *ip, struct page *pp,
3233 unsigned long offset, unsigned int count,
3236 struct vcache *vcp = VTOAFS(ip);
3244 memset(&tuio, 0, sizeof(tuio));
3245 memset(&iovec, 0, sizeof(iovec));
3247 buffer = kmap(pp) + offset;
3248 base = page_offset(pp) + offset;
3251 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
3252 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
3253 ICL_TYPE_INT32, 99999);
3255 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
3257 code = afs_write(vcp, &tuio, f_flags, credp, 0);
3259 i_size_write(ip, vcp->f.m.Length);
3260 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
3262 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
3264 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
3265 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
3266 ICL_TYPE_INT32, code);
3275 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
3276 unsigned long offset, unsigned int count)
3280 struct vcache *vcp = VTOAFS(ip);
3283 /* Catch recursive writeback. This occurs if the kernel decides
3284 * writeback is required whilst we are writing to the cache, or
3285 * flushing to the server. When we're running syncronously (as
3286 * opposed to from writepage) we can't actually do anything about
3287 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
3290 ObtainWriteLock(&vcp->lock, 532);
3291 afs_linux_prepare_writeback(vcp);
3292 ReleaseWriteLock(&vcp->lock);
3296 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
3299 ObtainWriteLock(&vcp->lock, 533);
3301 code1 = afs_linux_dopartialwrite(vcp, credp);
3302 afs_linux_complete_writeback(vcp);
3303 ReleaseWriteLock(&vcp->lock);
3314 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
3315 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
3317 afs_linux_writepage(struct page *pp)
3320 struct address_space *mapping = pp->mapping;
3321 struct inode *inode;
3324 unsigned int to = PAGE_SIZE;
3331 inode = mapping->host;
3332 vcp = VTOAFS(inode);
3333 isize = i_size_read(inode);
3335 /* Don't defeat an earlier truncate */
3336 if (page_offset(pp) > isize) {
3337 set_page_writeback(pp);
3343 ObtainWriteLock(&vcp->lock, 537);
3344 code = afs_linux_prepare_writeback(vcp);
3345 if (code == AOP_WRITEPAGE_ACTIVATE) {
3346 /* WRITEPAGE_ACTIVATE is the only return value that permits us
3347 * to return with the page still locked */
3348 ReleaseWriteLock(&vcp->lock);
3353 /* Grab the creds structure currently held in the vnode, and
3354 * get a reference to it, in case it goes away ... */
3360 ReleaseWriteLock(&vcp->lock);
3363 set_page_writeback(pp);
3365 SetPageUptodate(pp);
3367 /* We can unlock the page here, because it's protected by the
3368 * page_writeback flag. This should make us less vulnerable to
3369 * deadlocking in afs_write and afs_DoPartialWrite
3373 /* If this is the final page, then just write the number of bytes that
3374 * are actually in it */
3375 if ((isize - page_offset(pp)) < to )
3376 to = isize - page_offset(pp);
3378 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
3381 ObtainWriteLock(&vcp->lock, 538);
3383 /* As much as we might like to ignore a file server error here,
3384 * and just try again when we close(), unfortunately StoreAllSegments
3385 * will invalidate our chunks if the server returns a permanent error,
3386 * so we need to at least try and get that error back to the user
3389 code1 = afs_linux_dopartialwrite(vcp, credp);
3391 afs_linux_complete_writeback(vcp);
3392 ReleaseWriteLock(&vcp->lock);
3397 end_page_writeback(pp);
3409 /* afs_linux_permission
3410 * Check access rights - returns error if can't check or permission denied.
3413 #if defined(IOP_TAKES_MNT_IDMAP)
3415 afs_linux_permission(struct mnt_idmap *idmap, struct inode *ip, int mode)
3416 #elif defined(IOP_TAKES_USER_NAMESPACE)
3418 afs_linux_permission(struct user_namespace *mnt_userns, struct inode *ip, int mode)
3419 #elif defined(IOP_PERMISSION_TAKES_FLAGS)
3421 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
3422 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
3424 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
3427 afs_linux_permission(struct inode *ip, int mode)
3434 /* Check for RCU path walking */
3435 #if defined(IOP_PERMISSION_TAKES_FLAGS)
3436 if (flags & IPERM_FLAG_RCU)
3438 #elif defined(MAY_NOT_BLOCK)
3439 if (mode & MAY_NOT_BLOCK)
3445 if (mode & MAY_EXEC)
3447 if (mode & MAY_READ)
3449 if (mode & MAY_WRITE)
3451 code = afs_access(VTOAFS(ip), tmp, credp);
3455 return afs_convert_code(code);
3459 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
3463 struct inode *inode = FILE_INODE(file);
3464 loff_t pagebase = page_offset(page);
3466 if (i_size_read(inode) < (pagebase + offset))
3467 i_size_write(inode, pagebase + offset);
3469 if (PageChecked(page)) {
3470 SetPageUptodate(page);
3471 ClearPageChecked(page);
3474 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
3480 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
3485 * Linux's Documentation/filesystems/vfs.txt (.rst) details the expected
3486 * behaviour of prepare_write (prior to 2.6.28) and write_begin (2.6.28).
3487 * Essentially, if the page exists within the file, and is not being fully
3488 * written, then we should populate it.
3491 if (!PageUptodate(page)) {
3492 loff_t pagebase = page_offset(page);
3493 loff_t isize = i_size_read(page->mapping->host);
3495 /* Is the location we are writing to beyond the end of the file? */
3496 if (pagebase >= isize ||
3497 ((from == 0) && (pagebase + to) >= isize)) {
3498 zero_user_segments(page, 0, from, to, PAGE_SIZE);
3499 SetPageChecked(page);
3500 /* Are we we writing a full page */
3501 } else if (from == 0 && to == PAGE_SIZE) {
3502 SetPageChecked(page);
3503 /* Is the page readable, if it's wronly, we don't care, because we're
3504 * not actually going to read from it ... */
3505 } else if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
3506 /* We don't care if fillpage fails, because if it does the page
3507 * won't be marked as up to date
3509 afs_linux_fillpage(file, page);
3515 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3517 afs_linux_write_end(struct file *file, struct address_space *mapping,
3518 loff_t pos, unsigned len, unsigned copied,
3519 struct page *page, void *fsdata)
3522 unsigned int from = pos & (PAGE_SIZE - 1);
3524 code = afs_linux_commit_write(file, page, from, from + copied);
3531 # if defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN_NOFLAGS)
3533 afs_linux_write_begin(struct file *file, struct address_space *mapping,
3534 loff_t pos, unsigned len,
3535 struct page **pagep, void **fsdata)
3538 pgoff_t index = pos >> PAGE_SHIFT;
3539 unsigned int from = pos & (PAGE_SIZE - 1);
3542 page = grab_cache_page_write_begin(mapping, index);
3549 code = afs_linux_prepare_write(file, page, from, from + len);
3559 afs_linux_write_begin(struct file *file, struct address_space *mapping,
3560 loff_t pos, unsigned len, unsigned flags,
3561 struct page **pagep, void **fsdata)
3564 pgoff_t index = pos >> PAGE_SHIFT;
3565 unsigned int from = pos & (PAGE_SIZE - 1);
3568 page = grab_cache_page_write_begin(mapping, index, flags);
3575 code = afs_linux_prepare_write(file, page, from, from + len);
3583 # endif /* HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN_NOFLAGS */
3584 #endif /* STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN */
3586 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3588 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
3590 struct dentry **dpp;
3591 struct dentry *target;
3593 if (current->total_link_count > 0) {
3594 /* avoid symlink resolution limits when resolving; we cannot contribute to
3595 * an infinite symlink loop */
3596 /* only do this for follow_link when total_link_count is positive to be
3597 * on the safe side; there is at least one code path in the Linux
3598 * kernel where it seems like it may be possible to get here without
3599 * total_link_count getting incremented. it is not clear on how that
3600 * path is actually reached, but guard against it just to be safe */
3601 current->total_link_count--;
3604 target = canonical_dentry(dentry->d_inode);
3606 # ifdef STRUCT_NAMEIDATA_HAS_PATH
3607 dpp = &nd->path.dentry;
3617 *dpp = dget(dentry);
3620 nd->last_type = LAST_BIND;
3624 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
3627 static struct inode_operations afs_file_iops = {
3628 .permission = afs_linux_permission,
3629 .getattr = afs_linux_getattr,
3630 .setattr = afs_notify_change,
3633 static struct address_space_operations afs_file_aops = {
3634 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READ_FOLIO)
3635 .read_folio = afs_linux_read_folio,
3637 .readpage = afs_linux_readpage,
3639 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READAHEAD)
3640 .readahead = afs_linux_readahead,
3642 .readpages = afs_linux_readpages,
3644 .writepage = afs_linux_writepage,
3645 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_DIRTY_FOLIO) && \
3646 defined(HAVE_LINUX_BLOCK_DIRTY_FOLIO)
3647 .dirty_folio = block_dirty_folio,
3649 .set_page_dirty = __set_page_dirty_buffers,
3651 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3652 .write_begin = afs_linux_write_begin,
3653 .write_end = afs_linux_write_end,
3655 .commit_write = afs_linux_commit_write,
3656 .prepare_write = afs_linux_prepare_write,
3661 /* Separate ops vector for directories. Linux 2.2 tests type of inode
3662 * by what sort of operation is allowed.....
3665 static struct inode_operations afs_dir_iops = {
3666 .setattr = afs_notify_change,
3667 .create = afs_linux_create,
3668 .lookup = afs_linux_lookup,
3669 .link = afs_linux_link,
3670 .unlink = afs_linux_unlink,
3671 .symlink = afs_linux_symlink,
3672 .mkdir = afs_linux_mkdir,
3673 .rmdir = afs_linux_rmdir,
3674 .rename = afs_linux_rename,
3675 .getattr = afs_linux_getattr,
3676 .permission = afs_linux_permission,
3677 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3678 .follow_link = afs_linux_dir_follow_link,
3682 /* We really need a separate symlink set of ops, since do_follow_link()
3683 * determines if it _is_ a link by checking if the follow_link op is set.
3685 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3687 afs_symlink_filler(struct file *file, struct page *page)
3689 struct inode *ip = (struct inode *)page->mapping->host;
3690 char *p = (char *)kmap(page);
3694 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
3699 p[code] = '\0'; /* null terminate? */
3701 SetPageUptodate(page);
3712 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READ_FOLIO)
3714 afs_symlink_filler_folio(struct file *file, struct folio *folio)
3716 struct page *page = &folio->page;
3717 return afs_symlink_filler(file, page);
3722 static struct address_space_operations afs_symlink_aops = {
3723 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_READ_FOLIO)
3724 .read_folio = afs_symlink_filler_folio
3726 .readpage = afs_symlink_filler
3729 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3731 static struct inode_operations afs_symlink_iops = {
3732 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3733 .readlink = page_readlink,
3734 # if defined(HAVE_LINUX_PAGE_GET_LINK)
3735 .get_link = page_get_link,
3736 # elif defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
3737 .follow_link = page_follow_link,
3739 .follow_link = page_follow_link_light,
3740 .put_link = page_put_link,
3742 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
3743 .readlink = afs_linux_readlink,
3744 .follow_link = afs_linux_follow_link,
3745 .put_link = afs_linux_put_link,
3746 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3747 .setattr = afs_notify_change,
3748 .getattr = afs_linux_getattr,
3752 afs_fill_inode(struct inode *ip, struct vattr *vattr)
3755 vattr2inode(ip, vattr);
3757 #ifdef STRUCT_ADDRESS_SPACE_HAS_BACKING_DEV_INFO
3758 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
3760 /* Reset ops if symlink or directory. */
3761 if (S_ISREG(ip->i_mode)) {
3762 ip->i_op = &afs_file_iops;
3763 ip->i_fop = &afs_file_fops;
3764 ip->i_data.a_ops = &afs_file_aops;
3766 } else if (S_ISDIR(ip->i_mode)) {
3767 ip->i_op = &afs_dir_iops;
3768 ip->i_fop = &afs_dir_fops;
3770 } else if (S_ISLNK(ip->i_mode)) {
3771 ip->i_op = &afs_symlink_iops;
3772 #if defined(HAVE_LINUX_INODE_NOHIGHMEM)
3773 inode_nohighmem(ip);
3775 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3776 ip->i_data.a_ops = &afs_symlink_aops;
3777 ip->i_mapping = &ip->i_data;