open.c 26 KB

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