open.c 23 KB

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