Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Inode operations for Coda filesystem
0004  * Original version: (C) 1996 P. Braam and M. Callahan
0005  * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University
0006  * 
0007  * Carnegie Mellon encourages users to contribute improvements to
0008  * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
0009  */
0010 
0011 #include <linux/types.h>
0012 #include <linux/kernel.h>
0013 #include <linux/time.h>
0014 #include <linux/fs.h>
0015 #include <linux/stat.h>
0016 #include <linux/errno.h>
0017 #include <linux/uaccess.h>
0018 #include <linux/string.h>
0019 
0020 #include <linux/coda.h>
0021 #include "coda_psdev.h"
0022 #include "coda_linux.h"
0023 
0024 /* initialize the debugging variables */
0025 int coda_fake_statfs;
0026 
0027 /* print a fid */
0028 char * coda_f2s(struct CodaFid *f)
0029 {
0030     static char s[60];
0031 
0032     sprintf(s, "(%08x.%08x.%08x.%08x)", f->opaque[0], f->opaque[1], f->opaque[2], f->opaque[3]);
0033 
0034     return s;
0035 }
0036 
0037 /* recognize special .CONTROL name */
0038 int coda_iscontrol(const char *name, size_t length)
0039 {
0040     return ((CODA_CONTROLLEN == length) && 
0041                 (strncmp(name, CODA_CONTROL, CODA_CONTROLLEN) == 0));
0042 }
0043 
0044 unsigned short coda_flags_to_cflags(unsigned short flags)
0045 {
0046     unsigned short coda_flags = 0;
0047     
0048     if ((flags & O_ACCMODE) == O_RDONLY)
0049         coda_flags |= C_O_READ;
0050 
0051     if ((flags & O_ACCMODE) == O_RDWR)
0052         coda_flags |= C_O_READ | C_O_WRITE;
0053 
0054     if ((flags & O_ACCMODE) == O_WRONLY)
0055         coda_flags |= C_O_WRITE;
0056 
0057     if (flags & O_TRUNC)
0058         coda_flags |= C_O_TRUNC;
0059 
0060     if (flags & O_CREAT)
0061         coda_flags |= C_O_CREAT;
0062 
0063     if (flags & O_EXCL)
0064         coda_flags |= C_O_EXCL;
0065 
0066     return coda_flags;
0067 }
0068 
0069 static struct timespec64 coda_to_timespec64(struct coda_timespec ts)
0070 {
0071     struct timespec64 ts64 = {
0072         .tv_sec = ts.tv_sec,
0073         .tv_nsec = ts.tv_nsec,
0074     };
0075 
0076     return ts64;
0077 }
0078 
0079 static struct coda_timespec timespec64_to_coda(struct timespec64 ts64)
0080 {
0081     struct coda_timespec ts = {
0082         .tv_sec = ts64.tv_sec,
0083         .tv_nsec = ts64.tv_nsec,
0084     };
0085 
0086     return ts;
0087 }
0088 
0089 /* utility functions below */
0090 umode_t coda_inode_type(struct coda_vattr *attr)
0091 {
0092     switch (attr->va_type) {
0093     case C_VREG:
0094         return S_IFREG;
0095     case C_VDIR:
0096         return S_IFDIR;
0097     case C_VLNK:
0098         return S_IFLNK;
0099     case C_VNON:
0100     default:
0101         return 0;
0102     }
0103 }
0104 
0105 void coda_vattr_to_iattr(struct inode *inode, struct coda_vattr *attr)
0106 {
0107     /* inode's i_flags, i_ino are set by iget
0108      * XXX: is this all we need ??
0109      */
0110     umode_t inode_type = coda_inode_type(attr);
0111     inode->i_mode |= inode_type;
0112 
0113     if (attr->va_mode != (u_short) -1)
0114             inode->i_mode = attr->va_mode | inode_type;
0115         if (attr->va_uid != -1) 
0116             inode->i_uid = make_kuid(&init_user_ns, (uid_t) attr->va_uid);
0117         if (attr->va_gid != -1)
0118             inode->i_gid = make_kgid(&init_user_ns, (gid_t) attr->va_gid);
0119     if (attr->va_nlink != -1)
0120         set_nlink(inode, attr->va_nlink);
0121     if (attr->va_size != -1)
0122             inode->i_size = attr->va_size;
0123     if (attr->va_size != -1)
0124         inode->i_blocks = (attr->va_size + 511) >> 9;
0125     if (attr->va_atime.tv_sec != -1) 
0126         inode->i_atime = coda_to_timespec64(attr->va_atime);
0127     if (attr->va_mtime.tv_sec != -1)
0128         inode->i_mtime = coda_to_timespec64(attr->va_mtime);
0129         if (attr->va_ctime.tv_sec != -1)
0130         inode->i_ctime = coda_to_timespec64(attr->va_ctime);
0131 }
0132 
0133 
0134 /* 
0135  * BSD sets attributes that need not be modified to -1. 
0136  * Linux uses the valid field to indicate what should be
0137  * looked at.  The BSD type field needs to be deduced from linux 
0138  * mode.
0139  * So we have to do some translations here.
0140  */
0141 
0142 void coda_iattr_to_vattr(struct iattr *iattr, struct coda_vattr *vattr)
0143 {
0144         unsigned int valid;
0145 
0146         /* clean out */        
0147     vattr->va_mode = -1;
0148         vattr->va_uid = (vuid_t) -1; 
0149         vattr->va_gid = (vgid_t) -1;
0150         vattr->va_size = (off_t) -1;
0151     vattr->va_atime.tv_sec = (int64_t) -1;
0152     vattr->va_atime.tv_nsec = (long) -1;
0153     vattr->va_mtime.tv_sec = (int64_t) -1;
0154     vattr->va_mtime.tv_nsec = (long) -1;
0155     vattr->va_ctime.tv_sec = (int64_t) -1;
0156     vattr->va_ctime.tv_nsec = (long) -1;
0157         vattr->va_type = C_VNON;
0158     vattr->va_fileid = -1;
0159     vattr->va_gen = -1;
0160     vattr->va_bytes = -1;
0161     vattr->va_nlink = -1;
0162     vattr->va_blocksize = -1;
0163     vattr->va_rdev = -1;
0164         vattr->va_flags = 0;
0165 
0166         /* determine the type */
0167 #if 0
0168         mode = iattr->ia_mode;
0169                 if ( S_ISDIR(mode) ) {
0170                 vattr->va_type = C_VDIR; 
0171         } else if ( S_ISREG(mode) ) {
0172                 vattr->va_type = C_VREG;
0173         } else if ( S_ISLNK(mode) ) {
0174                 vattr->va_type = C_VLNK;
0175         } else {
0176                 /* don't do others */
0177                 vattr->va_type = C_VNON;
0178         }
0179 #endif 
0180 
0181         /* set those vattrs that need change */
0182         valid = iattr->ia_valid;
0183         if ( valid & ATTR_MODE ) {
0184                 vattr->va_mode = iattr->ia_mode;
0185     }
0186         if ( valid & ATTR_UID ) {
0187                 vattr->va_uid = (vuid_t) from_kuid(&init_user_ns, iattr->ia_uid);
0188     }
0189         if ( valid & ATTR_GID ) {
0190                 vattr->va_gid = (vgid_t) from_kgid(&init_user_ns, iattr->ia_gid);
0191     }
0192         if ( valid & ATTR_SIZE ) {
0193                 vattr->va_size = iattr->ia_size;
0194     }
0195         if ( valid & ATTR_ATIME ) {
0196         vattr->va_atime = timespec64_to_coda(iattr->ia_atime);
0197     }
0198         if ( valid & ATTR_MTIME ) {
0199         vattr->va_mtime = timespec64_to_coda(iattr->ia_mtime);
0200     }
0201         if ( valid & ATTR_CTIME ) {
0202         vattr->va_ctime = timespec64_to_coda(iattr->ia_ctime);
0203     }
0204 }
0205