open.c 25 KB

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