open.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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/quotaops.h>
  10. #include <linux/fsnotify.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/tty.h>
  14. #include <linux/namei.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/capability.h>
  17. #include <linux/security.h>
  18. #include <linux/mount.h>
  19. #include <linux/vfs.h>
  20. #include <linux/fcntl.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. int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  29. {
  30. int retval = -ENODEV;
  31. if (dentry) {
  32. retval = -ENOSYS;
  33. if (dentry->d_sb->s_op->statfs) {
  34. memset(buf, 0, sizeof(*buf));
  35. retval = security_sb_statfs(dentry);
  36. if (retval)
  37. return retval;
  38. retval = dentry->d_sb->s_op->statfs(dentry, buf);
  39. if (retval == 0 && buf->f_frsize == 0)
  40. buf->f_frsize = buf->f_bsize;
  41. }
  42. }
  43. return retval;
  44. }
  45. EXPORT_SYMBOL(vfs_statfs);
  46. static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
  47. {
  48. struct kstatfs st;
  49. int retval;
  50. retval = vfs_statfs(dentry, &st);
  51. if (retval)
  52. return retval;
  53. if (sizeof(*buf) == sizeof(st))
  54. memcpy(buf, &st, sizeof(st));
  55. else {
  56. if (sizeof buf->f_blocks == 4) {
  57. if ((st.f_blocks | st.f_bfree | st.f_bavail) &
  58. 0xffffffff00000000ULL)
  59. return -EOVERFLOW;
  60. /*
  61. * f_files and f_ffree may be -1; it's okay to stuff
  62. * that into 32 bits
  63. */
  64. if (st.f_files != -1 &&
  65. (st.f_files & 0xffffffff00000000ULL))
  66. return -EOVERFLOW;
  67. if (st.f_ffree != -1 &&
  68. (st.f_ffree & 0xffffffff00000000ULL))
  69. return -EOVERFLOW;
  70. }
  71. buf->f_type = st.f_type;
  72. buf->f_bsize = st.f_bsize;
  73. buf->f_blocks = st.f_blocks;
  74. buf->f_bfree = st.f_bfree;
  75. buf->f_bavail = st.f_bavail;
  76. buf->f_files = st.f_files;
  77. buf->f_ffree = st.f_ffree;
  78. buf->f_fsid = st.f_fsid;
  79. buf->f_namelen = st.f_namelen;
  80. buf->f_frsize = st.f_frsize;
  81. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  82. }
  83. return 0;
  84. }
  85. static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
  86. {
  87. struct kstatfs st;
  88. int retval;
  89. retval = vfs_statfs(dentry, &st);
  90. if (retval)
  91. return retval;
  92. if (sizeof(*buf) == sizeof(st))
  93. memcpy(buf, &st, sizeof(st));
  94. else {
  95. buf->f_type = st.f_type;
  96. buf->f_bsize = st.f_bsize;
  97. buf->f_blocks = st.f_blocks;
  98. buf->f_bfree = st.f_bfree;
  99. buf->f_bavail = st.f_bavail;
  100. buf->f_files = st.f_files;
  101. buf->f_ffree = st.f_ffree;
  102. buf->f_fsid = st.f_fsid;
  103. buf->f_namelen = st.f_namelen;
  104. buf->f_frsize = st.f_frsize;
  105. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  106. }
  107. return 0;
  108. }
  109. asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
  110. {
  111. struct nameidata nd;
  112. int error;
  113. error = user_path_walk(path, &nd);
  114. if (!error) {
  115. struct statfs tmp;
  116. error = vfs_statfs_native(nd.dentry, &tmp);
  117. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  118. error = -EFAULT;
  119. path_release(&nd);
  120. }
  121. return error;
  122. }
  123. asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
  124. {
  125. struct nameidata nd;
  126. long error;
  127. if (sz != sizeof(*buf))
  128. return -EINVAL;
  129. error = user_path_walk(path, &nd);
  130. if (!error) {
  131. struct statfs64 tmp;
  132. error = vfs_statfs64(nd.dentry, &tmp);
  133. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  134. error = -EFAULT;
  135. path_release(&nd);
  136. }
  137. return error;
  138. }
  139. asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
  140. {
  141. struct file * file;
  142. struct statfs tmp;
  143. int error;
  144. error = -EBADF;
  145. file = fget(fd);
  146. if (!file)
  147. goto out;
  148. error = vfs_statfs_native(file->f_path.dentry, &tmp);
  149. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  150. error = -EFAULT;
  151. fput(file);
  152. out:
  153. return error;
  154. }
  155. asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
  156. {
  157. struct file * file;
  158. struct statfs64 tmp;
  159. int error;
  160. if (sz != sizeof(*buf))
  161. return -EINVAL;
  162. error = -EBADF;
  163. file = fget(fd);
  164. if (!file)
  165. goto out;
  166. error = vfs_statfs64(file->f_path.dentry, &tmp);
  167. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  168. error = -EFAULT;
  169. fput(file);
  170. out:
  171. return error;
  172. }
  173. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  174. struct file *filp)
  175. {
  176. int err;
  177. struct iattr newattrs;
  178. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  179. if (length < 0)
  180. return -EINVAL;
  181. newattrs.ia_size = length;
  182. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  183. if (filp) {
  184. newattrs.ia_file = filp;
  185. newattrs.ia_valid |= ATTR_FILE;
  186. }
  187. /* Remove suid/sgid on truncate too */
  188. newattrs.ia_valid |= should_remove_suid(dentry);
  189. mutex_lock(&dentry->d_inode->i_mutex);
  190. err = notify_change(dentry, &newattrs);
  191. mutex_unlock(&dentry->d_inode->i_mutex);
  192. return err;
  193. }
  194. static long do_sys_truncate(const char __user * path, loff_t length)
  195. {
  196. struct nameidata nd;
  197. struct inode * inode;
  198. int error;
  199. error = -EINVAL;
  200. if (length < 0) /* sorry, but loff_t says... */
  201. goto out;
  202. error = user_path_walk(path, &nd);
  203. if (error)
  204. goto out;
  205. inode = nd.dentry->d_inode;
  206. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  207. error = -EISDIR;
  208. if (S_ISDIR(inode->i_mode))
  209. goto dput_and_out;
  210. error = -EINVAL;
  211. if (!S_ISREG(inode->i_mode))
  212. goto dput_and_out;
  213. error = vfs_permission(&nd, MAY_WRITE);
  214. if (error)
  215. goto dput_and_out;
  216. error = -EROFS;
  217. if (IS_RDONLY(inode))
  218. goto dput_and_out;
  219. error = -EPERM;
  220. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  221. goto dput_and_out;
  222. /*
  223. * Make sure that there are no leases.
  224. */
  225. error = break_lease(inode, FMODE_WRITE);
  226. if (error)
  227. goto dput_and_out;
  228. error = get_write_access(inode);
  229. if (error)
  230. goto dput_and_out;
  231. error = locks_verify_truncate(inode, NULL, length);
  232. if (!error) {
  233. DQUOT_INIT(inode);
  234. error = do_truncate(nd.dentry, length, 0, NULL);
  235. }
  236. put_write_access(inode);
  237. dput_and_out:
  238. path_release(&nd);
  239. out:
  240. return error;
  241. }
  242. asmlinkage long sys_truncate(const char __user * path, unsigned long length)
  243. {
  244. /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
  245. return do_sys_truncate(path, (long)length);
  246. }
  247. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  248. {
  249. struct inode * inode;
  250. struct dentry *dentry;
  251. struct file * file;
  252. int error;
  253. error = -EINVAL;
  254. if (length < 0)
  255. goto out;
  256. error = -EBADF;
  257. file = fget(fd);
  258. if (!file)
  259. goto out;
  260. /* explicitly opened as large or we are on 64-bit box */
  261. if (file->f_flags & O_LARGEFILE)
  262. small = 0;
  263. dentry = file->f_path.dentry;
  264. inode = dentry->d_inode;
  265. error = -EINVAL;
  266. if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
  267. goto out_putf;
  268. error = -EINVAL;
  269. /* Cannot ftruncate over 2^31 bytes without large file support */
  270. if (small && length > MAX_NON_LFS)
  271. goto out_putf;
  272. error = -EPERM;
  273. if (IS_APPEND(inode))
  274. goto out_putf;
  275. error = locks_verify_truncate(inode, file, length);
  276. if (!error)
  277. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
  278. out_putf:
  279. fput(file);
  280. out:
  281. return error;
  282. }
  283. asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
  284. {
  285. long ret = do_sys_ftruncate(fd, length, 1);
  286. /* avoid REGPARM breakage on x86: */
  287. prevent_tail_call(ret);
  288. return ret;
  289. }
  290. /* LFS versions of truncate are only needed on 32 bit machines */
  291. #if BITS_PER_LONG == 32
  292. asmlinkage long sys_truncate64(const char __user * path, loff_t length)
  293. {
  294. return do_sys_truncate(path, length);
  295. }
  296. asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
  297. {
  298. long ret = do_sys_ftruncate(fd, length, 0);
  299. /* avoid REGPARM breakage on x86: */
  300. prevent_tail_call(ret);
  301. return ret;
  302. }
  303. #endif
  304. /*
  305. * access() needs to use the real uid/gid, not the effective uid/gid.
  306. * We do this by temporarily clearing all FS-related capabilities and
  307. * switching the fsuid/fsgid around to the real ones.
  308. */
  309. asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
  310. {
  311. struct nameidata nd;
  312. int old_fsuid, old_fsgid;
  313. kernel_cap_t old_cap;
  314. int res;
  315. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  316. return -EINVAL;
  317. old_fsuid = current->fsuid;
  318. old_fsgid = current->fsgid;
  319. old_cap = current->cap_effective;
  320. current->fsuid = current->uid;
  321. current->fsgid = current->gid;
  322. /*
  323. * Clear the capabilities if we switch to a non-root user
  324. *
  325. * FIXME: There is a race here against sys_capset. The
  326. * capabilities can change yet we will restore the old
  327. * value below. We should hold task_capabilities_lock,
  328. * but we cannot because user_path_walk can sleep.
  329. */
  330. if (current->uid)
  331. cap_clear(current->cap_effective);
  332. else
  333. current->cap_effective = current->cap_permitted;
  334. res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
  335. if (res)
  336. goto out;
  337. res = vfs_permission(&nd, mode);
  338. /* SuS v2 requires we report a read only fs too */
  339. if(res || !(mode & S_IWOTH) ||
  340. special_file(nd.dentry->d_inode->i_mode))
  341. goto out_path_release;
  342. if(IS_RDONLY(nd.dentry->d_inode))
  343. res = -EROFS;
  344. out_path_release:
  345. path_release(&nd);
  346. out:
  347. current->fsuid = old_fsuid;
  348. current->fsgid = old_fsgid;
  349. current->cap_effective = old_cap;
  350. return res;
  351. }
  352. asmlinkage long sys_access(const char __user *filename, int mode)
  353. {
  354. return sys_faccessat(AT_FDCWD, filename, mode);
  355. }
  356. asmlinkage long sys_chdir(const char __user * filename)
  357. {
  358. struct nameidata nd;
  359. int error;
  360. error = __user_walk(filename,
  361. LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
  362. if (error)
  363. goto out;
  364. error = vfs_permission(&nd, MAY_EXEC);
  365. if (error)
  366. goto dput_and_out;
  367. set_fs_pwd(current->fs, nd.mnt, nd.dentry);
  368. dput_and_out:
  369. path_release(&nd);
  370. out:
  371. return error;
  372. }
  373. asmlinkage long sys_fchdir(unsigned int fd)
  374. {
  375. struct file *file;
  376. struct dentry *dentry;
  377. struct inode *inode;
  378. struct vfsmount *mnt;
  379. int error;
  380. error = -EBADF;
  381. file = fget(fd);
  382. if (!file)
  383. goto out;
  384. dentry = file->f_path.dentry;
  385. mnt = file->f_path.mnt;
  386. inode = dentry->d_inode;
  387. error = -ENOTDIR;
  388. if (!S_ISDIR(inode->i_mode))
  389. goto out_putf;
  390. error = file_permission(file, MAY_EXEC);
  391. if (!error)
  392. set_fs_pwd(current->fs, mnt, dentry);
  393. out_putf:
  394. fput(file);
  395. out:
  396. return error;
  397. }
  398. asmlinkage long sys_chroot(const char __user * filename)
  399. {
  400. struct nameidata nd;
  401. int error;
  402. error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
  403. if (error)
  404. goto out;
  405. error = vfs_permission(&nd, MAY_EXEC);
  406. if (error)
  407. goto dput_and_out;
  408. error = -EPERM;
  409. if (!capable(CAP_SYS_CHROOT))
  410. goto dput_and_out;
  411. set_fs_root(current->fs, nd.mnt, nd.dentry);
  412. set_fs_altroot();
  413. error = 0;
  414. dput_and_out:
  415. path_release(&nd);
  416. out:
  417. return error;
  418. }
  419. asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
  420. {
  421. struct inode * inode;
  422. struct dentry * dentry;
  423. struct file * file;
  424. int err = -EBADF;
  425. struct iattr newattrs;
  426. file = fget(fd);
  427. if (!file)
  428. goto out;
  429. dentry = file->f_path.dentry;
  430. inode = dentry->d_inode;
  431. audit_inode(NULL, inode);
  432. err = -EROFS;
  433. if (IS_RDONLY(inode))
  434. goto out_putf;
  435. err = -EPERM;
  436. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  437. goto out_putf;
  438. mutex_lock(&inode->i_mutex);
  439. if (mode == (mode_t) -1)
  440. mode = inode->i_mode;
  441. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  442. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  443. err = notify_change(dentry, &newattrs);
  444. mutex_unlock(&inode->i_mutex);
  445. out_putf:
  446. fput(file);
  447. out:
  448. return err;
  449. }
  450. asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
  451. mode_t mode)
  452. {
  453. struct nameidata nd;
  454. struct inode * inode;
  455. int error;
  456. struct iattr newattrs;
  457. error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
  458. if (error)
  459. goto out;
  460. inode = nd.dentry->d_inode;
  461. error = -EROFS;
  462. if (IS_RDONLY(inode))
  463. goto dput_and_out;
  464. error = -EPERM;
  465. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  466. goto dput_and_out;
  467. mutex_lock(&inode->i_mutex);
  468. if (mode == (mode_t) -1)
  469. mode = inode->i_mode;
  470. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  471. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  472. error = notify_change(nd.dentry, &newattrs);
  473. mutex_unlock(&inode->i_mutex);
  474. dput_and_out:
  475. path_release(&nd);
  476. out:
  477. return error;
  478. }
  479. asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
  480. {
  481. return sys_fchmodat(AT_FDCWD, filename, mode);
  482. }
  483. static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
  484. {
  485. struct inode * inode;
  486. int error;
  487. struct iattr newattrs;
  488. error = -ENOENT;
  489. if (!(inode = dentry->d_inode)) {
  490. printk(KERN_ERR "chown_common: NULL inode\n");
  491. goto out;
  492. }
  493. error = -EROFS;
  494. if (IS_RDONLY(inode))
  495. goto out;
  496. error = -EPERM;
  497. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  498. goto out;
  499. newattrs.ia_valid = ATTR_CTIME;
  500. if (user != (uid_t) -1) {
  501. newattrs.ia_valid |= ATTR_UID;
  502. newattrs.ia_uid = user;
  503. }
  504. if (group != (gid_t) -1) {
  505. newattrs.ia_valid |= ATTR_GID;
  506. newattrs.ia_gid = group;
  507. }
  508. if (!S_ISDIR(inode->i_mode))
  509. newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
  510. mutex_lock(&inode->i_mutex);
  511. error = notify_change(dentry, &newattrs);
  512. mutex_unlock(&inode->i_mutex);
  513. out:
  514. return error;
  515. }
  516. asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
  517. {
  518. struct nameidata nd;
  519. int error;
  520. error = user_path_walk(filename, &nd);
  521. if (error)
  522. goto out;
  523. error = chown_common(nd.dentry, user, group);
  524. path_release(&nd);
  525. out:
  526. return error;
  527. }
  528. asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
  529. gid_t group, int flag)
  530. {
  531. struct nameidata nd;
  532. int error = -EINVAL;
  533. int follow;
  534. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  535. goto out;
  536. follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  537. error = __user_walk_fd(dfd, filename, follow, &nd);
  538. if (error)
  539. goto out;
  540. error = chown_common(nd.dentry, user, group);
  541. path_release(&nd);
  542. out:
  543. return error;
  544. }
  545. asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
  546. {
  547. struct nameidata nd;
  548. int error;
  549. error = user_path_walk_link(filename, &nd);
  550. if (error)
  551. goto out;
  552. error = chown_common(nd.dentry, user, group);
  553. path_release(&nd);
  554. out:
  555. return error;
  556. }
  557. asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
  558. {
  559. struct file * file;
  560. int error = -EBADF;
  561. struct dentry * dentry;
  562. file = fget(fd);
  563. if (!file)
  564. goto out;
  565. dentry = file->f_path.dentry;
  566. audit_inode(NULL, dentry->d_inode);
  567. error = chown_common(dentry, user, group);
  568. fput(file);
  569. out:
  570. return error;
  571. }
  572. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  573. int flags, struct file *f,
  574. int (*open)(struct inode *, struct file *))
  575. {
  576. struct inode *inode;
  577. int error;
  578. f->f_flags = flags;
  579. f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
  580. FMODE_PREAD | FMODE_PWRITE;
  581. inode = dentry->d_inode;
  582. if (f->f_mode & FMODE_WRITE) {
  583. error = get_write_access(inode);
  584. if (error)
  585. goto cleanup_file;
  586. }
  587. f->f_mapping = inode->i_mapping;
  588. f->f_path.dentry = dentry;
  589. f->f_path.mnt = mnt;
  590. f->f_pos = 0;
  591. f->f_op = fops_get(inode->i_fop);
  592. file_move(f, &inode->i_sb->s_files);
  593. if (!open && f->f_op)
  594. open = f->f_op->open;
  595. if (open) {
  596. error = open(inode, f);
  597. if (error)
  598. goto cleanup_all;
  599. }
  600. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  601. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  602. /* NB: we're sure to have correct a_ops only after f_op->open */
  603. if (f->f_flags & O_DIRECT) {
  604. if (!f->f_mapping->a_ops ||
  605. ((!f->f_mapping->a_ops->direct_IO) &&
  606. (!f->f_mapping->a_ops->get_xip_page))) {
  607. fput(f);
  608. f = ERR_PTR(-EINVAL);
  609. }
  610. }
  611. return f;
  612. cleanup_all:
  613. fops_put(f->f_op);
  614. if (f->f_mode & FMODE_WRITE)
  615. put_write_access(inode);
  616. file_kill(f);
  617. f->f_path.dentry = NULL;
  618. f->f_path.mnt = NULL;
  619. cleanup_file:
  620. put_filp(f);
  621. dput(dentry);
  622. mntput(mnt);
  623. return ERR_PTR(error);
  624. }
  625. /*
  626. * Note that while the flag value (low two bits) for sys_open means:
  627. * 00 - read-only
  628. * 01 - write-only
  629. * 10 - read-write
  630. * 11 - special
  631. * it is changed into
  632. * 00 - no permissions needed
  633. * 01 - read-permission
  634. * 10 - write-permission
  635. * 11 - read-write
  636. * for the internal routines (ie open_namei()/follow_link() etc). 00 is
  637. * used by symlinks.
  638. */
  639. static struct file *do_filp_open(int dfd, const char *filename, int flags,
  640. int mode)
  641. {
  642. int namei_flags, error;
  643. struct nameidata nd;
  644. namei_flags = flags;
  645. if ((namei_flags+1) & O_ACCMODE)
  646. namei_flags++;
  647. error = open_namei(dfd, filename, namei_flags, mode, &nd);
  648. if (!error)
  649. return nameidata_to_filp(&nd, flags);
  650. return ERR_PTR(error);
  651. }
  652. struct file *filp_open(const char *filename, int flags, int mode)
  653. {
  654. return do_filp_open(AT_FDCWD, filename, flags, mode);
  655. }
  656. EXPORT_SYMBOL(filp_open);
  657. /**
  658. * lookup_instantiate_filp - instantiates the open intent filp
  659. * @nd: pointer to nameidata
  660. * @dentry: pointer to dentry
  661. * @open: open callback
  662. *
  663. * Helper for filesystems that want to use lookup open intents and pass back
  664. * a fully instantiated struct file to the caller.
  665. * This function is meant to be called from within a filesystem's
  666. * lookup method.
  667. * Beware of calling it for non-regular files! Those ->open methods might block
  668. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  669. * leading to a deadlock, as nobody can open that fifo anymore, because
  670. * another process to open fifo will block on locked parent when doing lookup).
  671. * Note that in case of error, nd->intent.open.file is destroyed, but the
  672. * path information remains valid.
  673. * If the open callback is set to NULL, then the standard f_op->open()
  674. * filesystem callback is substituted.
  675. */
  676. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  677. int (*open)(struct inode *, struct file *))
  678. {
  679. if (IS_ERR(nd->intent.open.file))
  680. goto out;
  681. if (IS_ERR(dentry))
  682. goto out_err;
  683. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
  684. nd->intent.open.flags - 1,
  685. nd->intent.open.file,
  686. open);
  687. out:
  688. return nd->intent.open.file;
  689. out_err:
  690. release_open_intent(nd);
  691. nd->intent.open.file = (struct file *)dentry;
  692. goto out;
  693. }
  694. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  695. /**
  696. * nameidata_to_filp - convert a nameidata to an open filp.
  697. * @nd: pointer to nameidata
  698. * @flags: open flags
  699. *
  700. * Note that this function destroys the original nameidata
  701. */
  702. struct file *nameidata_to_filp(struct nameidata *nd, int flags)
  703. {
  704. struct file *filp;
  705. /* Pick up the filp from the open intent */
  706. filp = nd->intent.open.file;
  707. /* Has the filesystem initialised the file for us? */
  708. if (filp->f_path.dentry == NULL)
  709. filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
  710. else
  711. path_release(nd);
  712. return filp;
  713. }
  714. /*
  715. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  716. * error.
  717. */
  718. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
  719. {
  720. int error;
  721. struct file *f;
  722. error = -ENFILE;
  723. f = get_empty_filp();
  724. if (f == NULL) {
  725. dput(dentry);
  726. mntput(mnt);
  727. return ERR_PTR(error);
  728. }
  729. return __dentry_open(dentry, mnt, flags, f, NULL);
  730. }
  731. EXPORT_SYMBOL(dentry_open);
  732. /*
  733. * Find an empty file descriptor entry, and mark it busy.
  734. */
  735. int get_unused_fd(void)
  736. {
  737. struct files_struct * files = current->files;
  738. int fd, error;
  739. struct fdtable *fdt;
  740. error = -EMFILE;
  741. spin_lock(&files->file_lock);
  742. repeat:
  743. fdt = files_fdtable(files);
  744. fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
  745. files->next_fd);
  746. /*
  747. * N.B. For clone tasks sharing a files structure, this test
  748. * will limit the total number of files that can be opened.
  749. */
  750. if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  751. goto out;
  752. /* Do we need to expand the fd array or fd set? */
  753. error = expand_files(files, fd);
  754. if (error < 0)
  755. goto out;
  756. if (error) {
  757. /*
  758. * If we needed to expand the fs array we
  759. * might have blocked - try again.
  760. */
  761. error = -EMFILE;
  762. goto repeat;
  763. }
  764. FD_SET(fd, fdt->open_fds);
  765. FD_CLR(fd, fdt->close_on_exec);
  766. files->next_fd = fd + 1;
  767. #if 1
  768. /* Sanity check */
  769. if (fdt->fd[fd] != NULL) {
  770. printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
  771. fdt->fd[fd] = NULL;
  772. }
  773. #endif
  774. error = fd;
  775. out:
  776. spin_unlock(&files->file_lock);
  777. return error;
  778. }
  779. EXPORT_SYMBOL(get_unused_fd);
  780. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  781. {
  782. struct fdtable *fdt = files_fdtable(files);
  783. __FD_CLR(fd, fdt->open_fds);
  784. if (fd < files->next_fd)
  785. files->next_fd = fd;
  786. }
  787. void fastcall put_unused_fd(unsigned int fd)
  788. {
  789. struct files_struct *files = current->files;
  790. spin_lock(&files->file_lock);
  791. __put_unused_fd(files, fd);
  792. spin_unlock(&files->file_lock);
  793. }
  794. EXPORT_SYMBOL(put_unused_fd);
  795. /*
  796. * Install a file pointer in the fd array.
  797. *
  798. * The VFS is full of places where we drop the files lock between
  799. * setting the open_fds bitmap and installing the file in the file
  800. * array. At any such point, we are vulnerable to a dup2() race
  801. * installing a file in the array before us. We need to detect this and
  802. * fput() the struct file we are about to overwrite in this case.
  803. *
  804. * It should never happen - if we allow dup2() do it, _really_ bad things
  805. * will follow.
  806. */
  807. void fastcall fd_install(unsigned int fd, struct file * file)
  808. {
  809. struct files_struct *files = current->files;
  810. struct fdtable *fdt;
  811. spin_lock(&files->file_lock);
  812. fdt = files_fdtable(files);
  813. BUG_ON(fdt->fd[fd] != NULL);
  814. rcu_assign_pointer(fdt->fd[fd], file);
  815. spin_unlock(&files->file_lock);
  816. }
  817. EXPORT_SYMBOL(fd_install);
  818. long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
  819. {
  820. char *tmp = getname(filename);
  821. int fd = PTR_ERR(tmp);
  822. if (!IS_ERR(tmp)) {
  823. fd = get_unused_fd();
  824. if (fd >= 0) {
  825. struct file *f = do_filp_open(dfd, tmp, flags, mode);
  826. if (IS_ERR(f)) {
  827. put_unused_fd(fd);
  828. fd = PTR_ERR(f);
  829. } else {
  830. fsnotify_open(f->f_path.dentry);
  831. fd_install(fd, f);
  832. }
  833. }
  834. putname(tmp);
  835. }
  836. return fd;
  837. }
  838. asmlinkage long sys_open(const char __user *filename, int flags, int mode)
  839. {
  840. long ret;
  841. if (force_o_largefile())
  842. flags |= O_LARGEFILE;
  843. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  844. /* avoid REGPARM breakage on x86: */
  845. prevent_tail_call(ret);
  846. return ret;
  847. }
  848. EXPORT_SYMBOL_GPL(sys_open);
  849. asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
  850. int mode)
  851. {
  852. long ret;
  853. if (force_o_largefile())
  854. flags |= O_LARGEFILE;
  855. ret = do_sys_open(dfd, filename, flags, mode);
  856. /* avoid REGPARM breakage on x86: */
  857. prevent_tail_call(ret);
  858. return ret;
  859. }
  860. #ifndef __alpha__
  861. /*
  862. * For backward compatibility? Maybe this should be moved
  863. * into arch/i386 instead?
  864. */
  865. asmlinkage long sys_creat(const char __user * pathname, int mode)
  866. {
  867. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  868. }
  869. #endif
  870. /*
  871. * "id" is the POSIX thread ID. We use the
  872. * files pointer for this..
  873. */
  874. int filp_close(struct file *filp, fl_owner_t id)
  875. {
  876. int retval = 0;
  877. if (!file_count(filp)) {
  878. printk(KERN_ERR "VFS: Close: file count is 0\n");
  879. return 0;
  880. }
  881. if (filp->f_op && filp->f_op->flush)
  882. retval = filp->f_op->flush(filp, id);
  883. dnotify_flush(filp, id);
  884. locks_remove_posix(filp, id);
  885. fput(filp);
  886. return retval;
  887. }
  888. EXPORT_SYMBOL(filp_close);
  889. /*
  890. * Careful here! We test whether the file pointer is NULL before
  891. * releasing the fd. This ensures that one clone task can't release
  892. * an fd while another clone is opening it.
  893. */
  894. asmlinkage long sys_close(unsigned int fd)
  895. {
  896. struct file * filp;
  897. struct files_struct *files = current->files;
  898. struct fdtable *fdt;
  899. int retval;
  900. spin_lock(&files->file_lock);
  901. fdt = files_fdtable(files);
  902. if (fd >= fdt->max_fds)
  903. goto out_unlock;
  904. filp = fdt->fd[fd];
  905. if (!filp)
  906. goto out_unlock;
  907. rcu_assign_pointer(fdt->fd[fd], NULL);
  908. FD_CLR(fd, fdt->close_on_exec);
  909. __put_unused_fd(files, fd);
  910. spin_unlock(&files->file_lock);
  911. retval = filp_close(filp, files);
  912. /* can't restart close syscall because file table entry was cleared */
  913. if (unlikely(retval == -ERESTARTSYS ||
  914. retval == -ERESTARTNOINTR ||
  915. retval == -ERESTARTNOHAND ||
  916. retval == -ERESTART_RESTARTBLOCK))
  917. retval = -EINTR;
  918. return retval;
  919. out_unlock:
  920. spin_unlock(&files->file_lock);
  921. return -EBADF;
  922. }
  923. EXPORT_SYMBOL(sys_close);
  924. /*
  925. * This routine simulates a hangup on the tty, to arrange that users
  926. * are given clean terminals at login time.
  927. */
  928. asmlinkage long sys_vhangup(void)
  929. {
  930. if (capable(CAP_SYS_TTY_CONFIG)) {
  931. /* XXX: this needs locking */
  932. tty_vhangup(current->signal->tty);
  933. return 0;
  934. }
  935. return -EPERM;
  936. }
  937. /*
  938. * Called when an inode is about to be open.
  939. * We use this to disallow opening large files on 32bit systems if
  940. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  941. * on this flag in sys_open.
  942. */
  943. int generic_file_open(struct inode * inode, struct file * filp)
  944. {
  945. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  946. return -EFBIG;
  947. return 0;
  948. }
  949. EXPORT_SYMBOL(generic_file_open);
  950. /*
  951. * This is used by subsystems that don't want seekable
  952. * file descriptors
  953. */
  954. int nonseekable_open(struct inode *inode, struct file *filp)
  955. {
  956. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  957. return 0;
  958. }
  959. EXPORT_SYMBOL(nonseekable_open);