open.c 23 KB

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