open.c 25 KB

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