open.c 24 KB

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