Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/open.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | #include <linux/string.h> | |
8 | #include <linux/mm.h> | |
1da177e4 | 9 | #include <linux/file.h> |
9f3acc31 | 10 | #include <linux/fdtable.h> |
0eeca283 | 11 | #include <linux/fsnotify.h> |
1da177e4 | 12 | #include <linux/module.h> |
1da177e4 LT |
13 | #include <linux/tty.h> |
14 | #include <linux/namei.h> | |
15 | #include <linux/backing-dev.h> | |
16f7e0fe | 16 | #include <linux/capability.h> |
086f7316 | 17 | #include <linux/securebits.h> |
1da177e4 LT |
18 | #include <linux/security.h> |
19 | #include <linux/mount.h> | |
5590ff0d | 20 | #include <linux/fcntl.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
1da177e4 LT |
22 | #include <asm/uaccess.h> |
23 | #include <linux/fs.h> | |
ef3daeda | 24 | #include <linux/personality.h> |
1da177e4 LT |
25 | #include <linux/pagemap.h> |
26 | #include <linux/syscalls.h> | |
ab2af1f5 | 27 | #include <linux/rcupdate.h> |
73241ccc | 28 | #include <linux/audit.h> |
97ac7350 | 29 | #include <linux/falloc.h> |
5ad4e53b | 30 | #include <linux/fs_struct.h> |
b65a9cfc | 31 | #include <linux/ima.h> |
2dfc1cae | 32 | #include <linux/dnotify.h> |
3f6d078d | 33 | #include <linux/compat.h> |
1da177e4 | 34 | |
e81e3f4d EP |
35 | #include "internal.h" |
36 | ||
4a30131e N |
37 | int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, |
38 | struct file *filp) | |
1da177e4 | 39 | { |
939a9421 | 40 | int ret; |
1da177e4 LT |
41 | struct iattr newattrs; |
42 | ||
43 | /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */ | |
44 | if (length < 0) | |
45 | return -EINVAL; | |
46 | ||
47 | newattrs.ia_size = length; | |
4a30131e | 48 | newattrs.ia_valid = ATTR_SIZE | time_attrs; |
cc4e69de MS |
49 | if (filp) { |
50 | newattrs.ia_file = filp; | |
51 | newattrs.ia_valid |= ATTR_FILE; | |
52 | } | |
1da177e4 | 53 | |
7b82dc0e | 54 | /* Remove suid/sgid on truncate too */ |
939a9421 AW |
55 | ret = should_remove_suid(dentry); |
56 | if (ret) | |
57 | newattrs.ia_valid |= ret | ATTR_FORCE; | |
7b82dc0e | 58 | |
1b1dcc1b | 59 | mutex_lock(&dentry->d_inode->i_mutex); |
939a9421 | 60 | ret = notify_change(dentry, &newattrs); |
1b1dcc1b | 61 | mutex_unlock(&dentry->d_inode->i_mutex); |
939a9421 | 62 | return ret; |
1da177e4 LT |
63 | } |
64 | ||
a02de960 | 65 | long vfs_truncate(struct path *path, loff_t length) |
1da177e4 | 66 | { |
2d8f3038 | 67 | struct inode *inode; |
a02de960 | 68 | long error; |
1da177e4 | 69 | |
a02de960 | 70 | inode = path->dentry->d_inode; |
1da177e4 LT |
71 | |
72 | /* For directories it's -EISDIR, for other non-regulars - -EINVAL */ | |
1da177e4 | 73 | if (S_ISDIR(inode->i_mode)) |
a02de960 | 74 | return -EISDIR; |
1da177e4 | 75 | if (!S_ISREG(inode->i_mode)) |
a02de960 | 76 | return -EINVAL; |
1da177e4 | 77 | |
a02de960 | 78 | error = mnt_want_write(path->mnt); |
1da177e4 | 79 | if (error) |
a02de960 | 80 | goto out; |
1da177e4 | 81 | |
256984a8 | 82 | error = inode_permission(inode, MAY_WRITE); |
9ac9b847 DH |
83 | if (error) |
84 | goto mnt_drop_write_and_out; | |
1da177e4 LT |
85 | |
86 | error = -EPERM; | |
c82e42da | 87 | if (IS_APPEND(inode)) |
9ac9b847 | 88 | goto mnt_drop_write_and_out; |
1da177e4 | 89 | |
9700382c | 90 | error = get_write_access(inode); |
1da177e4 | 91 | if (error) |
9ac9b847 | 92 | goto mnt_drop_write_and_out; |
1da177e4 | 93 | |
9700382c | 94 | /* |
95 | * Make sure that there are no leases. get_write_access() protects | |
96 | * against the truncate racing with a lease-granting setlease(). | |
97 | */ | |
8737c930 | 98 | error = break_lease(inode, O_WRONLY); |
1da177e4 | 99 | if (error) |
9700382c | 100 | goto put_write_and_out; |
1da177e4 LT |
101 | |
102 | error = locks_verify_truncate(inode, NULL, length); | |
be6d3e56 | 103 | if (!error) |
a02de960 | 104 | error = security_path_truncate(path); |
907f4554 | 105 | if (!error) |
a02de960 | 106 | error = do_truncate(path->dentry, length, 0, NULL); |
1da177e4 | 107 | |
9700382c | 108 | put_write_and_out: |
109 | put_write_access(inode); | |
9ac9b847 | 110 | mnt_drop_write_and_out: |
a02de960 | 111 | mnt_drop_write(path->mnt); |
1da177e4 LT |
112 | out: |
113 | return error; | |
114 | } | |
a02de960 DH |
115 | EXPORT_SYMBOL_GPL(vfs_truncate); |
116 | ||
117 | static long do_sys_truncate(const char __user *pathname, loff_t length) | |
118 | { | |
48f7530d | 119 | unsigned int lookup_flags = LOOKUP_FOLLOW; |
a02de960 DH |
120 | struct path path; |
121 | int error; | |
122 | ||
123 | if (length < 0) /* sorry, but loff_t says... */ | |
124 | return -EINVAL; | |
125 | ||
48f7530d JL |
126 | retry: |
127 | error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path); | |
a02de960 DH |
128 | if (!error) { |
129 | error = vfs_truncate(&path, length); | |
130 | path_put(&path); | |
131 | } | |
48f7530d JL |
132 | if (retry_estale(error, lookup_flags)) { |
133 | lookup_flags |= LOOKUP_REVAL; | |
134 | goto retry; | |
135 | } | |
a02de960 DH |
136 | return error; |
137 | } | |
1da177e4 | 138 | |
4fd8da8d | 139 | SYSCALL_DEFINE2(truncate, const char __user *, path, long, length) |
1da177e4 | 140 | { |
4fd8da8d | 141 | return do_sys_truncate(path, length); |
1da177e4 LT |
142 | } |
143 | ||
3f6d078d AV |
144 | #ifdef CONFIG_COMPAT |
145 | COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length) | |
146 | { | |
147 | return do_sys_truncate(path, length); | |
148 | } | |
149 | #endif | |
150 | ||
b01ec0ef | 151 | static long do_sys_ftruncate(unsigned int fd, loff_t length, int small) |
1da177e4 | 152 | { |
bf2965d5 | 153 | struct inode *inode; |
1da177e4 | 154 | struct dentry *dentry; |
2903ff01 | 155 | struct fd f; |
1da177e4 LT |
156 | int error; |
157 | ||
158 | error = -EINVAL; | |
159 | if (length < 0) | |
160 | goto out; | |
161 | error = -EBADF; | |
2903ff01 AV |
162 | f = fdget(fd); |
163 | if (!f.file) | |
1da177e4 LT |
164 | goto out; |
165 | ||
166 | /* explicitly opened as large or we are on 64-bit box */ | |
2903ff01 | 167 | if (f.file->f_flags & O_LARGEFILE) |
1da177e4 LT |
168 | small = 0; |
169 | ||
2903ff01 | 170 | dentry = f.file->f_path.dentry; |
1da177e4 LT |
171 | inode = dentry->d_inode; |
172 | error = -EINVAL; | |
2903ff01 | 173 | if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE)) |
1da177e4 LT |
174 | goto out_putf; |
175 | ||
176 | error = -EINVAL; | |
177 | /* Cannot ftruncate over 2^31 bytes without large file support */ | |
178 | if (small && length > MAX_NON_LFS) | |
179 | goto out_putf; | |
180 | ||
181 | error = -EPERM; | |
182 | if (IS_APPEND(inode)) | |
183 | goto out_putf; | |
184 | ||
14da9200 | 185 | sb_start_write(inode->i_sb); |
2903ff01 | 186 | error = locks_verify_truncate(inode, f.file, length); |
be6d3e56 | 187 | if (!error) |
2903ff01 | 188 | error = security_path_truncate(&f.file->f_path); |
1da177e4 | 189 | if (!error) |
2903ff01 | 190 | error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file); |
14da9200 | 191 | sb_end_write(inode->i_sb); |
1da177e4 | 192 | out_putf: |
2903ff01 | 193 | fdput(f); |
1da177e4 LT |
194 | out: |
195 | return error; | |
196 | } | |
197 | ||
bdc480e3 | 198 | SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length) |
1da177e4 | 199 | { |
0a489cb3 | 200 | long ret = do_sys_ftruncate(fd, length, 1); |
385910f2 | 201 | /* avoid REGPARM breakage on x86: */ |
54a01510 | 202 | asmlinkage_protect(2, ret, fd, length); |
0a489cb3 | 203 | return ret; |
1da177e4 LT |
204 | } |
205 | ||
3f6d078d AV |
206 | #ifdef CONFIG_COMPAT |
207 | COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length) | |
208 | { | |
209 | return do_sys_ftruncate(fd, length, 1); | |
210 | } | |
211 | #endif | |
212 | ||
1da177e4 LT |
213 | /* LFS versions of truncate are only needed on 32 bit machines */ |
214 | #if BITS_PER_LONG == 32 | |
4a0fd5bf | 215 | SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length) |
1da177e4 LT |
216 | { |
217 | return do_sys_truncate(path, length); | |
218 | } | |
219 | ||
4a0fd5bf | 220 | SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length) |
1da177e4 | 221 | { |
0a489cb3 | 222 | long ret = do_sys_ftruncate(fd, length, 0); |
385910f2 | 223 | /* avoid REGPARM breakage on x86: */ |
54a01510 | 224 | asmlinkage_protect(2, ret, fd, length); |
0a489cb3 | 225 | return ret; |
1da177e4 | 226 | } |
6673e0c3 | 227 | #endif /* BITS_PER_LONG == 32 */ |
1da177e4 | 228 | |
3e63cbb1 AJ |
229 | |
230 | int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len) | |
97ac7350 | 231 | { |
496ad9aa | 232 | struct inode *inode = file_inode(file); |
3e63cbb1 | 233 | long ret; |
97ac7350 AA |
234 | |
235 | if (offset < 0 || len <= 0) | |
3e63cbb1 | 236 | return -EINVAL; |
97ac7350 AA |
237 | |
238 | /* Return error if mode is not supported */ | |
79124f18 JB |
239 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
240 | return -EOPNOTSUPP; | |
241 | ||
242 | /* Punch hole must have keep size set */ | |
243 | if ((mode & FALLOC_FL_PUNCH_HOLE) && | |
244 | !(mode & FALLOC_FL_KEEP_SIZE)) | |
3e63cbb1 | 245 | return -EOPNOTSUPP; |
97ac7350 | 246 | |
97ac7350 | 247 | if (!(file->f_mode & FMODE_WRITE)) |
3e63cbb1 | 248 | return -EBADF; |
1ca551c6 MS |
249 | |
250 | /* It's not possible punch hole on append only file */ | |
251 | if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode)) | |
252 | return -EPERM; | |
253 | ||
254 | if (IS_IMMUTABLE(inode)) | |
255 | return -EPERM; | |
256 | ||
97ac7350 AA |
257 | /* |
258 | * Revalidate the write permissions, in case security policy has | |
259 | * changed since the files were opened. | |
260 | */ | |
261 | ret = security_file_permission(file, MAY_WRITE); | |
262 | if (ret) | |
3e63cbb1 | 263 | return ret; |
97ac7350 | 264 | |
97ac7350 | 265 | if (S_ISFIFO(inode->i_mode)) |
3e63cbb1 | 266 | return -ESPIPE; |
97ac7350 | 267 | |
97ac7350 AA |
268 | /* |
269 | * Let individual file system decide if it supports preallocation | |
270 | * for directories or not. | |
271 | */ | |
272 | if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) | |
3e63cbb1 | 273 | return -ENODEV; |
97ac7350 | 274 | |
97ac7350 AA |
275 | /* Check for wrap through zero too */ |
276 | if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0)) | |
3e63cbb1 | 277 | return -EFBIG; |
97ac7350 | 278 | |
2fe17c10 | 279 | if (!file->f_op->fallocate) |
3e63cbb1 | 280 | return -EOPNOTSUPP; |
97ac7350 | 281 | |
14da9200 JK |
282 | sb_start_write(inode->i_sb); |
283 | ret = file->f_op->fallocate(file, mode, offset, len); | |
284 | sb_end_write(inode->i_sb); | |
285 | return ret; | |
3e63cbb1 AJ |
286 | } |
287 | ||
4a0fd5bf | 288 | SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len) |
3e63cbb1 | 289 | { |
2903ff01 | 290 | struct fd f = fdget(fd); |
3e63cbb1 AJ |
291 | int error = -EBADF; |
292 | ||
2903ff01 AV |
293 | if (f.file) { |
294 | error = do_fallocate(f.file, mode, offset, len); | |
295 | fdput(f); | |
3e63cbb1 | 296 | } |
3e63cbb1 | 297 | return error; |
97ac7350 | 298 | } |
3e63cbb1 | 299 | |
1da177e4 LT |
300 | /* |
301 | * access() needs to use the real uid/gid, not the effective uid/gid. | |
302 | * We do this by temporarily clearing all FS-related capabilities and | |
303 | * switching the fsuid/fsgid around to the real ones. | |
304 | */ | |
6559eed8 | 305 | SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode) |
1da177e4 | 306 | { |
d84f4f99 DH |
307 | const struct cred *old_cred; |
308 | struct cred *override_cred; | |
2d8f3038 | 309 | struct path path; |
256984a8 | 310 | struct inode *inode; |
1da177e4 | 311 | int res; |
87fa5595 | 312 | unsigned int lookup_flags = LOOKUP_FOLLOW; |
1da177e4 LT |
313 | |
314 | if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */ | |
315 | return -EINVAL; | |
316 | ||
d84f4f99 DH |
317 | override_cred = prepare_creds(); |
318 | if (!override_cred) | |
319 | return -ENOMEM; | |
1da177e4 | 320 | |
d84f4f99 DH |
321 | override_cred->fsuid = override_cred->uid; |
322 | override_cred->fsgid = override_cred->gid; | |
1da177e4 | 323 | |
086f7316 | 324 | if (!issecure(SECURE_NO_SETUID_FIXUP)) { |
1cdcbec1 | 325 | /* Clear the capabilities if we switch to a non-root user */ |
18815a18 EB |
326 | kuid_t root_uid = make_kuid(override_cred->user_ns, 0); |
327 | if (!uid_eq(override_cred->uid, root_uid)) | |
d84f4f99 | 328 | cap_clear(override_cred->cap_effective); |
086f7316 | 329 | else |
d84f4f99 DH |
330 | override_cred->cap_effective = |
331 | override_cred->cap_permitted; | |
086f7316 | 332 | } |
1da177e4 | 333 | |
d84f4f99 | 334 | old_cred = override_creds(override_cred); |
87fa5595 JL |
335 | retry: |
336 | res = user_path_at(dfd, filename, lookup_flags, &path); | |
6902d925 DH |
337 | if (res) |
338 | goto out; | |
339 | ||
2d8f3038 | 340 | inode = path.dentry->d_inode; |
256984a8 AV |
341 | |
342 | if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) { | |
30524472 AV |
343 | /* |
344 | * MAY_EXEC on regular files is denied if the fs is mounted | |
345 | * with the "noexec" flag. | |
346 | */ | |
347 | res = -EACCES; | |
2d8f3038 | 348 | if (path.mnt->mnt_flags & MNT_NOEXEC) |
30524472 AV |
349 | goto out_path_release; |
350 | } | |
351 | ||
256984a8 | 352 | res = inode_permission(inode, mode | MAY_ACCESS); |
6902d925 | 353 | /* SuS v2 requires we report a read only fs too */ |
256984a8 | 354 | if (res || !(mode & S_IWOTH) || special_file(inode->i_mode)) |
6902d925 | 355 | goto out_path_release; |
2f676cbc DH |
356 | /* |
357 | * This is a rare case where using __mnt_is_readonly() | |
358 | * is OK without a mnt_want/drop_write() pair. Since | |
359 | * no actual write to the fs is performed here, we do | |
360 | * not need to telegraph to that to anyone. | |
361 | * | |
362 | * By doing this, we accept that this access is | |
363 | * inherently racy and know that the fs may change | |
364 | * state before we even see this result. | |
365 | */ | |
2d8f3038 | 366 | if (__mnt_is_readonly(path.mnt)) |
6902d925 | 367 | res = -EROFS; |
1da177e4 | 368 | |
6902d925 | 369 | out_path_release: |
2d8f3038 | 370 | path_put(&path); |
87fa5595 JL |
371 | if (retry_estale(res, lookup_flags)) { |
372 | lookup_flags |= LOOKUP_REVAL; | |
373 | goto retry; | |
374 | } | |
6902d925 | 375 | out: |
d84f4f99 DH |
376 | revert_creds(old_cred); |
377 | put_cred(override_cred); | |
1da177e4 LT |
378 | return res; |
379 | } | |
380 | ||
ca013e94 | 381 | SYSCALL_DEFINE2(access, const char __user *, filename, int, mode) |
5590ff0d UD |
382 | { |
383 | return sys_faccessat(AT_FDCWD, filename, mode); | |
384 | } | |
385 | ||
3cdad428 | 386 | SYSCALL_DEFINE1(chdir, const char __user *, filename) |
1da177e4 | 387 | { |
2d8f3038 | 388 | struct path path; |
1da177e4 | 389 | int error; |
0291c0a5 JL |
390 | unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
391 | retry: | |
392 | error = user_path_at(AT_FDCWD, filename, lookup_flags, &path); | |
1da177e4 LT |
393 | if (error) |
394 | goto out; | |
395 | ||
9cfcac81 | 396 | error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); |
1da177e4 LT |
397 | if (error) |
398 | goto dput_and_out; | |
399 | ||
2d8f3038 | 400 | set_fs_pwd(current->fs, &path); |
1da177e4 LT |
401 | |
402 | dput_and_out: | |
2d8f3038 | 403 | path_put(&path); |
0291c0a5 JL |
404 | if (retry_estale(error, lookup_flags)) { |
405 | lookup_flags |= LOOKUP_REVAL; | |
406 | goto retry; | |
407 | } | |
1da177e4 LT |
408 | out: |
409 | return error; | |
410 | } | |
411 | ||
3cdad428 | 412 | SYSCALL_DEFINE1(fchdir, unsigned int, fd) |
1da177e4 | 413 | { |
2903ff01 | 414 | struct fd f = fdget_raw(fd); |
1da177e4 | 415 | struct inode *inode; |
2903ff01 | 416 | int error = -EBADF; |
1da177e4 LT |
417 | |
418 | error = -EBADF; | |
2903ff01 | 419 | if (!f.file) |
1da177e4 LT |
420 | goto out; |
421 | ||
496ad9aa | 422 | inode = file_inode(f.file); |
1da177e4 LT |
423 | |
424 | error = -ENOTDIR; | |
425 | if (!S_ISDIR(inode->i_mode)) | |
426 | goto out_putf; | |
427 | ||
9cfcac81 | 428 | error = inode_permission(inode, MAY_EXEC | MAY_CHDIR); |
1da177e4 | 429 | if (!error) |
2903ff01 | 430 | set_fs_pwd(current->fs, &f.file->f_path); |
1da177e4 | 431 | out_putf: |
2903ff01 | 432 | fdput(f); |
1da177e4 LT |
433 | out: |
434 | return error; | |
435 | } | |
436 | ||
3480b257 | 437 | SYSCALL_DEFINE1(chroot, const char __user *, filename) |
1da177e4 | 438 | { |
2d8f3038 | 439 | struct path path; |
1da177e4 | 440 | int error; |
2771261e JL |
441 | unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
442 | retry: | |
443 | error = user_path_at(AT_FDCWD, filename, lookup_flags, &path); | |
1da177e4 LT |
444 | if (error) |
445 | goto out; | |
446 | ||
9cfcac81 | 447 | error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); |
1da177e4 LT |
448 | if (error) |
449 | goto dput_and_out; | |
450 | ||
451 | error = -EPERM; | |
a85fb273 | 452 | if (!nsown_capable(CAP_SYS_CHROOT)) |
1da177e4 | 453 | goto dput_and_out; |
8b8efb44 TH |
454 | error = security_path_chroot(&path); |
455 | if (error) | |
456 | goto dput_and_out; | |
1da177e4 | 457 | |
2d8f3038 | 458 | set_fs_root(current->fs, &path); |
1da177e4 LT |
459 | error = 0; |
460 | dput_and_out: | |
2d8f3038 | 461 | path_put(&path); |
2771261e JL |
462 | if (retry_estale(error, lookup_flags)) { |
463 | lookup_flags |= LOOKUP_REVAL; | |
464 | goto retry; | |
465 | } | |
1da177e4 LT |
466 | out: |
467 | return error; | |
468 | } | |
469 | ||
e57712eb | 470 | static int chmod_common(struct path *path, umode_t mode) |
1da177e4 | 471 | { |
e57712eb | 472 | struct inode *inode = path->dentry->d_inode; |
1da177e4 | 473 | struct iattr newattrs; |
e57712eb | 474 | int error; |
1da177e4 | 475 | |
e57712eb AV |
476 | error = mnt_want_write(path->mnt); |
477 | if (error) | |
478 | return error; | |
fe542cf5 | 479 | mutex_lock(&inode->i_mutex); |
cdcf116d | 480 | error = security_path_chmod(path, mode); |
e57712eb | 481 | if (error) |
fe542cf5 | 482 | goto out_unlock; |
1da177e4 LT |
483 | newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
484 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; | |
e57712eb | 485 | error = notify_change(path->dentry, &newattrs); |
fe542cf5 | 486 | out_unlock: |
1b1dcc1b | 487 | mutex_unlock(&inode->i_mutex); |
e57712eb AV |
488 | mnt_drop_write(path->mnt); |
489 | return error; | |
490 | } | |
491 | ||
49f0a076 | 492 | SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) |
e57712eb AV |
493 | { |
494 | struct file * file; | |
495 | int err = -EBADF; | |
496 | ||
497 | file = fget(fd); | |
498 | if (file) { | |
bfcec708 | 499 | audit_inode(NULL, file->f_path.dentry, 0); |
e57712eb AV |
500 | err = chmod_common(&file->f_path, mode); |
501 | fput(file); | |
502 | } | |
1da177e4 LT |
503 | return err; |
504 | } | |
505 | ||
49f0a076 | 506 | SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode) |
1da177e4 | 507 | { |
2d8f3038 | 508 | struct path path; |
1da177e4 | 509 | int error; |
14ff690c JL |
510 | unsigned int lookup_flags = LOOKUP_FOLLOW; |
511 | retry: | |
512 | error = user_path_at(dfd, filename, lookup_flags, &path); | |
e57712eb AV |
513 | if (!error) { |
514 | error = chmod_common(&path, mode); | |
515 | path_put(&path); | |
14ff690c JL |
516 | if (retry_estale(error, lookup_flags)) { |
517 | lookup_flags |= LOOKUP_REVAL; | |
518 | goto retry; | |
519 | } | |
e57712eb | 520 | } |
1da177e4 LT |
521 | return error; |
522 | } | |
523 | ||
49f0a076 | 524 | SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode) |
5590ff0d UD |
525 | { |
526 | return sys_fchmodat(AT_FDCWD, filename, mode); | |
527 | } | |
528 | ||
fe542cf5 | 529 | static int chown_common(struct path *path, uid_t user, gid_t group) |
1da177e4 | 530 | { |
fe542cf5 | 531 | struct inode *inode = path->dentry->d_inode; |
1da177e4 LT |
532 | int error; |
533 | struct iattr newattrs; | |
52137abe EB |
534 | kuid_t uid; |
535 | kgid_t gid; | |
536 | ||
537 | uid = make_kuid(current_user_ns(), user); | |
538 | gid = make_kgid(current_user_ns(), group); | |
1da177e4 | 539 | |
1da177e4 LT |
540 | newattrs.ia_valid = ATTR_CTIME; |
541 | if (user != (uid_t) -1) { | |
52137abe EB |
542 | if (!uid_valid(uid)) |
543 | return -EINVAL; | |
1da177e4 | 544 | newattrs.ia_valid |= ATTR_UID; |
52137abe | 545 | newattrs.ia_uid = uid; |
1da177e4 LT |
546 | } |
547 | if (group != (gid_t) -1) { | |
52137abe EB |
548 | if (!gid_valid(gid)) |
549 | return -EINVAL; | |
1da177e4 | 550 | newattrs.ia_valid |= ATTR_GID; |
52137abe | 551 | newattrs.ia_gid = gid; |
1da177e4 LT |
552 | } |
553 | if (!S_ISDIR(inode->i_mode)) | |
b5376771 SH |
554 | newattrs.ia_valid |= |
555 | ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV; | |
1b1dcc1b | 556 | mutex_lock(&inode->i_mutex); |
d2b31ca6 | 557 | error = security_path_chown(path, uid, gid); |
fe542cf5 TH |
558 | if (!error) |
559 | error = notify_change(path->dentry, &newattrs); | |
1b1dcc1b | 560 | mutex_unlock(&inode->i_mutex); |
beb29e05 | 561 | |
1da177e4 LT |
562 | return error; |
563 | } | |
564 | ||
6559eed8 HC |
565 | SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user, |
566 | gid_t, group, int, flag) | |
5590ff0d | 567 | { |
2d8f3038 | 568 | struct path path; |
5590ff0d | 569 | int error = -EINVAL; |
65cfc672 | 570 | int lookup_flags; |
5590ff0d | 571 | |
65cfc672 | 572 | if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0) |
5590ff0d UD |
573 | goto out; |
574 | ||
65cfc672 AV |
575 | lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; |
576 | if (flag & AT_EMPTY_PATH) | |
577 | lookup_flags |= LOOKUP_EMPTY; | |
99a5df37 | 578 | retry: |
65cfc672 | 579 | error = user_path_at(dfd, filename, lookup_flags, &path); |
6902d925 DH |
580 | if (error) |
581 | goto out; | |
2d8f3038 | 582 | error = mnt_want_write(path.mnt); |
2af482a7 DH |
583 | if (error) |
584 | goto out_release; | |
fe542cf5 | 585 | error = chown_common(&path, user, group); |
2d8f3038 | 586 | mnt_drop_write(path.mnt); |
2af482a7 | 587 | out_release: |
2d8f3038 | 588 | path_put(&path); |
99a5df37 JL |
589 | if (retry_estale(error, lookup_flags)) { |
590 | lookup_flags |= LOOKUP_REVAL; | |
591 | goto retry; | |
592 | } | |
5590ff0d UD |
593 | out: |
594 | return error; | |
595 | } | |
596 | ||
55e4def0 | 597 | SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group) |
1da177e4 | 598 | { |
55e4def0 DH |
599 | return sys_fchownat(AT_FDCWD, filename, user, group, 0); |
600 | } | |
1da177e4 | 601 | |
55e4def0 DH |
602 | SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group) |
603 | { | |
604 | return sys_fchownat(AT_FDCWD, filename, user, group, | |
605 | AT_SYMLINK_NOFOLLOW); | |
1da177e4 LT |
606 | } |
607 | ||
ca013e94 | 608 | SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group) |
1da177e4 | 609 | { |
2903ff01 | 610 | struct fd f = fdget(fd); |
1da177e4 LT |
611 | int error = -EBADF; |
612 | ||
2903ff01 | 613 | if (!f.file) |
6902d925 DH |
614 | goto out; |
615 | ||
2903ff01 | 616 | error = mnt_want_write_file(f.file); |
2af482a7 DH |
617 | if (error) |
618 | goto out_fput; | |
bfcec708 | 619 | audit_inode(NULL, f.file->f_path.dentry, 0); |
2903ff01 AV |
620 | error = chown_common(&f.file->f_path, user, group); |
621 | mnt_drop_write_file(f.file); | |
2af482a7 | 622 | out_fput: |
2903ff01 | 623 | fdput(f); |
6902d925 | 624 | out: |
1da177e4 LT |
625 | return error; |
626 | } | |
627 | ||
4a3fd211 DH |
628 | /* |
629 | * You have to be very careful that these write | |
630 | * counts get cleaned up in error cases and | |
631 | * upon __fput(). This should probably never | |
632 | * be called outside of __dentry_open(). | |
633 | */ | |
634 | static inline int __get_file_write_access(struct inode *inode, | |
635 | struct vfsmount *mnt) | |
636 | { | |
637 | int error; | |
638 | error = get_write_access(inode); | |
639 | if (error) | |
640 | return error; | |
641 | /* | |
642 | * Do not take mount writer counts on | |
643 | * special files since no writes to | |
644 | * the mount itself will occur. | |
645 | */ | |
646 | if (!special_file(inode->i_mode)) { | |
647 | /* | |
648 | * Balanced in __fput() | |
649 | */ | |
eb04c282 | 650 | error = __mnt_want_write(mnt); |
4a3fd211 DH |
651 | if (error) |
652 | put_write_access(inode); | |
653 | } | |
654 | return error; | |
655 | } | |
656 | ||
90ad1a8e MS |
657 | int open_check_o_direct(struct file *f) |
658 | { | |
659 | /* NB: we're sure to have correct a_ops only after f_op->open */ | |
660 | if (f->f_flags & O_DIRECT) { | |
661 | if (!f->f_mapping->a_ops || | |
662 | ((!f->f_mapping->a_ops->direct_IO) && | |
663 | (!f->f_mapping->a_ops->get_xip_mem))) { | |
664 | return -EINVAL; | |
665 | } | |
666 | } | |
667 | return 0; | |
668 | } | |
669 | ||
02e5180d | 670 | static int do_dentry_open(struct file *f, |
96b7e579 AV |
671 | int (*open)(struct inode *, struct file *), |
672 | const struct cred *cred) | |
1da177e4 | 673 | { |
1abf0c71 | 674 | static const struct file_operations empty_fops = {}; |
1da177e4 LT |
675 | struct inode *inode; |
676 | int error; | |
677 | ||
5300990c | 678 | f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK | |
a1a5b3d9 | 679 | FMODE_PREAD | FMODE_PWRITE; |
1abf0c71 AV |
680 | |
681 | if (unlikely(f->f_flags & O_PATH)) | |
682 | f->f_mode = FMODE_PATH; | |
683 | ||
b5bcdda3 | 684 | path_get(&f->f_path); |
dd37978c | 685 | inode = f->f_inode = f->f_path.dentry->d_inode; |
1da177e4 | 686 | if (f->f_mode & FMODE_WRITE) { |
02e5180d | 687 | error = __get_file_write_access(inode, f->f_path.mnt); |
1da177e4 LT |
688 | if (error) |
689 | goto cleanup_file; | |
ad775f5a DH |
690 | if (!special_file(inode->i_mode)) |
691 | file_take_write(f); | |
1da177e4 LT |
692 | } |
693 | ||
694 | f->f_mapping = inode->i_mapping; | |
ee2ffa0d | 695 | file_sb_list_add(f, inode->i_sb); |
1da177e4 | 696 | |
1abf0c71 AV |
697 | if (unlikely(f->f_mode & FMODE_PATH)) { |
698 | f->f_op = &empty_fops; | |
96b7e579 | 699 | return 0; |
1abf0c71 AV |
700 | } |
701 | ||
702 | f->f_op = fops_get(inode->i_fop); | |
703 | ||
83d49856 | 704 | error = security_file_open(f, cred); |
788e7dd4 YN |
705 | if (error) |
706 | goto cleanup_all; | |
707 | ||
f3c7691e BF |
708 | error = break_lease(inode, f->f_flags); |
709 | if (error) | |
710 | goto cleanup_all; | |
711 | ||
834f2a4a TM |
712 | if (!open && f->f_op) |
713 | open = f->f_op->open; | |
714 | if (open) { | |
715 | error = open(inode, f); | |
1da177e4 LT |
716 | if (error) |
717 | goto cleanup_all; | |
718 | } | |
890275b5 MZ |
719 | if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) |
720 | i_readcount_inc(inode); | |
834f2a4a | 721 | |
1da177e4 LT |
722 | f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
723 | ||
724 | file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); | |
725 | ||
96b7e579 | 726 | return 0; |
1da177e4 LT |
727 | |
728 | cleanup_all: | |
729 | fops_put(f->f_op); | |
c3c4f694 | 730 | file_sb_list_del(f); |
4a3fd211 | 731 | if (f->f_mode & FMODE_WRITE) { |
1da177e4 | 732 | put_write_access(inode); |
ad775f5a DH |
733 | if (!special_file(inode->i_mode)) { |
734 | /* | |
735 | * We don't consider this a real | |
736 | * mnt_want/drop_write() pair | |
737 | * because it all happenend right | |
738 | * here, so just reset the state. | |
739 | */ | |
740 | file_reset_write(f); | |
fe7c8051 | 741 | __mnt_drop_write(f->f_path.mnt); |
ad775f5a | 742 | } |
4a3fd211 | 743 | } |
1da177e4 | 744 | cleanup_file: |
02e5180d AV |
745 | path_put(&f->f_path); |
746 | f->f_path.mnt = NULL; | |
747 | f->f_path.dentry = NULL; | |
dd37978c | 748 | f->f_inode = NULL; |
96b7e579 | 749 | return error; |
1da177e4 LT |
750 | } |
751 | ||
d18e9008 MS |
752 | /** |
753 | * finish_open - finish opening a file | |
754 | * @od: opaque open data | |
755 | * @dentry: pointer to dentry | |
756 | * @open: open callback | |
757 | * | |
758 | * This can be used to finish opening a file passed to i_op->atomic_open(). | |
759 | * | |
760 | * If the open callback is set to NULL, then the standard f_op->open() | |
761 | * filesystem callback is substituted. | |
762 | */ | |
30d90494 AV |
763 | int finish_open(struct file *file, struct dentry *dentry, |
764 | int (*open)(struct inode *, struct file *), | |
765 | int *opened) | |
d18e9008 | 766 | { |
96b7e579 | 767 | int error; |
3d8a00d2 | 768 | BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */ |
d18e9008 | 769 | |
b5bcdda3 | 770 | file->f_path.dentry = dentry; |
02e5180d | 771 | error = do_dentry_open(file, open, current_cred()); |
96b7e579 | 772 | if (!error) |
47237687 | 773 | *opened |= FILE_OPENED; |
d18e9008 | 774 | |
96b7e579 | 775 | return error; |
d18e9008 MS |
776 | } |
777 | EXPORT_SYMBOL(finish_open); | |
778 | ||
779 | /** | |
780 | * finish_no_open - finish ->atomic_open() without opening the file | |
781 | * | |
782 | * @od: opaque open data | |
783 | * @dentry: dentry or NULL (as returned from ->lookup()) | |
784 | * | |
785 | * This can be used to set the result of a successful lookup in ->atomic_open(). | |
786 | * The filesystem's atomic_open() method shall return NULL after calling this. | |
787 | */ | |
e45198a6 | 788 | int finish_no_open(struct file *file, struct dentry *dentry) |
d18e9008 | 789 | { |
30d90494 | 790 | file->f_path.dentry = dentry; |
e45198a6 | 791 | return 1; |
d18e9008 MS |
792 | } |
793 | EXPORT_SYMBOL(finish_no_open); | |
794 | ||
765927b2 | 795 | struct file *dentry_open(const struct path *path, int flags, |
745ca247 | 796 | const struct cred *cred) |
a1a5b3d9 PS |
797 | { |
798 | int error; | |
799 | struct file *f; | |
800 | ||
e0e81739 DH |
801 | validate_creds(cred); |
802 | ||
c212f9aa | 803 | /* We must always pass in a valid mount pointer. */ |
765927b2 | 804 | BUG_ON(!path->mnt); |
322ee5b3 | 805 | |
a1a5b3d9 | 806 | f = get_empty_filp(); |
1afc99be AV |
807 | if (!IS_ERR(f)) { |
808 | f->f_flags = flags; | |
809 | f->f_path = *path; | |
810 | error = do_dentry_open(f, NULL, cred); | |
811 | if (!error) { | |
812 | /* from now on we need fput() to dispose of f */ | |
813 | error = open_check_o_direct(f); | |
814 | if (error) { | |
815 | fput(f); | |
816 | f = ERR_PTR(error); | |
817 | } | |
818 | } else { | |
819 | put_filp(f); | |
2a027e7a AV |
820 | f = ERR_PTR(error); |
821 | } | |
2a027e7a AV |
822 | } |
823 | return f; | |
a1a5b3d9 | 824 | } |
1da177e4 LT |
825 | EXPORT_SYMBOL(dentry_open); |
826 | ||
a218d0fd | 827 | static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op) |
47c805dc AV |
828 | { |
829 | int lookup_flags = 0; | |
830 | int acc_mode; | |
831 | ||
e68726ff MS |
832 | if (flags & O_CREAT) |
833 | op->mode = (mode & S_IALLUGO) | S_IFREG; | |
834 | else | |
835 | op->mode = 0; | |
47c805dc AV |
836 | |
837 | /* Must never be set by userspace */ | |
c6f3d811 | 838 | flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC; |
47c805dc AV |
839 | |
840 | /* | |
841 | * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only | |
842 | * check for O_DSYNC if the need any syncing at all we enforce it's | |
843 | * always set instead of having to deal with possibly weird behaviour | |
844 | * for malicious applications setting only __O_SYNC. | |
845 | */ | |
846 | if (flags & __O_SYNC) | |
847 | flags |= O_DSYNC; | |
848 | ||
1abf0c71 AV |
849 | /* |
850 | * If we have O_PATH in the open flag. Then we | |
851 | * cannot have anything other than the below set of flags | |
852 | */ | |
853 | if (flags & O_PATH) { | |
854 | flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH; | |
855 | acc_mode = 0; | |
856 | } else { | |
857 | acc_mode = MAY_OPEN | ACC_MODE(flags); | |
858 | } | |
47c805dc | 859 | |
1abf0c71 | 860 | op->open_flag = flags; |
47c805dc AV |
861 | |
862 | /* O_TRUNC implies we need access checks for write permissions */ | |
863 | if (flags & O_TRUNC) | |
864 | acc_mode |= MAY_WRITE; | |
865 | ||
866 | /* Allow the LSM permission hook to distinguish append | |
867 | access from general write access. */ | |
868 | if (flags & O_APPEND) | |
869 | acc_mode |= MAY_APPEND; | |
870 | ||
871 | op->acc_mode = acc_mode; | |
872 | ||
1abf0c71 AV |
873 | op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN; |
874 | ||
47c805dc AV |
875 | if (flags & O_CREAT) { |
876 | op->intent |= LOOKUP_CREATE; | |
877 | if (flags & O_EXCL) | |
878 | op->intent |= LOOKUP_EXCL; | |
879 | } | |
880 | ||
881 | if (flags & O_DIRECTORY) | |
882 | lookup_flags |= LOOKUP_DIRECTORY; | |
883 | if (!(flags & O_NOFOLLOW)) | |
884 | lookup_flags |= LOOKUP_FOLLOW; | |
885 | return lookup_flags; | |
886 | } | |
887 | ||
669abf4e JL |
888 | /** |
889 | * file_open_name - open file and return file pointer | |
890 | * | |
891 | * @name: struct filename containing path to open | |
892 | * @flags: open flags as per the open(2) second argument | |
893 | * @mode: mode for the new file if O_CREAT is set, else ignored | |
894 | * | |
895 | * This is the helper to open a file from kernelspace if you really | |
896 | * have to. But in generally you should not do this, so please move | |
897 | * along, nothing to see here.. | |
898 | */ | |
899 | struct file *file_open_name(struct filename *name, int flags, umode_t mode) | |
900 | { | |
901 | struct open_flags op; | |
902 | int lookup = build_open_flags(flags, mode, &op); | |
903 | return do_filp_open(AT_FDCWD, name, &op, lookup); | |
904 | } | |
905 | ||
47c805dc AV |
906 | /** |
907 | * filp_open - open file and return file pointer | |
908 | * | |
909 | * @filename: path to open | |
910 | * @flags: open flags as per the open(2) second argument | |
911 | * @mode: mode for the new file if O_CREAT is set, else ignored | |
912 | * | |
913 | * This is the helper to open a file from kernelspace if you really | |
914 | * have to. But in generally you should not do this, so please move | |
915 | * along, nothing to see here.. | |
916 | */ | |
a218d0fd | 917 | struct file *filp_open(const char *filename, int flags, umode_t mode) |
47c805dc | 918 | { |
669abf4e JL |
919 | struct filename name = {.name = filename}; |
920 | return file_open_name(&name, flags, mode); | |
47c805dc AV |
921 | } |
922 | EXPORT_SYMBOL(filp_open); | |
923 | ||
73d049a4 AV |
924 | struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
925 | const char *filename, int flags) | |
926 | { | |
927 | struct open_flags op; | |
928 | int lookup = build_open_flags(flags, 0, &op); | |
929 | if (flags & O_CREAT) | |
930 | return ERR_PTR(-EINVAL); | |
931 | if (!filename && (flags & O_DIRECTORY)) | |
932 | if (!dentry->d_inode->i_op->lookup) | |
933 | return ERR_PTR(-ENOTDIR); | |
934 | return do_file_open_root(dentry, mnt, filename, &op, lookup); | |
935 | } | |
936 | EXPORT_SYMBOL(file_open_root); | |
937 | ||
a218d0fd | 938 | long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode) |
1da177e4 | 939 | { |
47c805dc AV |
940 | struct open_flags op; |
941 | int lookup = build_open_flags(flags, mode, &op); | |
91a27b2a | 942 | struct filename *tmp = getname(filename); |
e922efc3 | 943 | int fd = PTR_ERR(tmp); |
1da177e4 | 944 | |
1da177e4 | 945 | if (!IS_ERR(tmp)) { |
f23513e8 | 946 | fd = get_unused_fd_flags(flags); |
1da177e4 | 947 | if (fd >= 0) { |
669abf4e | 948 | struct file *f = do_filp_open(dfd, tmp, &op, lookup); |
fed2fc18 TN |
949 | if (IS_ERR(f)) { |
950 | put_unused_fd(fd); | |
951 | fd = PTR_ERR(f); | |
952 | } else { | |
2a12a9d7 | 953 | fsnotify_open(f); |
fed2fc18 TN |
954 | fd_install(fd, f); |
955 | } | |
1da177e4 | 956 | } |
1da177e4 LT |
957 | putname(tmp); |
958 | } | |
959 | return fd; | |
1da177e4 | 960 | } |
e922efc3 | 961 | |
a218d0fd | 962 | SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) |
e922efc3 | 963 | { |
385910f2 LT |
964 | long ret; |
965 | ||
e922efc3 MS |
966 | if (force_o_largefile()) |
967 | flags |= O_LARGEFILE; | |
968 | ||
385910f2 LT |
969 | ret = do_sys_open(AT_FDCWD, filename, flags, mode); |
970 | /* avoid REGPARM breakage on x86: */ | |
54a01510 | 971 | asmlinkage_protect(3, ret, filename, flags, mode); |
385910f2 | 972 | return ret; |
e922efc3 | 973 | } |
1da177e4 | 974 | |
6559eed8 | 975 | SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, |
a218d0fd | 976 | umode_t, mode) |
5590ff0d | 977 | { |
385910f2 LT |
978 | long ret; |
979 | ||
5590ff0d UD |
980 | if (force_o_largefile()) |
981 | flags |= O_LARGEFILE; | |
982 | ||
385910f2 LT |
983 | ret = do_sys_open(dfd, filename, flags, mode); |
984 | /* avoid REGPARM breakage on x86: */ | |
54a01510 | 985 | asmlinkage_protect(4, ret, dfd, filename, flags, mode); |
385910f2 | 986 | return ret; |
5590ff0d | 987 | } |
5590ff0d | 988 | |
1da177e4 LT |
989 | #ifndef __alpha__ |
990 | ||
991 | /* | |
992 | * For backward compatibility? Maybe this should be moved | |
993 | * into arch/i386 instead? | |
994 | */ | |
a218d0fd | 995 | SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode) |
1da177e4 LT |
996 | { |
997 | return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode); | |
998 | } | |
999 | ||
1000 | #endif | |
1001 | ||
1002 | /* | |
1003 | * "id" is the POSIX thread ID. We use the | |
1004 | * files pointer for this.. | |
1005 | */ | |
1006 | int filp_close(struct file *filp, fl_owner_t id) | |
1007 | { | |
45778ca8 | 1008 | int retval = 0; |
1da177e4 LT |
1009 | |
1010 | if (!file_count(filp)) { | |
1011 | printk(KERN_ERR "VFS: Close: file count is 0\n"); | |
45778ca8 | 1012 | return 0; |
1da177e4 LT |
1013 | } |
1014 | ||
45778ca8 | 1015 | if (filp->f_op && filp->f_op->flush) |
75e1fcc0 | 1016 | retval = filp->f_op->flush(filp, id); |
1da177e4 | 1017 | |
1abf0c71 AV |
1018 | if (likely(!(filp->f_mode & FMODE_PATH))) { |
1019 | dnotify_flush(filp, id); | |
1020 | locks_remove_posix(filp, id); | |
1021 | } | |
1da177e4 LT |
1022 | fput(filp); |
1023 | return retval; | |
1024 | } | |
1025 | ||
1026 | EXPORT_SYMBOL(filp_close); | |
1027 | ||
1028 | /* | |
1029 | * Careful here! We test whether the file pointer is NULL before | |
1030 | * releasing the fd. This ensures that one clone task can't release | |
1031 | * an fd while another clone is opening it. | |
1032 | */ | |
ca013e94 | 1033 | SYSCALL_DEFINE1(close, unsigned int, fd) |
1da177e4 | 1034 | { |
483ce1d4 | 1035 | int retval = __close_fd(current->files, fd); |
ee731f4f EP |
1036 | |
1037 | /* can't restart close syscall because file table entry was cleared */ | |
1038 | if (unlikely(retval == -ERESTARTSYS || | |
1039 | retval == -ERESTARTNOINTR || | |
1040 | retval == -ERESTARTNOHAND || | |
1041 | retval == -ERESTART_RESTARTBLOCK)) | |
1042 | retval = -EINTR; | |
1043 | ||
1044 | return retval; | |
1da177e4 | 1045 | } |
1da177e4 LT |
1046 | EXPORT_SYMBOL(sys_close); |
1047 | ||
1048 | /* | |
1049 | * This routine simulates a hangup on the tty, to arrange that users | |
1050 | * are given clean terminals at login time. | |
1051 | */ | |
ca013e94 | 1052 | SYSCALL_DEFINE0(vhangup) |
1da177e4 LT |
1053 | { |
1054 | if (capable(CAP_SYS_TTY_CONFIG)) { | |
2cb5998b | 1055 | tty_vhangup_self(); |
1da177e4 LT |
1056 | return 0; |
1057 | } | |
1058 | return -EPERM; | |
1059 | } | |
1060 | ||
1061 | /* | |
1062 | * Called when an inode is about to be open. | |
1063 | * We use this to disallow opening large files on 32bit systems if | |
1064 | * the caller didn't specify O_LARGEFILE. On 64bit systems we force | |
1065 | * on this flag in sys_open. | |
1066 | */ | |
1067 | int generic_file_open(struct inode * inode, struct file * filp) | |
1068 | { | |
1069 | if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) | |
a9c62a18 | 1070 | return -EOVERFLOW; |
1da177e4 LT |
1071 | return 0; |
1072 | } | |
1073 | ||
1074 | EXPORT_SYMBOL(generic_file_open); | |
1075 | ||
1076 | /* | |
1077 | * This is used by subsystems that don't want seekable | |
06b1e104 DT |
1078 | * file descriptors. The function is not supposed to ever fail, the only |
1079 | * reason it returns an 'int' and not 'void' is so that it can be plugged | |
1080 | * directly into file_operations structure. | |
1da177e4 LT |
1081 | */ |
1082 | int nonseekable_open(struct inode *inode, struct file *filp) | |
1083 | { | |
1084 | filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); | |
1085 | return 0; | |
1086 | } | |
1087 | ||
1088 | EXPORT_SYMBOL(nonseekable_open); |