open.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  628. int flags, struct file *f)
  629. {
  630. struct inode *inode;
  631. int error;
  632. f->f_flags = flags;
  633. f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
  634. FMODE_PREAD | FMODE_PWRITE;
  635. inode = dentry->d_inode;
  636. if (f->f_mode & FMODE_WRITE) {
  637. error = get_write_access(inode);
  638. if (error)
  639. goto cleanup_file;
  640. }
  641. f->f_mapping = inode->i_mapping;
  642. f->f_dentry = dentry;
  643. f->f_vfsmnt = mnt;
  644. f->f_pos = 0;
  645. f->f_op = fops_get(inode->i_fop);
  646. file_move(f, &inode->i_sb->s_files);
  647. if (f->f_op && f->f_op->open) {
  648. error = f->f_op->open(inode,f);
  649. if (error)
  650. goto cleanup_all;
  651. }
  652. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  653. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  654. /* NB: we're sure to have correct a_ops only after f_op->open */
  655. if (f->f_flags & O_DIRECT) {
  656. if (!f->f_mapping->a_ops ||
  657. ((!f->f_mapping->a_ops->direct_IO) &&
  658. (!f->f_mapping->a_ops->get_xip_page))) {
  659. fput(f);
  660. f = ERR_PTR(-EINVAL);
  661. }
  662. }
  663. return f;
  664. cleanup_all:
  665. fops_put(f->f_op);
  666. if (f->f_mode & FMODE_WRITE)
  667. put_write_access(inode);
  668. file_kill(f);
  669. f->f_dentry = NULL;
  670. f->f_vfsmnt = NULL;
  671. cleanup_file:
  672. put_filp(f);
  673. dput(dentry);
  674. mntput(mnt);
  675. return ERR_PTR(error);
  676. }
  677. /*
  678. * Note that while the flag value (low two bits) for sys_open means:
  679. * 00 - read-only
  680. * 01 - write-only
  681. * 10 - read-write
  682. * 11 - special
  683. * it is changed into
  684. * 00 - no permissions needed
  685. * 01 - read-permission
  686. * 10 - write-permission
  687. * 11 - read-write
  688. * for the internal routines (ie open_namei()/follow_link() etc). 00 is
  689. * used by symlinks.
  690. */
  691. struct file *filp_open(const char * filename, int flags, int mode)
  692. {
  693. int namei_flags, error;
  694. struct nameidata nd;
  695. struct file *f;
  696. namei_flags = flags;
  697. if ((namei_flags+1) & O_ACCMODE)
  698. namei_flags++;
  699. if (namei_flags & O_TRUNC)
  700. namei_flags |= 2;
  701. error = -ENFILE;
  702. f = get_empty_filp();
  703. if (f == NULL)
  704. return ERR_PTR(error);
  705. error = open_namei(filename, namei_flags, mode, &nd);
  706. if (!error)
  707. return __dentry_open(nd.dentry, nd.mnt, flags, f);
  708. put_filp(f);
  709. return ERR_PTR(error);
  710. }
  711. EXPORT_SYMBOL(filp_open);
  712. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
  713. {
  714. int error;
  715. struct file *f;
  716. error = -ENFILE;
  717. f = get_empty_filp();
  718. if (f == NULL)
  719. return ERR_PTR(error);
  720. return __dentry_open(dentry, mnt, flags, f);
  721. }
  722. EXPORT_SYMBOL(dentry_open);
  723. /*
  724. * Find an empty file descriptor entry, and mark it busy.
  725. */
  726. int get_unused_fd(void)
  727. {
  728. struct files_struct * files = current->files;
  729. int fd, error;
  730. struct fdtable *fdt;
  731. error = -EMFILE;
  732. spin_lock(&files->file_lock);
  733. repeat:
  734. fdt = files_fdtable(files);
  735. fd = find_next_zero_bit(fdt->open_fds->fds_bits,
  736. fdt->max_fdset,
  737. fdt->next_fd);
  738. /*
  739. * N.B. For clone tasks sharing a files structure, this test
  740. * will limit the total number of files that can be opened.
  741. */
  742. if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  743. goto out;
  744. /* Do we need to expand the fd array or fd set? */
  745. error = expand_files(files, fd);
  746. if (error < 0)
  747. goto out;
  748. if (error) {
  749. /*
  750. * If we needed to expand the fs array we
  751. * might have blocked - try again.
  752. */
  753. error = -EMFILE;
  754. goto repeat;
  755. }
  756. FD_SET(fd, fdt->open_fds);
  757. FD_CLR(fd, fdt->close_on_exec);
  758. fdt->next_fd = fd + 1;
  759. #if 1
  760. /* Sanity check */
  761. if (fdt->fd[fd] != NULL) {
  762. printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
  763. fdt->fd[fd] = NULL;
  764. }
  765. #endif
  766. error = fd;
  767. out:
  768. spin_unlock(&files->file_lock);
  769. return error;
  770. }
  771. EXPORT_SYMBOL(get_unused_fd);
  772. static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
  773. {
  774. struct fdtable *fdt = files_fdtable(files);
  775. __FD_CLR(fd, fdt->open_fds);
  776. if (fd < fdt->next_fd)
  777. fdt->next_fd = fd;
  778. }
  779. void fastcall put_unused_fd(unsigned int fd)
  780. {
  781. struct files_struct *files = current->files;
  782. spin_lock(&files->file_lock);
  783. __put_unused_fd(files, fd);
  784. spin_unlock(&files->file_lock);
  785. }
  786. EXPORT_SYMBOL(put_unused_fd);
  787. /*
  788. * Install a file pointer in the fd array.
  789. *
  790. * The VFS is full of places where we drop the files lock between
  791. * setting the open_fds bitmap and installing the file in the file
  792. * array. At any such point, we are vulnerable to a dup2() race
  793. * installing a file in the array before us. We need to detect this and
  794. * fput() the struct file we are about to overwrite in this case.
  795. *
  796. * It should never happen - if we allow dup2() do it, _really_ bad things
  797. * will follow.
  798. */
  799. void fastcall fd_install(unsigned int fd, struct file * file)
  800. {
  801. struct files_struct *files = current->files;
  802. struct fdtable *fdt;
  803. spin_lock(&files->file_lock);
  804. fdt = files_fdtable(files);
  805. BUG_ON(fdt->fd[fd] != NULL);
  806. rcu_assign_pointer(fdt->fd[fd], file);
  807. spin_unlock(&files->file_lock);
  808. }
  809. EXPORT_SYMBOL(fd_install);
  810. long do_sys_open(const char __user *filename, int flags, int mode)
  811. {
  812. char *tmp = getname(filename);
  813. int fd = PTR_ERR(tmp);
  814. if (!IS_ERR(tmp)) {
  815. fd = get_unused_fd();
  816. if (fd >= 0) {
  817. struct file *f = filp_open(tmp, flags, mode);
  818. if (IS_ERR(f)) {
  819. put_unused_fd(fd);
  820. fd = PTR_ERR(f);
  821. } else {
  822. fsnotify_open(f->f_dentry);
  823. fd_install(fd, f);
  824. }
  825. }
  826. putname(tmp);
  827. }
  828. return fd;
  829. }
  830. asmlinkage long sys_open(const char __user *filename, int flags, int mode)
  831. {
  832. if (force_o_largefile())
  833. flags |= O_LARGEFILE;
  834. return do_sys_open(filename, flags, mode);
  835. }
  836. EXPORT_SYMBOL_GPL(sys_open);
  837. #ifndef __alpha__
  838. /*
  839. * For backward compatibility? Maybe this should be moved
  840. * into arch/i386 instead?
  841. */
  842. asmlinkage long sys_creat(const char __user * pathname, int mode)
  843. {
  844. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  845. }
  846. #endif
  847. /*
  848. * "id" is the POSIX thread ID. We use the
  849. * files pointer for this..
  850. */
  851. int filp_close(struct file *filp, fl_owner_t id)
  852. {
  853. int retval = 0;
  854. if (!file_count(filp)) {
  855. printk(KERN_ERR "VFS: Close: file count is 0\n");
  856. return 0;
  857. }
  858. if (filp->f_op && filp->f_op->flush)
  859. retval = filp->f_op->flush(filp);
  860. dnotify_flush(filp, id);
  861. locks_remove_posix(filp, id);
  862. fput(filp);
  863. return retval;
  864. }
  865. EXPORT_SYMBOL(filp_close);
  866. /*
  867. * Careful here! We test whether the file pointer is NULL before
  868. * releasing the fd. This ensures that one clone task can't release
  869. * an fd while another clone is opening it.
  870. */
  871. asmlinkage long sys_close(unsigned int fd)
  872. {
  873. struct file * filp;
  874. struct files_struct *files = current->files;
  875. struct fdtable *fdt;
  876. spin_lock(&files->file_lock);
  877. fdt = files_fdtable(files);
  878. if (fd >= fdt->max_fds)
  879. goto out_unlock;
  880. filp = fdt->fd[fd];
  881. if (!filp)
  882. goto out_unlock;
  883. rcu_assign_pointer(fdt->fd[fd], NULL);
  884. FD_CLR(fd, fdt->close_on_exec);
  885. __put_unused_fd(files, fd);
  886. spin_unlock(&files->file_lock);
  887. return filp_close(filp, files);
  888. out_unlock:
  889. spin_unlock(&files->file_lock);
  890. return -EBADF;
  891. }
  892. EXPORT_SYMBOL(sys_close);
  893. /*
  894. * This routine simulates a hangup on the tty, to arrange that users
  895. * are given clean terminals at login time.
  896. */
  897. asmlinkage long sys_vhangup(void)
  898. {
  899. if (capable(CAP_SYS_TTY_CONFIG)) {
  900. tty_vhangup(current->signal->tty);
  901. return 0;
  902. }
  903. return -EPERM;
  904. }
  905. /*
  906. * Called when an inode is about to be open.
  907. * We use this to disallow opening large files on 32bit systems if
  908. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  909. * on this flag in sys_open.
  910. */
  911. int generic_file_open(struct inode * inode, struct file * filp)
  912. {
  913. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  914. return -EFBIG;
  915. return 0;
  916. }
  917. EXPORT_SYMBOL(generic_file_open);
  918. /*
  919. * This is used by subsystems that don't want seekable
  920. * file descriptors
  921. */
  922. int nonseekable_open(struct inode *inode, struct file *filp)
  923. {
  924. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  925. return 0;
  926. }
  927. EXPORT_SYMBOL(nonseekable_open);