open.c 27 KB

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