open.c 27 KB

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