Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/fs/stat.c
0003  *
0004  *  Copyright (C) 1991, 1992  Linus Torvalds
0005  */
0006 
0007 #include <linux/export.h>
0008 #include <linux/mm.h>
0009 #include <linux/errno.h>
0010 #include <linux/file.h>
0011 #include <linux/highuid.h>
0012 #include <linux/fs.h>
0013 #include <linux/namei.h>
0014 #include <linux/security.h>
0015 #include <linux/syscalls.h>
0016 #include <linux/pagemap.h>
0017 
0018 #include <linux/uaccess.h>
0019 #include <asm/unistd.h>
0020 
0021 void generic_fillattr(struct inode *inode, struct kstat *stat)
0022 {
0023     stat->dev = inode->i_sb->s_dev;
0024     stat->ino = inode->i_ino;
0025     stat->mode = inode->i_mode;
0026     stat->nlink = inode->i_nlink;
0027     stat->uid = inode->i_uid;
0028     stat->gid = inode->i_gid;
0029     stat->rdev = inode->i_rdev;
0030     stat->size = i_size_read(inode);
0031     stat->atime = inode->i_atime;
0032     stat->mtime = inode->i_mtime;
0033     stat->ctime = inode->i_ctime;
0034     stat->blksize = (1 << inode->i_blkbits);
0035     stat->blocks = inode->i_blocks;
0036 }
0037 
0038 EXPORT_SYMBOL(generic_fillattr);
0039 
0040 /**
0041  * vfs_getattr_nosec - getattr without security checks
0042  * @path: file to get attributes from
0043  * @stat: structure to return attributes in
0044  *
0045  * Get attributes without calling security_inode_getattr.
0046  *
0047  * Currently the only caller other than vfs_getattr is internal to the
0048  * filehandle lookup code, which uses only the inode number and returns
0049  * no attributes to any user.  Any other code probably wants
0050  * vfs_getattr.
0051  */
0052 int vfs_getattr_nosec(struct path *path, struct kstat *stat)
0053 {
0054     struct inode *inode = d_backing_inode(path->dentry);
0055 
0056     if (inode->i_op->getattr)
0057         return inode->i_op->getattr(path->mnt, path->dentry, stat);
0058 
0059     generic_fillattr(inode, stat);
0060     return 0;
0061 }
0062 
0063 EXPORT_SYMBOL(vfs_getattr_nosec);
0064 
0065 int vfs_getattr(struct path *path, struct kstat *stat)
0066 {
0067     int retval;
0068 
0069     retval = security_inode_getattr(path);
0070     if (retval)
0071         return retval;
0072     return vfs_getattr_nosec(path, stat);
0073 }
0074 
0075 EXPORT_SYMBOL(vfs_getattr);
0076 
0077 int vfs_fstat(unsigned int fd, struct kstat *stat)
0078 {
0079     struct fd f = fdget_raw(fd);
0080     int error = -EBADF;
0081 
0082     if (f.file) {
0083         error = vfs_getattr(&f.file->f_path, stat);
0084         fdput(f);
0085     }
0086     return error;
0087 }
0088 EXPORT_SYMBOL(vfs_fstat);
0089 
0090 int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
0091         int flag)
0092 {
0093     struct path path;
0094     int error = -EINVAL;
0095     unsigned int lookup_flags = 0;
0096 
0097     if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
0098               AT_EMPTY_PATH)) != 0)
0099         goto out;
0100 
0101     if (!(flag & AT_SYMLINK_NOFOLLOW))
0102         lookup_flags |= LOOKUP_FOLLOW;
0103     if (flag & AT_EMPTY_PATH)
0104         lookup_flags |= LOOKUP_EMPTY;
0105 retry:
0106     error = user_path_at(dfd, filename, lookup_flags, &path);
0107     if (error)
0108         goto out;
0109 
0110     error = vfs_getattr(&path, stat);
0111     path_put(&path);
0112     if (retry_estale(error, lookup_flags)) {
0113         lookup_flags |= LOOKUP_REVAL;
0114         goto retry;
0115     }
0116 out:
0117     return error;
0118 }
0119 EXPORT_SYMBOL(vfs_fstatat);
0120 
0121 int vfs_stat(const char __user *name, struct kstat *stat)
0122 {
0123     return vfs_fstatat(AT_FDCWD, name, stat, 0);
0124 }
0125 EXPORT_SYMBOL(vfs_stat);
0126 
0127 int vfs_lstat(const char __user *name, struct kstat *stat)
0128 {
0129     return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
0130 }
0131 EXPORT_SYMBOL(vfs_lstat);
0132 
0133 
0134 #ifdef __ARCH_WANT_OLD_STAT
0135 
0136 /*
0137  * For backward compatibility?  Maybe this should be moved
0138  * into arch/i386 instead?
0139  */
0140 static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
0141 {
0142     static int warncount = 5;
0143     struct __old_kernel_stat tmp;
0144     
0145     if (warncount > 0) {
0146         warncount--;
0147         printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
0148             current->comm);
0149     } else if (warncount < 0) {
0150         /* it's laughable, but... */
0151         warncount = 0;
0152     }
0153 
0154     memset(&tmp, 0, sizeof(struct __old_kernel_stat));
0155     tmp.st_dev = old_encode_dev(stat->dev);
0156     tmp.st_ino = stat->ino;
0157     if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
0158         return -EOVERFLOW;
0159     tmp.st_mode = stat->mode;
0160     tmp.st_nlink = stat->nlink;
0161     if (tmp.st_nlink != stat->nlink)
0162         return -EOVERFLOW;
0163     SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
0164     SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
0165     tmp.st_rdev = old_encode_dev(stat->rdev);
0166 #if BITS_PER_LONG == 32
0167     if (stat->size > MAX_NON_LFS)
0168         return -EOVERFLOW;
0169 #endif  
0170     tmp.st_size = stat->size;
0171     tmp.st_atime = stat->atime.tv_sec;
0172     tmp.st_mtime = stat->mtime.tv_sec;
0173     tmp.st_ctime = stat->ctime.tv_sec;
0174     return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
0175 }
0176 
0177 SYSCALL_DEFINE2(stat, const char __user *, filename,
0178         struct __old_kernel_stat __user *, statbuf)
0179 {
0180     struct kstat stat;
0181     int error;
0182 
0183     error = vfs_stat(filename, &stat);
0184     if (error)
0185         return error;
0186 
0187     return cp_old_stat(&stat, statbuf);
0188 }
0189 
0190 SYSCALL_DEFINE2(lstat, const char __user *, filename,
0191         struct __old_kernel_stat __user *, statbuf)
0192 {
0193     struct kstat stat;
0194     int error;
0195 
0196     error = vfs_lstat(filename, &stat);
0197     if (error)
0198         return error;
0199 
0200     return cp_old_stat(&stat, statbuf);
0201 }
0202 
0203 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
0204 {
0205     struct kstat stat;
0206     int error = vfs_fstat(fd, &stat);
0207 
0208     if (!error)
0209         error = cp_old_stat(&stat, statbuf);
0210 
0211     return error;
0212 }
0213 
0214 #endif /* __ARCH_WANT_OLD_STAT */
0215 
0216 #if BITS_PER_LONG == 32
0217 #  define choose_32_64(a,b) a
0218 #else
0219 #  define choose_32_64(a,b) b
0220 #endif
0221 
0222 #define valid_dev(x)  choose_32_64(old_valid_dev(x),true)
0223 #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
0224 
0225 #ifndef INIT_STRUCT_STAT_PADDING
0226 #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
0227 #endif
0228 
0229 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
0230 {
0231     struct stat tmp;
0232 
0233     if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
0234         return -EOVERFLOW;
0235 #if BITS_PER_LONG == 32
0236     if (stat->size > MAX_NON_LFS)
0237         return -EOVERFLOW;
0238 #endif
0239 
0240     INIT_STRUCT_STAT_PADDING(tmp);
0241     tmp.st_dev = encode_dev(stat->dev);
0242     tmp.st_ino = stat->ino;
0243     if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
0244         return -EOVERFLOW;
0245     tmp.st_mode = stat->mode;
0246     tmp.st_nlink = stat->nlink;
0247     if (tmp.st_nlink != stat->nlink)
0248         return -EOVERFLOW;
0249     SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
0250     SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
0251     tmp.st_rdev = encode_dev(stat->rdev);
0252     tmp.st_size = stat->size;
0253     tmp.st_atime = stat->atime.tv_sec;
0254     tmp.st_mtime = stat->mtime.tv_sec;
0255     tmp.st_ctime = stat->ctime.tv_sec;
0256 #ifdef STAT_HAVE_NSEC
0257     tmp.st_atime_nsec = stat->atime.tv_nsec;
0258     tmp.st_mtime_nsec = stat->mtime.tv_nsec;
0259     tmp.st_ctime_nsec = stat->ctime.tv_nsec;
0260 #endif
0261     tmp.st_blocks = stat->blocks;
0262     tmp.st_blksize = stat->blksize;
0263     return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
0264 }
0265 
0266 SYSCALL_DEFINE2(newstat, const char __user *, filename,
0267         struct stat __user *, statbuf)
0268 {
0269     struct kstat stat;
0270     int error = vfs_stat(filename, &stat);
0271 
0272     if (error)
0273         return error;
0274     return cp_new_stat(&stat, statbuf);
0275 }
0276 
0277 SYSCALL_DEFINE2(newlstat, const char __user *, filename,
0278         struct stat __user *, statbuf)
0279 {
0280     struct kstat stat;
0281     int error;
0282 
0283     error = vfs_lstat(filename, &stat);
0284     if (error)
0285         return error;
0286 
0287     return cp_new_stat(&stat, statbuf);
0288 }
0289 
0290 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
0291 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
0292         struct stat __user *, statbuf, int, flag)
0293 {
0294     struct kstat stat;
0295     int error;
0296 
0297     error = vfs_fstatat(dfd, filename, &stat, flag);
0298     if (error)
0299         return error;
0300     return cp_new_stat(&stat, statbuf);
0301 }
0302 #endif
0303 
0304 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
0305 {
0306     struct kstat stat;
0307     int error = vfs_fstat(fd, &stat);
0308 
0309     if (!error)
0310         error = cp_new_stat(&stat, statbuf);
0311 
0312     return error;
0313 }
0314 
0315 SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
0316         char __user *, buf, int, bufsiz)
0317 {
0318     struct path path;
0319     int error;
0320     int empty = 0;
0321     unsigned int lookup_flags = LOOKUP_EMPTY;
0322 
0323     if (bufsiz <= 0)
0324         return -EINVAL;
0325 
0326 retry:
0327     error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
0328     if (!error) {
0329         struct inode *inode = d_backing_inode(path.dentry);
0330 
0331         error = empty ? -ENOENT : -EINVAL;
0332         /*
0333          * AFS mountpoints allow readlink(2) but are not symlinks
0334          */
0335         if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
0336             error = security_inode_readlink(path.dentry);
0337             if (!error) {
0338                 touch_atime(&path);
0339                 error = vfs_readlink(path.dentry, buf, bufsiz);
0340             }
0341         }
0342         path_put(&path);
0343         if (retry_estale(error, lookup_flags)) {
0344             lookup_flags |= LOOKUP_REVAL;
0345             goto retry;
0346         }
0347     }
0348     return error;
0349 }
0350 
0351 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
0352         int, bufsiz)
0353 {
0354     return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
0355 }
0356 
0357 
0358 /* ---------- LFS-64 ----------- */
0359 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
0360 
0361 #ifndef INIT_STRUCT_STAT64_PADDING
0362 #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
0363 #endif
0364 
0365 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
0366 {
0367     struct stat64 tmp;
0368 
0369     INIT_STRUCT_STAT64_PADDING(tmp);
0370 #ifdef CONFIG_MIPS
0371     /* mips has weird padding, so we don't get 64 bits there */
0372     tmp.st_dev = new_encode_dev(stat->dev);
0373     tmp.st_rdev = new_encode_dev(stat->rdev);
0374 #else
0375     tmp.st_dev = huge_encode_dev(stat->dev);
0376     tmp.st_rdev = huge_encode_dev(stat->rdev);
0377 #endif
0378     tmp.st_ino = stat->ino;
0379     if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
0380         return -EOVERFLOW;
0381 #ifdef STAT64_HAS_BROKEN_ST_INO
0382     tmp.__st_ino = stat->ino;
0383 #endif
0384     tmp.st_mode = stat->mode;
0385     tmp.st_nlink = stat->nlink;
0386     tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
0387     tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
0388     tmp.st_atime = stat->atime.tv_sec;
0389     tmp.st_atime_nsec = stat->atime.tv_nsec;
0390     tmp.st_mtime = stat->mtime.tv_sec;
0391     tmp.st_mtime_nsec = stat->mtime.tv_nsec;
0392     tmp.st_ctime = stat->ctime.tv_sec;
0393     tmp.st_ctime_nsec = stat->ctime.tv_nsec;
0394     tmp.st_size = stat->size;
0395     tmp.st_blocks = stat->blocks;
0396     tmp.st_blksize = stat->blksize;
0397     return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
0398 }
0399 
0400 SYSCALL_DEFINE2(stat64, const char __user *, filename,
0401         struct stat64 __user *, statbuf)
0402 {
0403     struct kstat stat;
0404     int error = vfs_stat(filename, &stat);
0405 
0406     if (!error)
0407         error = cp_new_stat64(&stat, statbuf);
0408 
0409     return error;
0410 }
0411 
0412 SYSCALL_DEFINE2(lstat64, const char __user *, filename,
0413         struct stat64 __user *, statbuf)
0414 {
0415     struct kstat stat;
0416     int error = vfs_lstat(filename, &stat);
0417 
0418     if (!error)
0419         error = cp_new_stat64(&stat, statbuf);
0420 
0421     return error;
0422 }
0423 
0424 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
0425 {
0426     struct kstat stat;
0427     int error = vfs_fstat(fd, &stat);
0428 
0429     if (!error)
0430         error = cp_new_stat64(&stat, statbuf);
0431 
0432     return error;
0433 }
0434 
0435 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
0436         struct stat64 __user *, statbuf, int, flag)
0437 {
0438     struct kstat stat;
0439     int error;
0440 
0441     error = vfs_fstatat(dfd, filename, &stat, flag);
0442     if (error)
0443         return error;
0444     return cp_new_stat64(&stat, statbuf);
0445 }
0446 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
0447 
0448 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
0449 void __inode_add_bytes(struct inode *inode, loff_t bytes)
0450 {
0451     inode->i_blocks += bytes >> 9;
0452     bytes &= 511;
0453     inode->i_bytes += bytes;
0454     if (inode->i_bytes >= 512) {
0455         inode->i_blocks++;
0456         inode->i_bytes -= 512;
0457     }
0458 }
0459 
0460 void inode_add_bytes(struct inode *inode, loff_t bytes)
0461 {
0462     spin_lock(&inode->i_lock);
0463     __inode_add_bytes(inode, bytes);
0464     spin_unlock(&inode->i_lock);
0465 }
0466 
0467 EXPORT_SYMBOL(inode_add_bytes);
0468 
0469 void __inode_sub_bytes(struct inode *inode, loff_t bytes)
0470 {
0471     inode->i_blocks -= bytes >> 9;
0472     bytes &= 511;
0473     if (inode->i_bytes < bytes) {
0474         inode->i_blocks--;
0475         inode->i_bytes += 512;
0476     }
0477     inode->i_bytes -= bytes;
0478 }
0479 
0480 EXPORT_SYMBOL(__inode_sub_bytes);
0481 
0482 void inode_sub_bytes(struct inode *inode, loff_t bytes)
0483 {
0484     spin_lock(&inode->i_lock);
0485     __inode_sub_bytes(inode, bytes);
0486     spin_unlock(&inode->i_lock);
0487 }
0488 
0489 EXPORT_SYMBOL(inode_sub_bytes);
0490 
0491 loff_t inode_get_bytes(struct inode *inode)
0492 {
0493     loff_t ret;
0494 
0495     spin_lock(&inode->i_lock);
0496     ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
0497     spin_unlock(&inode->i_lock);
0498     return ret;
0499 }
0500 
0501 EXPORT_SYMBOL(inode_get_bytes);
0502 
0503 void inode_set_bytes(struct inode *inode, loff_t bytes)
0504 {
0505     /* Caller is here responsible for sufficient locking
0506      * (ie. inode->i_lock) */
0507     inode->i_blocks = bytes >> 9;
0508     inode->i_bytes = bytes & 511;
0509 }
0510 
0511 EXPORT_SYMBOL(inode_set_bytes);