open.c 24 KB

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