linux cache file open fail print error
[openafs.git] / src / afs / LINUX / osi_file.c
index e63de50..e1a11fb 100644 (file)
 #include <afsconfig.h>
 #include "afs/param.h"
 
-RCSID
-    ("$Header$");
 
-#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/smp_lock.h>
+#include <linux/namei.h>
+#if defined(LINUX_USE_FH)
+#include <linux/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_HAS_CRED)
+extern struct cred *cache_creds;
+#endif
 
-#if defined(AFS_LINUX26_ENV) 
-void *
-osi_UFSOpen(afs_int32 ainode)
+struct file *
+afs_linux_raw_open(afs_dcache_id_t *ainode)
 {
-    register struct osi_file *afile = NULL;
-    extern int cacheDiskType;
     struct inode *tip = NULL;
     struct dentry *dp = NULL;
-    struct file *filp = NULL;
-#if defined(AFS_CACHE_VNODE_PATH)
-    int code;
-    int dummy;
-    char fname[1024];
-#endif
-    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",
-                 sizeof(struct osi_file));
-    }
-    memset(afile, 0, sizeof(struct osi_file));
-#ifdef AFS_CACHE_VNODE_PATH
-    if (ainode < 0) {
-      switch (ainode) {
-      case AFS_CACHE_CELLS_INODE:
-       snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CellItems");
-       break;
-      case AFS_CACHE_ITEMS_INODE:
-       snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "CacheItems");
-       break;
-      case AFS_CACHE_VOLUME_INODE:
-       snprintf(fname, 1024, "%s/%s", afs_cachebasedir, "VolumeItems");
-       break;
-      default:
-       osi_Panic("Invalid negative inode");
-      }
-    } else {
-      dummy = ainode / afs_numfilesperdir;
-      snprintf(fname, 1024, "%s/D%d/V%d", afs_cachebasedir, dummy, ainode);
-    }
+    struct file* filp;
 
-    code = osi_lookupname(fname, AFS_UIOSYS, 0, &dp);
-    if (code) {
-       osi_Panic("Failed cache file lookup: %s in UFSOpen\n", fname);
-    }
-    tip = dp->d_inode;
-#else
-    tip = iget(afs_cacheSBp, (u_long) ainode);
+#if !defined(LINUX_USE_FH)
+    tip = iget(afs_cacheSBp, ainode->ufs);
     if (!tip)
-       osi_Panic("Can't get inode %d\n", ainode);
+       osi_Panic("Can't get inode %d\n", (int) ainode->ufs);
 
     dp = d_alloc_anon(tip);
-    if (!dp) 
-           osi_Panic("Can't get dentry for inode %d\n", ainode);          
+#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");
+    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)
+    /* Use stashed credentials - prevent selinux/apparmor problems  */
+    filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR, cache_creds);
+#else
     filp = dentry_open(dp, mntget(afs_cacheMnt), O_RDWR);
+#endif
     if (IS_ERR(filp))
-       osi_Panic("Can't open inode %d\n", ainode);
-    afile->filp = filp;
-    afile->size = FILE_INODE(filp)->i_size;
-    AFS_GLOCK();
-    afile->offset = 0;
-    afile->proc = (int (*)())0;
-    afile->inum = ainode;      /* for hint validity checking */
-    return (void *)afile;
-}
+#if defined(LINUX_USE_FH)
+       osi_Panic("Can't open file: %d\n", PTR_ERR(filp));
 #else
+       osi_Panic("Can't open inode %d\n", (int) ainode->ufs);
+#endif
+    return filp;
+}
+
 void *
-osi_UFSOpen(afs_int32 ainode)
+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;
     }
@@ -133,56 +88,75 @@ osi_UFSOpen(afs_int32 ainode)
     AFS_GUNLOCK();
     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, (u_long) ainode);
-    if (!tip)
-       osi_Panic("Can't get inode %d\n", ainode);
-    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);
-    afile->size = tip->i_size;
+
+    afile->filp = afs_linux_raw_open(ainode);
+    afile->size = i_size_read(FILE_INODE(afile->filp));
     AFS_GLOCK();
     afile->offset = 0;
     afile->proc = (int (*)())0;
-    afile->inum = ainode;      /* for hint validity checking */
     return (void *)afile;
 }
+
+#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).
+ * The cache has to live on a single filesystem with uniform file 
+ * handles, otherwise we panic.
+ */
+void osi_get_fh(struct dentry *dp, afs_ufs_dcache_id_t *ainode) {
+    int max_len;
+    int type;
+
+    if (cache_fh_len > 0)
+       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;
+    }
+}
+#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)
 {
-    register afs_int32 code;
     AFS_STATCNT(osi_Stat);
-    MObtainWriteLock(&afs_xosi, 320);
-    astat->size = OSIFILE_INODE(afile)->i_size;
-#if defined(AFS_LINUX26_ENV)
+    ObtainWriteLock(&afs_xosi, 320);
+    astat->size = i_size_read(OSIFILE_INODE(afile));
     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;
+
+    ReleaseWriteLock(&afs_xosi);
+    return 0;
 }
 
-#ifdef AFS_LINUX26_ENV
 int
 osi_UFSClose(register struct osi_file *afile)
 {
@@ -196,24 +170,6 @@ osi_UFSClose(register struct osi_file *afile)
     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);
-    return 0;
-}
-#endif
 
 int
 osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
@@ -231,55 +187,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);
+    ObtainWriteLock(&afs_xosi, 321);
     AFS_GUNLOCK();
-#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
+#ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
+    down_write(&inode->i_alloc_sem);
+#endif
     newattrs.ia_size = asize;
     newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
-#if defined(AFS_LINUX24_ENV)
     newattrs.ia_ctime = CURRENT_TIME;
 
     /* avoid notify_change() since it wants to update dentry->d_parent */
     lock_kernel();
     code = inode_change_ok(inode, &newattrs);
-    if (!code)
+    if (!code) {
 #ifdef INODE_SETATTR_NOT_VOID
-       code = inode_setattr(inode, &newattrs);
+       if (inode->i_op && inode->i_op->setattr)
+           code = inode->i_op->setattr(afile->filp->f_dentry, &newattrs);
+       else
+           code = inode_setattr(inode, &newattrs);
 #else
-       inode_setattr(inode, &newattrs);
+        inode_setattr(inode, &newattrs);
 #endif
+    }
     unlock_kernel();
     if (!code)
        truncate_inode_pages(&inode->i_data, asize);
-#else
-    inode->i_size = 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_ALLOC_SEM
+    up_write(&inode->i_alloc_sem);
+#endif
 #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_GLOCK();
-    MReleaseWriteLock(&afs_xosi);
+    ReleaseWriteLock(&afs_xosi);
     return code;
 }
 
@@ -392,8 +340,8 @@ int
 osi_InitCacheInfo(char *aname)
 {
     int code;
+    extern afs_dcache_id_t cacheInode;
     struct dentry *dp;
-    extern ino_t cacheInode;
     extern struct osi_dev cacheDev;
     extern afs_int32 afs_fsfragsize;
     extern struct super_block *afs_cacheSBp;
@@ -402,7 +350,7 @@ osi_InitCacheInfo(char *aname)
     if (code)
        return ENOENT;
 
-    cacheInode = dp->d_inode->i_ino;
+    osi_get_fh(dp, &cacheInode.ufs);
     cacheDev.dev = dp->d_inode->i_sb->s_dev;
     afs_fsfragsize = dp->d_inode->i_sb->s_blocksize - 1;
     afs_cacheSBp = dp->d_inode->i_sb;
@@ -423,11 +371,7 @@ osi_InitCacheInfo(char *aname)
 int
 osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
 {
-#ifdef AFS_LINUX26_ENV
     struct file *filp = osifile->filp;
-#else
-    struct file *filp = &osifile->file;
-#endif
     KERNEL_SPACE_DECL;
     int code = 0;
     struct iovec *iov;
@@ -443,7 +387,8 @@ osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
     /* 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;
+           code = -1;
+           goto out;
     } else
        filp->f_pos = uiop->uio_offset;
 
@@ -481,6 +426,7 @@ osi_rdwr(struct osi_file *osifile, uio_t * uiop, int rw)
        code = 0;
     }
 
+out:
     if (uiop->uio_seg == AFS_UIOSYS)
        TO_KERNEL_SPACE();