LINUX 5.3.0: Check for 'recurse' arg in keyring_search
[openafs.git] / src / afs / LINUX / osi_file.c
index 8777ef0..bb7cb77 100644 (file)
 #include "afs/param.h"
 
 
-#ifdef AFS_LINUX24_ENV
-#include "h/module.h" /* early to avoid printf->printk mapping */
-#endif
+#include <linux/module.h> /* early to avoid printf->printk mapping */
 #include "afs/sysincludes.h"   /* Standard vendor system headers */
 #include "afsincludes.h"       /* Afs-based standard headers */
 #include "afs/afs_stats.h"     /* afs statistics */
-#include "h/smp_lock.h"
-#if defined(AFS_LINUX26_ENV)
-#include "h/namei.h"
+#include <linux/namei.h>
+
+#if defined(HAVE_LINUX_EXPORTFS_H)
+#include <linux/exportfs.h>
+#endif
+#include "osi_compat.h"
+
+#ifndef CURRENT_TIME
+# if defined(HAVE_LINUX_KTIME_GET_COARSE_REAL_TS64)
+#   define AFS_CURRENT_TIME(x) (ktime_get_coarse_real_ts64((x)))
+# else
+#  ifdef IATTR_TAKES_64BIT_TIME
+#    define AFS_CURRENT_TIME(x) do {*(x) = current_kernel_time64();} while (0)
+#  else
+#    define AFS_CURRENT_TIME(x) do {*(x) = current_kernel_time();} while (0)
+#  endif
+# endif
+#else
+# define AFS_CURRENT_TIME(x) do {*(x) = CURRENT_TIME;} while(0)
 #endif
-#if defined(LINUX_USE_FH)
-#include "h/exportfs.h"
+
 int cache_fh_type = -1;
 int cache_fh_len = -1;
-#endif
 
-afs_lock_t afs_xosi;           /* lock is for tvattr */
 extern struct osi_dev cacheDev;
-#if defined(AFS_LINUX24_ENV)
 extern struct vfsmount *afs_cacheMnt;
-#endif
 extern struct super_block *afs_cacheSBp;
+#if defined(STRUCT_TASK_STRUCT_HAS_CRED)
+extern struct cred *cache_creds;
+#endif
 
-#if defined(AFS_LINUX26_ENV) 
-void *
-osi_UFSOpen(afs_dcache_id_t *ainode)
+/* Old export ops: decode_fh will call back here. Accept any dentry it suggests */
+int
+afs_fh_acceptable(void *context, struct dentry *dp)
+{
+    return 1;
+}
+
+struct file *
+afs_linux_raw_open(afs_dcache_id_t *ainode)
 {
-    struct osi_file *afile = NULL;
-    extern int cacheDiskType;
     struct inode *tip = NULL;
     struct dentry *dp = NULL;
-    struct file *filp = NULL;
+    struct file* filp;
 
-    AFS_STATCNT(osi_UFSOpen);
-    if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
-       osi_Panic("UFSOpen called for non-UFS cache\n");
-    }
-    if (!afs_osicred_initialized) {
-       /* valid for alpha_osf, SunOS, Ultrix */
-       memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
-       crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
-       afs_osicred_initialized = 1;
-    }
-    afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
-    AFS_GUNLOCK();
-    if (!afile) {
-       osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
-                 (int)sizeof(struct osi_file));
-    }
-    memset(afile, 0, sizeof(struct osi_file));
-#if !defined(LINUX_USE_FH)
-    tip = iget(afs_cacheSBp, ainode->ufs);
-    if (!tip)
-       osi_Panic("Can't get inode %d\n", ainode->ufs);
-
-    dp = d_alloc_anon(tip);
-#else
-    dp = afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh, cache_fh_len, cache_fh_type);
-    if (!dp) 
-           osi_Panic("Can't get dentry\n");          
+    dp = afs_get_dentry_from_fh(afs_cacheSBp, ainode, cache_fh_len, cache_fh_type,
+               afs_fh_acceptable);
+    if ((!dp) || IS_ERR(dp))
+           osi_Panic("Can't get dentry\n");
     tip = dp->d_inode;
-#endif
-    tip->i_flags |= MS_NOATIME;        /* Disable updating access times. */
+    tip->i_flags |= S_NOATIME; /* Disable updating access times. */
 
-#if defined(STRUCT_TASK_HAS_CRED)
-    filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, current_cred());
-#else
-    filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
-#endif
+#if defined(STRUCT_TASK_STRUCT_HAS_CRED)
+    /* Use stashed credentials - prevent selinux/apparmor problems  */
+    filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, cache_creds);
     if (IS_ERR(filp))
-#if defined(LINUX_USE_FH)
-       osi_Panic("Can't open file\n");
+       filp = afs_dentry_open(dp, afs_cacheMnt, O_RDWR, current_cred());
 #else
-       osi_Panic("Can't open inode %d\n", ainode->ufs);
+    filp = dentry_open(dget(dp), mntget(afs_cacheMnt), O_RDWR);
 #endif
-    afile->filp = filp;
-    afile->size = i_size_read(FILE_INODE(filp));
-    AFS_GLOCK();
-    afile->offset = 0;
-    afile->proc = (int (*)())0;
-    afile->inum = tip->i_ino;  /* for hint validity checking */
-    return (void *)afile;
+    if (IS_ERR(filp)) {
+       afs_warn("afs: Cannot open cache file (code %d). Trying to continue, "
+                 "but AFS accesses may return errors or panic the system\n",
+                 (int) PTR_ERR(filp));
+        filp = NULL;
+    }
+
+    dput(dp);
+
+    return filp;
 }
-#else
+
 void *
 osi_UFSOpen(afs_dcache_id_t *ainode)
 {
-    register struct osi_file *afile = NULL;
+    struct osi_file *afile = NULL;
     extern int cacheDiskType;
-    afs_int32 code = 0;
-    struct inode *tip = NULL;
-    struct file *filp = NULL;
+
     AFS_STATCNT(osi_UFSOpen);
     if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
        osi_Panic("UFSOpen called for non-UFS cache\n");
     }
     if (!afs_osicred_initialized) {
-       /* valid for alpha_osf, SunOS, Ultrix */
-       memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
+       memset(&afs_osi_cred, 0, sizeof(afs_ucred_t));
        crhold(&afs_osi_cred);  /* don't let it evaporate, since it is static */
        afs_osicred_initialized = 1;
     }
-    afile = (struct osi_file *)osi_AllocLargeSpace(sizeof(struct osi_file));
     AFS_GUNLOCK();
+    afile = kmalloc(sizeof(struct osi_file), GFP_NOFS);
     if (!afile) {
        osi_Panic("osi_UFSOpen: Failed to allocate %d bytes for osi_file.\n",
-                 sizeof(struct osi_file));
+                 (int)sizeof(struct osi_file));
     }
     memset(afile, 0, sizeof(struct osi_file));
-    filp = &afile->file;
-    filp->f_dentry = &afile->dentry;
-    tip = iget(afs_cacheSBp, ainode->ufs);
-    if (!tip)
-       osi_Panic("Can't get inode %d\n", ainode->ufs);
-    FILE_INODE(filp) = tip;
-    tip->i_flags |= MS_NOATIME;        /* Disable updating access times. */
-    filp->f_flags = O_RDWR;
-#if defined(AFS_LINUX24_ENV)
-    filp->f_mode = FMODE_READ|FMODE_WRITE;
-    filp->f_op = fops_get(tip->i_fop);
-#else
-    filp->f_op = tip->i_op->default_file_ops;
-#endif
-    if (filp->f_op && filp->f_op->open)
-       code = filp->f_op->open(tip, filp);
-    if (code)
-       osi_Panic("Can't open inode %d\n", ainode->ufs);
-    afile->size = i_size_read(tip);
+
+    afile->filp = afs_linux_raw_open(ainode);
+    if (afile->filp) {
+        afile->size = i_size_read(FILE_INODE(afile->filp));
+    }
     AFS_GLOCK();
+
+    if (!afile->filp) {
+        osi_FreeLargeSpace(afile);
+        return NULL;
+    }
+
     afile->offset = 0;
     afile->proc = (int (*)())0;
-    afile->inum = ainode->ufs; /* for hint validity checking */
     return (void *)afile;
 }
-#endif
 
-#if defined(LINUX_USE_FH)
 /*
  * Given a dentry, return the file handle as encoded by the filesystem.
  * We can't assume anything about the length (words, not bytes).
@@ -162,57 +140,33 @@ void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
        max_len = cache_fh_len;
     else
        max_len = MAX_FH_LEN;
-    if (dp->d_sb->s_export_op->encode_fh) {
-        type = dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], &max_len, 0);
-        if (type == 255) {
-           osi_Panic("File handle encoding failed\n");
-        }
-        if (cache_fh_type < 0)
-            cache_fh_type = type;
-        if (cache_fh_len < 0) {
-            cache_fh_len = max_len;
-        }
-        if (type != cache_fh_type || max_len != cache_fh_len) {
-           osi_Panic("Inconsistent file handles within cache\n");
-        }
-    } else {
-         /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
-       if (cache_fh_type < 0)
-           cache_fh_type = FILEID_INO32_GEN;
-       if (cache_fh_len < 0)
-           cache_fh_len = sizeof(struct fid)/4;
-        ainode->fh.i32.ino = dp->d_inode->i_ino;
-        ainode->fh.i32.gen = dp->d_inode->i_generation;
+    type = afs_get_fh_from_dentry(dp, ainode, &max_len);
+    if (type == 255) {
+       osi_Panic("File handle encoding failed\n");
+    }
+    if (cache_fh_type < 0)
+       cache_fh_type = type;
+    if (cache_fh_len < 0) {
+       cache_fh_len = max_len;
+    }
+    if (type != cache_fh_type || max_len != cache_fh_len) {
+       osi_Panic("Inconsistent file handles within cache\n");
     }
 }
-#else
-void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
-    *ainode = dp->d_inode->i_ino;
-}
-#endif
 
 int
-afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
+afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
 {
-    register afs_int32 code;
     AFS_STATCNT(osi_Stat);
-    MObtainWriteLock(&afs_xosi, 320);
     astat->size = i_size_read(OSIFILE_INODE(afile));
-#if defined(AFS_LINUX26_ENV)
     astat->mtime = OSIFILE_INODE(afile)->i_mtime.tv_sec;
     astat->atime = OSIFILE_INODE(afile)->i_atime.tv_sec;
-#else
-    astat->mtime = OSIFILE_INODE(afile)->i_mtime;
-    astat->atime = OSIFILE_INODE(afile)->i_atime;
-#endif
-    code = 0;
-    MReleaseWriteLock(&afs_xosi);
-    return code;
+
+    return 0;
 }
 
-#ifdef AFS_LINUX26_ENV
 int
-osi_UFSClose(register struct osi_file *afile)
+osi_UFSClose(struct osi_file *afile)
 {
     AFS_STATCNT(osi_Close);
     if (afile) {
@@ -220,33 +174,14 @@ osi_UFSClose(register struct osi_file *afile)
            filp_close(afile->filp, NULL);
        }
     }
-
-    osi_FreeLargeSpace(afile);
-    return 0;
-}
-#else
-int
-osi_UFSClose(register struct osi_file *afile)
-{
-    AFS_STATCNT(osi_Close);
-    if (afile) {
-       if (FILE_INODE(&afile->file)) {
-           struct file *filp = &afile->file;
-           if (filp->f_op && filp->f_op->release)
-               filp->f_op->release(FILE_INODE(filp), filp);
-           iput(FILE_INODE(filp));
-       }
-    }
-
-    osi_FreeLargeSpace(afile);
+    kfree(afile);
     return 0;
 }
-#endif
 
 int
-osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
+osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
 {
-    register afs_int32 code;
+    afs_int32 code;
     struct osi_stat tstat;
     struct iattr newattrs;
     struct inode *inode = OSIFILE_INODE(afile);
@@ -259,73 +194,47 @@ osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
     code = afs_osi_Stat(afile, &tstat);
     if (code || tstat.size <= asize)
        return code;
-    MObtainWriteLock(&afs_xosi, 321);
     AFS_GUNLOCK();
+    afs_linux_lock_inode(inode);
 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
     down_write(&inode->i_alloc_sem);
 #endif
-#ifdef STRUCT_INODE_HAS_I_MUTEX
-    mutex_lock(&inode->i_mutex);
-#else
-    down(&inode->i_sem);
-#endif
     newattrs.ia_size = asize;
     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
-#if defined(AFS_LINUX24_ENV)
-    newattrs.ia_ctime = CURRENT_TIME;
+    AFS_CURRENT_TIME(&newattrs.ia_ctime);
 
     /* avoid notify_change() since it wants to update dentry->d_parent */
-    lock_kernel();
-    code = inode_change_ok(inode, &newattrs);
-    if (!code)
-#ifdef INODE_SETATTR_NOT_VOID
-#if defined(AFS_LINUX26_ENV)
-       if (inode->i_op && inode->i_op->setattr)
-           code = inode->i_op->setattr(afile->filp->f_dentry, &newattrs);
-       else
-#endif
-           code = inode_setattr(inode, &newattrs);
+#ifdef HAVE_LINUX_SETATTR_PREPARE
+    code = setattr_prepare(file_dentry(afile->filp), &newattrs);
 #else
-        inode_setattr(inode, &newattrs);
+    code = inode_change_ok(inode, &newattrs);
 #endif
-    unlock_kernel();
+    if (!code)
+       code = afs_inode_setattr(afile, &newattrs);
     if (!code)
        truncate_inode_pages(&inode->i_data, asize);
-#else
-    i_size_write(inode, asize);
-    if (inode->i_sb->s_op && inode->i_sb->s_op->notify_change) {
-       code = inode->i_sb->s_op->notify_change(&afile->dentry, &newattrs);
-    }
-    if (!code) {
-       truncate_inode_pages(inode, asize);
-       if (inode->i_op && inode->i_op->truncate)
-           inode->i_op->truncate(inode);
-    }
-#endif
     code = -code;
-#ifdef STRUCT_INODE_HAS_I_MUTEX
-    mutex_unlock(&inode->i_mutex);
-#else
-    up(&inode->i_sem);
-#endif
 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
     up_write(&inode->i_alloc_sem);
 #endif
+    afs_linux_unlock_inode(inode);
     AFS_GLOCK();
-    MReleaseWriteLock(&afs_xosi);
     return code;
 }
 
 
 /* Generic read interface */
 int
-afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
+afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
             afs_int32 asize)
 {
     struct uio auio;
     struct iovec iov;
     afs_int32 code;
 
+    memset(&auio, 0, sizeof(auio));
+    memset(&iov, 0, sizeof(iov));
+
     AFS_STATCNT(osi_Read);
 
     /*
@@ -333,10 +242,10 @@ afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
      * down. No point in crashing when we are already shutting down
      */
     if (!afile) {
-       if (!afs_shuttingdown)
+       if (afs_shuttingdown == AFS_RUNNING)
            osi_Panic("osi_Read called with null param");
        else
-           return EIO;
+           return -EIO;
     }
 
     if (offset != -1)
@@ -351,27 +260,32 @@ afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
     } else {
        afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, auio.uio_resid,
                   ICL_TYPE_INT32, code);
-       code = -1;
+       if (code > 0) {
+           code = -code;
+       }
     }
     return code;
 }
 
 /* Generic write interface */
 int
-afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
+afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
              afs_int32 asize)
 {
     struct uio auio;
     struct iovec iov;
     afs_int32 code;
 
+    memset(&auio, 0, sizeof(auio));
+    memset(&iov, 0, sizeof(iov));
+
     AFS_STATCNT(osi_Write);
 
     if (!afile) {
-       if (!afs_shuttingdown)
+       if (afs_shuttingdown == AFS_RUNNING)
            osi_Panic("afs_osi_Write called with null param");
        else
-           return EIO;
+           return -EIO;
     }
 
     if (offset != -1)
@@ -385,9 +299,10 @@ afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
        afile->offset += code;
     } else {
        if (code == ENOSPC)
-           afs_warnuser
-               ("\n\n\n*** Cache partition is FULL - Decrease cachesize!!! ***\n\n");
-       code = -1;
+            afs_WarnENOSPC();
+       if (code > 0) {
+           code = -code;
+       }
     }
 
     if (afile->proc)
@@ -401,7 +316,7 @@ afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
     This routine written from the RT NFS port strategy routine.
     It has been generalized a bit, but should still be pretty clear. */
 int
-afs_osi_MapStrategy(int (*aproc) (struct buf * bp), register struct buf *bp)
+afs_osi_MapStrategy(int (*aproc) (struct buf * bp), struct buf *bp)
 {
     afs_int32 returnCode;
 
@@ -442,43 +357,39 @@ osi_InitCacheInfo(char *aname)
 
     dput(dp);
 
+    afs_init_sb_export_ops(afs_cacheSBp);
+
     return 0;
 }
 
 
-#define FOP_READ(F, B, C) (F)->f_op->read(F, B, (size_t)(C), &(F)->f_pos)
-#define FOP_WRITE(F, B, C) (F)->f_op->write(F, B, (size_t)(C), &(F)->f_pos)
-
 /* osi_rdwr
  * seek, then read or write to an open inode. addrp points to data in
  * kernel space.
  */
 int
-osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
+osi_rdwr(struct osi_file *osifile, struct uio *uiop, int rw)
 {
-#ifdef AFS_LINUX26_ENV
     struct file *filp = osifile->filp;
-#else
-    struct file *filp = &osifile->file;
-#endif
-    KERNEL_SPACE_DECL;
+#ifdef AFS_FILE_NEEDS_SET_FS
+    mm_segment_t old_fs = {0};
+#endif /* AFS_FILE_NEEDS_SET_FS */
     int code = 0;
     struct iovec *iov;
-    afs_size_t count;
+    size_t count;
     unsigned long savelim;
+    loff_t pos;
 
     savelim = current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur;
     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
 
-    if (uiop->uio_seg == AFS_UIOSYS)
-       TO_USER_SPACE();
-
-    /* seek to the desired position. Return -1 on error. */
-    if (filp->f_op->llseek) {
-       if (filp->f_op->llseek(filp, (loff_t) uiop->uio_offset, 0) != uiop->uio_offset)
-           return -1;
-    } else
-       filp->f_pos = uiop->uio_offset;
+#ifdef AFS_FILE_NEEDS_SET_FS
+    if (uiop->uio_seg == AFS_UIOSYS) {
+       /* Switch into user space */
+       old_fs = get_fs();
+       set_fs(get_ds());
+    }
+#endif /* AFS_FILE_NEEDS_SET_FS */
 
     while (code == 0 && uiop->uio_resid > 0 && uiop->uio_iovcnt > 0) {
        iov = uiop->uio_iov;
@@ -489,10 +400,11 @@ osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
            continue;
        }
 
+       pos = uiop->uio_offset;
        if (rw == UIO_READ)
-           code = FOP_READ(filp, iov->iov_base, count);
+           code = afs_file_read(filp, iov->iov_base, count, &pos);
        else
-           code = FOP_WRITE(filp, iov->iov_base, count);
+           code = afs_file_write(filp, iov->iov_base, count, &pos);
 
        if (code < 0) {
            code = -code;
@@ -514,8 +426,12 @@ osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
        code = 0;
     }
 
-    if (uiop->uio_seg == AFS_UIOSYS)
-       TO_KERNEL_SPACE();
+#ifdef AFS_FILE_NEEDS_SET_FS
+    if (uiop->uio_seg == AFS_UIOSYS) {
+       /* Switch back into kernel space */
+       set_fs(old_fs);
+    }
+#endif /* AFS_FILE_NEEDS_SET_FS */
 
     current->TASK_STRUCT_RLIM[RLIMIT_FSIZE].rlim_cur = savelim;
 
@@ -526,7 +442,7 @@ osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
  * Setup a uio struct.
  */
 void
-setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
+setup_uio(struct uio *uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
          int count, uio_flag_t flag, uio_seg_t seg)
 {
     iovecp->iov_base = (char *)buf;
@@ -545,7 +461,7 @@ setup_uio(uio_t * uiop, struct iovec *iovecp, const char *buf, afs_offs_t pos,
  * UIO_WRITE : uio -> dp
  */
 int
-uiomove(char *dp, int length, uio_flag_t rw, uio_t * uiop)
+uiomove(char *dp, int length, uio_flag_t rw, struct uio *uiop)
 {
     int count;
     struct iovec *iov;