open.c 26 KB

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