4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/file.h>
11 #include <linux/highuid.h>
13 #include <linux/namei.h>
14 #include <linux/security.h>
15 #include <linux/syscalls.h>
16 #include <linux/pagemap.h>
18 #include <asm/uaccess.h>
19 #include <asm/unistd.h>
21 void generic_fillattr(struct inode *inode, struct kstat *stat)
23 stat->dev = inode->i_sb->s_dev;
24 stat->ino = inode->i_ino;
25 stat->mode = inode->i_mode;
26 stat->nlink = inode->i_nlink;
27 stat->uid = inode->i_uid;
28 stat->gid = inode->i_gid;
29 stat->rdev = inode->i_rdev;
30 stat->atime = inode->i_atime;
31 stat->mtime = inode->i_mtime;
32 stat->ctime = inode->i_ctime;
33 stat->size = i_size_read(inode);
34 stat->blocks = inode->i_blocks;
35 stat->blksize = (1 << inode->i_blkbits);
38 EXPORT_SYMBOL(generic_fillattr);
40 int vfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
42 struct inode *inode = dentry->d_inode;
45 retval = security_inode_getattr(mnt, dentry);
49 if (inode->i_op->getattr)
50 return inode->i_op->getattr(mnt, dentry, stat);
52 generic_fillattr(inode, stat);
56 EXPORT_SYMBOL(vfs_getattr);
58 int vfs_fstat(unsigned int fd, struct kstat *stat)
60 struct file *f = fget(fd);
64 error = vfs_getattr(f->f_path.mnt, f->f_path.dentry, stat);
69 EXPORT_SYMBOL(vfs_fstat);
71 int vfs_fstatat(int dfd, char __user *filename, struct kstat *stat, int flag)
77 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
80 if (!(flag & AT_SYMLINK_NOFOLLOW))
81 lookup_flags |= LOOKUP_FOLLOW;
83 error = user_path_at(dfd, filename, lookup_flags, &path);
87 error = vfs_getattr(path.mnt, path.dentry, stat);
92 EXPORT_SYMBOL(vfs_fstatat);
94 int vfs_stat(char __user *name, struct kstat *stat)
96 return vfs_fstatat(AT_FDCWD, name, stat, 0);
98 EXPORT_SYMBOL(vfs_stat);
100 int vfs_lstat(char __user *name, struct kstat *stat)
102 return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
104 EXPORT_SYMBOL(vfs_lstat);
107 #ifdef __ARCH_WANT_OLD_STAT
110 * For backward compatibility? Maybe this should be moved
111 * into arch/i386 instead?
113 static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
115 static int warncount = 5;
116 struct __old_kernel_stat tmp;
120 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
122 } else if (warncount < 0) {
123 /* it's laughable, but... */
127 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
128 tmp.st_dev = old_encode_dev(stat->dev);
129 tmp.st_ino = stat->ino;
130 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
132 tmp.st_mode = stat->mode;
133 tmp.st_nlink = stat->nlink;
134 if (tmp.st_nlink != stat->nlink)
136 SET_UID(tmp.st_uid, stat->uid);
137 SET_GID(tmp.st_gid, stat->gid);
138 tmp.st_rdev = old_encode_dev(stat->rdev);
139 #if BITS_PER_LONG == 32
140 if (stat->size > MAX_NON_LFS)
143 tmp.st_size = stat->size;
144 tmp.st_atime = stat->atime.tv_sec;
145 tmp.st_mtime = stat->mtime.tv_sec;
146 tmp.st_ctime = stat->ctime.tv_sec;
147 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
150 SYSCALL_DEFINE2(stat, char __user *, filename, struct __old_kernel_stat __user *, statbuf)
155 error = vfs_stat(filename, &stat);
159 return cp_old_stat(&stat, statbuf);
162 SYSCALL_DEFINE2(lstat, char __user *, filename, struct __old_kernel_stat __user *, statbuf)
167 error = vfs_lstat(filename, &stat);
171 return cp_old_stat(&stat, statbuf);
174 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
177 int error = vfs_fstat(fd, &stat);
180 error = cp_old_stat(&stat, statbuf);
185 #endif /* __ARCH_WANT_OLD_STAT */
187 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
191 #if BITS_PER_LONG == 32
192 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
195 if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
199 memset(&tmp, 0, sizeof(tmp));
200 #if BITS_PER_LONG == 32
201 tmp.st_dev = old_encode_dev(stat->dev);
203 tmp.st_dev = new_encode_dev(stat->dev);
205 tmp.st_ino = stat->ino;
206 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
208 tmp.st_mode = stat->mode;
209 tmp.st_nlink = stat->nlink;
210 if (tmp.st_nlink != stat->nlink)
212 SET_UID(tmp.st_uid, stat->uid);
213 SET_GID(tmp.st_gid, stat->gid);
214 #if BITS_PER_LONG == 32
215 tmp.st_rdev = old_encode_dev(stat->rdev);
217 tmp.st_rdev = new_encode_dev(stat->rdev);
219 #if BITS_PER_LONG == 32
220 if (stat->size > MAX_NON_LFS)
223 tmp.st_size = stat->size;
224 tmp.st_atime = stat->atime.tv_sec;
225 tmp.st_mtime = stat->mtime.tv_sec;
226 tmp.st_ctime = stat->ctime.tv_sec;
227 #ifdef STAT_HAVE_NSEC
228 tmp.st_atime_nsec = stat->atime.tv_nsec;
229 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
230 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
232 tmp.st_blocks = stat->blocks;
233 tmp.st_blksize = stat->blksize;
234 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
237 SYSCALL_DEFINE2(newstat, char __user *, filename, struct stat __user *, statbuf)
240 int error = vfs_stat(filename, &stat);
244 return cp_new_stat(&stat, statbuf);
247 SYSCALL_DEFINE2(newlstat, char __user *, filename, struct stat __user *, statbuf)
252 error = vfs_lstat(filename, &stat);
256 return cp_new_stat(&stat, statbuf);
259 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
260 SYSCALL_DEFINE4(newfstatat, int, dfd, char __user *, filename,
261 struct stat __user *, statbuf, int, flag)
266 error = vfs_fstatat(dfd, filename, &stat, flag);
269 return cp_new_stat(&stat, statbuf);
273 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
276 int error = vfs_fstat(fd, &stat);
279 error = cp_new_stat(&stat, statbuf);
284 SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
285 char __user *, buf, int, bufsiz)
293 error = user_path_at(dfd, pathname, 0, &path);
295 struct inode *inode = path.dentry->d_inode;
298 if (inode->i_op->readlink) {
299 error = security_inode_readlink(path.dentry);
301 touch_atime(path.mnt, path.dentry);
302 error = inode->i_op->readlink(path.dentry,
311 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
314 return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
318 /* ---------- LFS-64 ----------- */
319 #ifdef __ARCH_WANT_STAT64
321 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
325 memset(&tmp, 0, sizeof(struct stat64));
327 /* mips has weird padding, so we don't get 64 bits there */
328 if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
330 tmp.st_dev = new_encode_dev(stat->dev);
331 tmp.st_rdev = new_encode_dev(stat->rdev);
333 tmp.st_dev = huge_encode_dev(stat->dev);
334 tmp.st_rdev = huge_encode_dev(stat->rdev);
336 tmp.st_ino = stat->ino;
337 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
339 #ifdef STAT64_HAS_BROKEN_ST_INO
340 tmp.__st_ino = stat->ino;
342 tmp.st_mode = stat->mode;
343 tmp.st_nlink = stat->nlink;
344 tmp.st_uid = stat->uid;
345 tmp.st_gid = stat->gid;
346 tmp.st_atime = stat->atime.tv_sec;
347 tmp.st_atime_nsec = stat->atime.tv_nsec;
348 tmp.st_mtime = stat->mtime.tv_sec;
349 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
350 tmp.st_ctime = stat->ctime.tv_sec;
351 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
352 tmp.st_size = stat->size;
353 tmp.st_blocks = stat->blocks;
354 tmp.st_blksize = stat->blksize;
355 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
358 SYSCALL_DEFINE2(stat64, char __user *, filename, struct stat64 __user *, statbuf)
361 int error = vfs_stat(filename, &stat);
364 error = cp_new_stat64(&stat, statbuf);
369 SYSCALL_DEFINE2(lstat64, char __user *, filename, struct stat64 __user *, statbuf)
372 int error = vfs_lstat(filename, &stat);
375 error = cp_new_stat64(&stat, statbuf);
380 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
383 int error = vfs_fstat(fd, &stat);
386 error = cp_new_stat64(&stat, statbuf);
391 SYSCALL_DEFINE4(fstatat64, int, dfd, char __user *, filename,
392 struct stat64 __user *, statbuf, int, flag)
397 error = vfs_fstatat(dfd, filename, &stat, flag);
400 return cp_new_stat64(&stat, statbuf);
402 #endif /* __ARCH_WANT_STAT64 */
404 void inode_add_bytes(struct inode *inode, loff_t bytes)
406 spin_lock(&inode->i_lock);
407 inode->i_blocks += bytes >> 9;
409 inode->i_bytes += bytes;
410 if (inode->i_bytes >= 512) {
412 inode->i_bytes -= 512;
414 spin_unlock(&inode->i_lock);
417 EXPORT_SYMBOL(inode_add_bytes);
419 void inode_sub_bytes(struct inode *inode, loff_t bytes)
421 spin_lock(&inode->i_lock);
422 inode->i_blocks -= bytes >> 9;
424 if (inode->i_bytes < bytes) {
426 inode->i_bytes += 512;
428 inode->i_bytes -= bytes;
429 spin_unlock(&inode->i_lock);
432 EXPORT_SYMBOL(inode_sub_bytes);
434 loff_t inode_get_bytes(struct inode *inode)
438 spin_lock(&inode->i_lock);
439 ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
440 spin_unlock(&inode->i_lock);
444 EXPORT_SYMBOL(inode_get_bytes);
446 void inode_set_bytes(struct inode *inode, loff_t bytes)
448 /* Caller is here responsible for sufficient locking
449 * (ie. inode->i_lock) */
450 inode->i_blocks = bytes >> 9;
451 inode->i_bytes = bytes & 511;
454 EXPORT_SYMBOL(inode_set_bytes);