1/*
2 *  linux/fs/stat.c
3 *
4 *  Copyright (C) 1991, 1992  Linus Torvalds
5 */
6
7#include <linux/module.h>
8#include <linux/mm.h>
9#include <linux/errno.h>
10#include <linux/file.h>
11#include <linux/highuid.h>
12#include <linux/fs.h>
13#include <linux/namei.h>
14#include <linux/security.h>
15#include <linux/syscalls.h>
16#include <linux/pagemap.h>
17
18#include <asm/uaccess.h>
19#include <asm/unistd.h>
20
21void generic_fillattr(struct inode *inode, struct kstat *stat)
22{
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);
36}
37
38EXPORT_SYMBOL(generic_fillattr);
39
40int vfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
41{
42	struct inode *inode = dentry->d_inode;
43	int retval;
44
45	retval = security_inode_getattr(mnt, dentry);
46	if (retval)
47		return retval;
48
49	if (inode->i_op->getattr)
50		return inode->i_op->getattr(mnt, dentry, stat);
51
52	generic_fillattr(inode, stat);
53	return 0;
54}
55
56EXPORT_SYMBOL(vfs_getattr);
57
58int vfs_fstat(unsigned int fd, struct kstat *stat)
59{
60	struct file *f = fget(fd);
61	int error = -EBADF;
62
63	if (f) {
64		error = vfs_getattr(f->f_path.mnt, f->f_path.dentry, stat);
65		fput(f);
66	}
67	return error;
68}
69EXPORT_SYMBOL(vfs_fstat);
70
71int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
72		int flag)
73{
74	struct path path;
75	int error = -EINVAL;
76	int lookup_flags = 0;
77
78	if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
79		goto out;
80
81	if (!(flag & AT_SYMLINK_NOFOLLOW))
82		lookup_flags |= LOOKUP_FOLLOW;
83
84	error = user_path_at(dfd, filename, lookup_flags, &path);
85	if (error)
86		goto out;
87
88	error = vfs_getattr(path.mnt, path.dentry, stat);
89	path_put(&path);
90out:
91	return error;
92}
93EXPORT_SYMBOL(vfs_fstatat);
94
95int vfs_stat(const char __user *name, struct kstat *stat)
96{
97	return vfs_fstatat(AT_FDCWD, name, stat, 0);
98}
99EXPORT_SYMBOL(vfs_stat);
100
101int vfs_lstat(const char __user *name, struct kstat *stat)
102{
103	return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
104}
105EXPORT_SYMBOL(vfs_lstat);
106
107
108#ifdef __ARCH_WANT_OLD_STAT
109
110/*
111 * For backward compatibility?  Maybe this should be moved
112 * into arch/i386 instead?
113 */
114static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
115{
116	static int warncount = 5;
117	struct __old_kernel_stat tmp;
118
119	if (warncount > 0) {
120		warncount--;
121		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
122			current->comm);
123	} else if (warncount < 0) {
124		/* it's laughable, but... */
125		warncount = 0;
126	}
127
128	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
129	tmp.st_dev = old_encode_dev(stat->dev);
130	tmp.st_ino = stat->ino;
131	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
132		return -EOVERFLOW;
133	tmp.st_mode = stat->mode;
134	tmp.st_nlink = stat->nlink;
135	if (tmp.st_nlink != stat->nlink)
136		return -EOVERFLOW;
137	SET_UID(tmp.st_uid, stat->uid);
138	SET_GID(tmp.st_gid, stat->gid);
139	tmp.st_rdev = old_encode_dev(stat->rdev);
140    /* Foxconn modified start pling 12/04/2009 */
141    /* Remove large file size limitation */
142#if (!defined SAMBA_ENABLE)
143#if BITS_PER_LONG == 32
144	if (stat->size > MAX_NON_LFS)
145		return -EOVERFLOW;
146#endif
147#endif
148    /* Foxconn modified end pling 12/04/2009 */
149	tmp.st_size = stat->size;
150	tmp.st_atime = stat->atime.tv_sec;
151	tmp.st_mtime = stat->mtime.tv_sec;
152	tmp.st_ctime = stat->ctime.tv_sec;
153	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
154}
155
156SYSCALL_DEFINE2(stat, const char __user *, filename,
157		struct __old_kernel_stat __user *, statbuf)
158{
159	struct kstat stat;
160	int error;
161
162	error = vfs_stat(filename, &stat);
163	if (error)
164		return error;
165
166	return cp_old_stat(&stat, statbuf);
167}
168
169SYSCALL_DEFINE2(lstat, const char __user *, filename,
170		struct __old_kernel_stat __user *, statbuf)
171{
172	struct kstat stat;
173	int error;
174
175	error = vfs_lstat(filename, &stat);
176	if (error)
177		return error;
178
179	return cp_old_stat(&stat, statbuf);
180}
181
182SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
183{
184	struct kstat stat;
185	int error = vfs_fstat(fd, &stat);
186
187	if (!error)
188		error = cp_old_stat(&stat, statbuf);
189
190	return error;
191}
192
193#endif /* __ARCH_WANT_OLD_STAT */
194
195static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
196{
197	struct stat tmp;
198
199#if BITS_PER_LONG == 32
200	if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
201		return -EOVERFLOW;
202#else
203	if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
204		return -EOVERFLOW;
205#endif
206
207	memset(&tmp, 0, sizeof(tmp));
208#if BITS_PER_LONG == 32
209	tmp.st_dev = old_encode_dev(stat->dev);
210#else
211	tmp.st_dev = new_encode_dev(stat->dev);
212#endif
213	tmp.st_ino = stat->ino;
214	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
215		return -EOVERFLOW;
216	tmp.st_mode = stat->mode;
217	tmp.st_nlink = stat->nlink;
218	if (tmp.st_nlink != stat->nlink)
219		return -EOVERFLOW;
220	SET_UID(tmp.st_uid, stat->uid);
221	SET_GID(tmp.st_gid, stat->gid);
222#if BITS_PER_LONG == 32
223	tmp.st_rdev = old_encode_dev(stat->rdev);
224#else
225	tmp.st_rdev = new_encode_dev(stat->rdev);
226#endif
227    /* Foxconn modified start pling 12/04/2009 */
228    /* Remove large file size limitation */
229#if (!defined SAMBA_ENABLE)
230#if BITS_PER_LONG == 32
231	if (stat->size > MAX_NON_LFS)
232		return -EOVERFLOW;
233#endif
234#endif
235    /* Foxconn modified end pling 12/04/2009 */
236	tmp.st_size = stat->size;
237	tmp.st_atime = stat->atime.tv_sec;
238	tmp.st_mtime = stat->mtime.tv_sec;
239	tmp.st_ctime = stat->ctime.tv_sec;
240#ifdef STAT_HAVE_NSEC
241	tmp.st_atime_nsec = stat->atime.tv_nsec;
242	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
243	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
244#endif
245	tmp.st_blocks = stat->blocks;
246	tmp.st_blksize = stat->blksize;
247	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
248}
249
250SYSCALL_DEFINE2(newstat, const char __user *, filename,
251		struct stat __user *, statbuf)
252{
253	struct kstat stat;
254	int error = vfs_stat(filename, &stat);
255
256	if (error)
257		return error;
258	return cp_new_stat(&stat, statbuf);
259}
260
261SYSCALL_DEFINE2(newlstat, const char __user *, filename,
262		struct stat __user *, statbuf)
263{
264	struct kstat stat;
265	int error;
266
267	error = vfs_lstat(filename, &stat);
268	if (error)
269		return error;
270
271	return cp_new_stat(&stat, statbuf);
272}
273
274#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
275SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
276		struct stat __user *, statbuf, int, flag)
277{
278	struct kstat stat;
279	int error;
280
281	error = vfs_fstatat(dfd, filename, &stat, flag);
282	if (error)
283		return error;
284	return cp_new_stat(&stat, statbuf);
285}
286#endif
287
288SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
289{
290	struct kstat stat;
291	int error = vfs_fstat(fd, &stat);
292
293	if (!error)
294		error = cp_new_stat(&stat, statbuf);
295
296	return error;
297}
298
299SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
300		char __user *, buf, int, bufsiz)
301{
302	struct path path;
303	int error;
304
305	if (bufsiz <= 0)
306		return -EINVAL;
307
308	error = user_path_at(dfd, pathname, 0, &path);
309	if (!error) {
310		struct inode *inode = path.dentry->d_inode;
311
312		error = -EINVAL;
313		if (inode->i_op->readlink) {
314			error = security_inode_readlink(path.dentry);
315			if (!error) {
316				touch_atime(path.mnt, path.dentry);
317				error = inode->i_op->readlink(path.dentry,
318							      buf, bufsiz);
319			}
320		}
321		path_put(&path);
322	}
323	return error;
324}
325
326SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
327		int, bufsiz)
328{
329	return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
330}
331
332
333/* ---------- LFS-64 ----------- */
334#ifdef __ARCH_WANT_STAT64
335
336static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
337{
338	struct stat64 tmp;
339
340	memset(&tmp, 0, sizeof(struct stat64));
341#ifdef CONFIG_MIPS
342	/* mips has weird padding, so we don't get 64 bits there */
343	if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
344		return -EOVERFLOW;
345	tmp.st_dev = new_encode_dev(stat->dev);
346	tmp.st_rdev = new_encode_dev(stat->rdev);
347#else
348	tmp.st_dev = huge_encode_dev(stat->dev);
349	tmp.st_rdev = huge_encode_dev(stat->rdev);
350#endif
351	tmp.st_ino = stat->ino;
352	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
353		return -EOVERFLOW;
354#ifdef STAT64_HAS_BROKEN_ST_INO
355	tmp.__st_ino = stat->ino;
356#endif
357	tmp.st_mode = stat->mode;
358	tmp.st_nlink = stat->nlink;
359	tmp.st_uid = stat->uid;
360	tmp.st_gid = stat->gid;
361	tmp.st_atime = stat->atime.tv_sec;
362	tmp.st_atime_nsec = stat->atime.tv_nsec;
363	tmp.st_mtime = stat->mtime.tv_sec;
364	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
365	tmp.st_ctime = stat->ctime.tv_sec;
366	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
367	tmp.st_size = stat->size;
368	tmp.st_blocks = stat->blocks;
369	tmp.st_blksize = stat->blksize;
370	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
371}
372
373SYSCALL_DEFINE2(stat64, const char __user *, filename,
374		struct stat64 __user *, statbuf)
375{
376	struct kstat stat;
377	int error = vfs_stat(filename, &stat);
378
379	if (!error)
380		error = cp_new_stat64(&stat, statbuf);
381
382	return error;
383}
384
385SYSCALL_DEFINE2(lstat64, const char __user *, filename,
386		struct stat64 __user *, statbuf)
387{
388	struct kstat stat;
389	int error = vfs_lstat(filename, &stat);
390
391	if (!error)
392		error = cp_new_stat64(&stat, statbuf);
393
394	return error;
395}
396
397SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
398{
399	struct kstat stat;
400	int error = vfs_fstat(fd, &stat);
401
402	if (!error)
403		error = cp_new_stat64(&stat, statbuf);
404
405	return error;
406}
407
408SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
409		struct stat64 __user *, statbuf, int, flag)
410{
411	struct kstat stat;
412	int error;
413
414	error = vfs_fstatat(dfd, filename, &stat, flag);
415	if (error)
416		return error;
417	return cp_new_stat64(&stat, statbuf);
418}
419#endif /* __ARCH_WANT_STAT64 */
420
421/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
422void __inode_add_bytes(struct inode *inode, loff_t bytes)
423{
424	inode->i_blocks += bytes >> 9;
425	bytes &= 511;
426	inode->i_bytes += bytes;
427	if (inode->i_bytes >= 512) {
428		inode->i_blocks++;
429		inode->i_bytes -= 512;
430	}
431}
432
433void inode_add_bytes(struct inode *inode, loff_t bytes)
434{
435	spin_lock(&inode->i_lock);
436	__inode_add_bytes(inode, bytes);
437	spin_unlock(&inode->i_lock);
438}
439
440EXPORT_SYMBOL(inode_add_bytes);
441
442void inode_sub_bytes(struct inode *inode, loff_t bytes)
443{
444	spin_lock(&inode->i_lock);
445	inode->i_blocks -= bytes >> 9;
446	bytes &= 511;
447	if (inode->i_bytes < bytes) {
448		inode->i_blocks--;
449		inode->i_bytes += 512;
450	}
451	inode->i_bytes -= bytes;
452	spin_unlock(&inode->i_lock);
453}
454
455EXPORT_SYMBOL(inode_sub_bytes);
456
457loff_t inode_get_bytes(struct inode *inode)
458{
459	loff_t ret;
460
461	spin_lock(&inode->i_lock);
462	ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
463	spin_unlock(&inode->i_lock);
464	return ret;
465}
466
467EXPORT_SYMBOL(inode_get_bytes);
468
469void inode_set_bytes(struct inode *inode, loff_t bytes)
470{
471	/* Caller is here responsible for sufficient locking
472	 * (ie. inode->i_lock) */
473	inode->i_blocks = bytes >> 9;
474	inode->i_bytes = bytes & 511;
475}
476
477EXPORT_SYMBOL(inode_set_bytes);
478