open.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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/file.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/quotaops.h>
  11. #include <linux/fsnotify.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/namei.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/capability.h>
  18. #include <linux/security.h>
  19. #include <linux/mount.h>
  20. #include <linux/vfs.h>
  21. #include <linux/fcntl.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 <linux/audit.h>
  29. #include <linux/falloc.h>
  30. int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  31. {
  32. int retval = -ENODEV;
  33. if (dentry) {
  34. retval = -ENOSYS;
  35. if (dentry->d_sb->s_op->statfs) {
  36. memset(buf, 0, sizeof(*buf));
  37. retval = security_sb_statfs(dentry);
  38. if (retval)
  39. return retval;
  40. retval = dentry->d_sb->s_op->statfs(dentry, 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 dentry *dentry, struct statfs *buf)
  49. {
  50. struct kstatfs st;
  51. int retval;
  52. retval = vfs_statfs(dentry, &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 dentry *dentry, struct statfs64 *buf)
  88. {
  89. struct kstatfs st;
  90. int retval;
  91. retval = vfs_statfs(dentry, &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.path.dentry, &tmp);
  119. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  120. error = -EFAULT;
  121. path_put(&nd.path);
  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.path.dentry, &tmp);
  135. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  136. error = -EFAULT;
  137. path_put(&nd.path);
  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_path.dentry, &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_path.dentry, &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. /* Remove suid/sgid on truncate too */
  190. newattrs.ia_valid |= should_remove_suid(dentry);
  191. mutex_lock(&dentry->d_inode->i_mutex);
  192. err = notify_change(dentry, &newattrs);
  193. mutex_unlock(&dentry->d_inode->i_mutex);
  194. return err;
  195. }
  196. static long do_sys_truncate(const char __user * path, loff_t length)
  197. {
  198. struct nameidata nd;
  199. struct inode * inode;
  200. int error;
  201. error = -EINVAL;
  202. if (length < 0) /* sorry, but loff_t says... */
  203. goto out;
  204. error = user_path_walk(path, &nd);
  205. if (error)
  206. goto out;
  207. inode = nd.path.dentry->d_inode;
  208. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  209. error = -EISDIR;
  210. if (S_ISDIR(inode->i_mode))
  211. goto dput_and_out;
  212. error = -EINVAL;
  213. if (!S_ISREG(inode->i_mode))
  214. goto dput_and_out;
  215. error = mnt_want_write(nd.path.mnt);
  216. if (error)
  217. goto dput_and_out;
  218. error = vfs_permission(&nd, MAY_WRITE);
  219. if (error)
  220. goto mnt_drop_write_and_out;
  221. error = -EPERM;
  222. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  223. goto mnt_drop_write_and_out;
  224. error = get_write_access(inode);
  225. if (error)
  226. goto mnt_drop_write_and_out;
  227. /*
  228. * Make sure that there are no leases. get_write_access() protects
  229. * against the truncate racing with a lease-granting setlease().
  230. */
  231. error = break_lease(inode, FMODE_WRITE);
  232. if (error)
  233. goto put_write_and_out;
  234. error = locks_verify_truncate(inode, NULL, length);
  235. if (!error) {
  236. DQUOT_INIT(inode);
  237. error = do_truncate(nd.path.dentry, length, 0, NULL);
  238. }
  239. put_write_and_out:
  240. put_write_access(inode);
  241. mnt_drop_write_and_out:
  242. mnt_drop_write(nd.path.mnt);
  243. dput_and_out:
  244. path_put(&nd.path);
  245. out:
  246. return error;
  247. }
  248. asmlinkage long sys_truncate(const char __user * path, unsigned long length)
  249. {
  250. /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
  251. return do_sys_truncate(path, (long)length);
  252. }
  253. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  254. {
  255. struct inode * inode;
  256. struct dentry *dentry;
  257. struct file * file;
  258. int error;
  259. error = -EINVAL;
  260. if (length < 0)
  261. goto out;
  262. error = -EBADF;
  263. file = fget(fd);
  264. if (!file)
  265. goto out;
  266. /* explicitly opened as large or we are on 64-bit box */
  267. if (file->f_flags & O_LARGEFILE)
  268. small = 0;
  269. dentry = file->f_path.dentry;
  270. inode = dentry->d_inode;
  271. error = -EINVAL;
  272. if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
  273. goto out_putf;
  274. error = -EINVAL;
  275. /* Cannot ftruncate over 2^31 bytes without large file support */
  276. if (small && length > MAX_NON_LFS)
  277. goto out_putf;
  278. error = -EPERM;
  279. if (IS_APPEND(inode))
  280. goto out_putf;
  281. error = locks_verify_truncate(inode, file, length);
  282. if (!error)
  283. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
  284. out_putf:
  285. fput(file);
  286. out:
  287. return error;
  288. }
  289. asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
  290. {
  291. long ret = do_sys_ftruncate(fd, length, 1);
  292. /* avoid REGPARM breakage on x86: */
  293. asmlinkage_protect(2, ret, fd, length);
  294. return ret;
  295. }
  296. /* LFS versions of truncate are only needed on 32 bit machines */
  297. #if BITS_PER_LONG == 32
  298. asmlinkage long sys_truncate64(const char __user * path, loff_t length)
  299. {
  300. return do_sys_truncate(path, length);
  301. }
  302. asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
  303. {
  304. long ret = do_sys_ftruncate(fd, length, 0);
  305. /* avoid REGPARM breakage on x86: */
  306. asmlinkage_protect(2, ret, fd, length);
  307. return ret;
  308. }
  309. #endif
  310. asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len)
  311. {
  312. struct file *file;
  313. struct inode *inode;
  314. long ret = -EINVAL;
  315. if (offset < 0 || len <= 0)
  316. goto out;
  317. /* Return error if mode is not supported */
  318. ret = -EOPNOTSUPP;
  319. if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
  320. goto out;
  321. ret = -EBADF;
  322. file = fget(fd);
  323. if (!file)
  324. goto out;
  325. if (!(file->f_mode & FMODE_WRITE))
  326. goto out_fput;
  327. /*
  328. * Revalidate the write permissions, in case security policy has
  329. * changed since the files were opened.
  330. */
  331. ret = security_file_permission(file, MAY_WRITE);
  332. if (ret)
  333. goto out_fput;
  334. inode = file->f_path.dentry->d_inode;
  335. ret = -ESPIPE;
  336. if (S_ISFIFO(inode->i_mode))
  337. goto out_fput;
  338. ret = -ENODEV;
  339. /*
  340. * Let individual file system decide if it supports preallocation
  341. * for directories or not.
  342. */
  343. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  344. goto out_fput;
  345. ret = -EFBIG;
  346. /* Check for wrap through zero too */
  347. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  348. goto out_fput;
  349. if (inode->i_op && inode->i_op->fallocate)
  350. ret = inode->i_op->fallocate(inode, mode, offset, len);
  351. else
  352. ret = -EOPNOTSUPP;
  353. out_fput:
  354. fput(file);
  355. out:
  356. return ret;
  357. }
  358. /*
  359. * access() needs to use the real uid/gid, not the effective uid/gid.
  360. * We do this by temporarily clearing all FS-related capabilities and
  361. * switching the fsuid/fsgid around to the real ones.
  362. */
  363. asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
  364. {
  365. struct nameidata nd;
  366. int old_fsuid, old_fsgid;
  367. kernel_cap_t old_cap;
  368. int res;
  369. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  370. return -EINVAL;
  371. old_fsuid = current->fsuid;
  372. old_fsgid = current->fsgid;
  373. old_cap = current->cap_effective;
  374. current->fsuid = current->uid;
  375. current->fsgid = current->gid;
  376. /*
  377. * Clear the capabilities if we switch to a non-root user
  378. *
  379. * FIXME: There is a race here against sys_capset. The
  380. * capabilities can change yet we will restore the old
  381. * value below. We should hold task_capabilities_lock,
  382. * but we cannot because user_path_walk can sleep.
  383. */
  384. if (current->uid)
  385. cap_clear(current->cap_effective);
  386. else
  387. current->cap_effective = current->cap_permitted;
  388. res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
  389. if (res)
  390. goto out;
  391. res = vfs_permission(&nd, mode);
  392. /* SuS v2 requires we report a read only fs too */
  393. if(res || !(mode & S_IWOTH) ||
  394. special_file(nd.path.dentry->d_inode->i_mode))
  395. goto out_path_release;
  396. /*
  397. * This is a rare case where using __mnt_is_readonly()
  398. * is OK without a mnt_want/drop_write() pair. Since
  399. * no actual write to the fs is performed here, we do
  400. * not need to telegraph to that to anyone.
  401. *
  402. * By doing this, we accept that this access is
  403. * inherently racy and know that the fs may change
  404. * state before we even see this result.
  405. */
  406. if (__mnt_is_readonly(nd.path.mnt))
  407. res = -EROFS;
  408. out_path_release:
  409. path_put(&nd.path);
  410. out:
  411. current->fsuid = old_fsuid;
  412. current->fsgid = old_fsgid;
  413. current->cap_effective = old_cap;
  414. return res;
  415. }
  416. asmlinkage long sys_access(const char __user *filename, int mode)
  417. {
  418. return sys_faccessat(AT_FDCWD, filename, mode);
  419. }
  420. asmlinkage long sys_chdir(const char __user * filename)
  421. {
  422. struct nameidata nd;
  423. int error;
  424. error = __user_walk(filename,
  425. LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
  426. if (error)
  427. goto out;
  428. error = vfs_permission(&nd, MAY_EXEC);
  429. if (error)
  430. goto dput_and_out;
  431. set_fs_pwd(current->fs, &nd.path);
  432. dput_and_out:
  433. path_put(&nd.path);
  434. out:
  435. return error;
  436. }
  437. asmlinkage long sys_fchdir(unsigned int fd)
  438. {
  439. struct file *file;
  440. struct inode *inode;
  441. int error;
  442. error = -EBADF;
  443. file = fget(fd);
  444. if (!file)
  445. goto out;
  446. inode = file->f_path.dentry->d_inode;
  447. error = -ENOTDIR;
  448. if (!S_ISDIR(inode->i_mode))
  449. goto out_putf;
  450. error = file_permission(file, MAY_EXEC);
  451. if (!error)
  452. set_fs_pwd(current->fs, &file->f_path);
  453. out_putf:
  454. fput(file);
  455. out:
  456. return error;
  457. }
  458. asmlinkage long sys_chroot(const char __user * filename)
  459. {
  460. struct nameidata nd;
  461. int error;
  462. error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
  463. if (error)
  464. goto out;
  465. error = vfs_permission(&nd, MAY_EXEC);
  466. if (error)
  467. goto dput_and_out;
  468. error = -EPERM;
  469. if (!capable(CAP_SYS_CHROOT))
  470. goto dput_and_out;
  471. set_fs_root(current->fs, &nd.path);
  472. set_fs_altroot();
  473. error = 0;
  474. dput_and_out:
  475. path_put(&nd.path);
  476. out:
  477. return error;
  478. }
  479. asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
  480. {
  481. struct inode * inode;
  482. struct dentry * dentry;
  483. struct file * file;
  484. int err = -EBADF;
  485. struct iattr newattrs;
  486. file = fget(fd);
  487. if (!file)
  488. goto out;
  489. dentry = file->f_path.dentry;
  490. inode = dentry->d_inode;
  491. audit_inode(NULL, dentry);
  492. err = mnt_want_write(file->f_path.mnt);
  493. if (err)
  494. goto out_putf;
  495. err = -EPERM;
  496. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  497. goto out_drop_write;
  498. mutex_lock(&inode->i_mutex);
  499. if (mode == (mode_t) -1)
  500. mode = inode->i_mode;
  501. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  502. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  503. err = notify_change(dentry, &newattrs);
  504. mutex_unlock(&inode->i_mutex);
  505. out_drop_write:
  506. mnt_drop_write(file->f_path.mnt);
  507. out_putf:
  508. fput(file);
  509. out:
  510. return err;
  511. }
  512. asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
  513. mode_t mode)
  514. {
  515. struct nameidata nd;
  516. struct inode * inode;
  517. int error;
  518. struct iattr newattrs;
  519. error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
  520. if (error)
  521. goto out;
  522. inode = nd.path.dentry->d_inode;
  523. error = mnt_want_write(nd.path.mnt);
  524. if (error)
  525. goto dput_and_out;
  526. error = -EPERM;
  527. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  528. goto out_drop_write;
  529. mutex_lock(&inode->i_mutex);
  530. if (mode == (mode_t) -1)
  531. mode = inode->i_mode;
  532. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  533. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  534. error = notify_change(nd.path.dentry, &newattrs);
  535. mutex_unlock(&inode->i_mutex);
  536. out_drop_write:
  537. mnt_drop_write(nd.path.mnt);
  538. dput_and_out:
  539. path_put(&nd.path);
  540. out:
  541. return error;
  542. }
  543. asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
  544. {
  545. return sys_fchmodat(AT_FDCWD, filename, mode);
  546. }
  547. static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
  548. {
  549. struct inode * inode;
  550. int error;
  551. struct iattr newattrs;
  552. error = -ENOENT;
  553. if (!(inode = dentry->d_inode)) {
  554. printk(KERN_ERR "chown_common: NULL inode\n");
  555. goto out;
  556. }
  557. error = -EPERM;
  558. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  559. goto out;
  560. newattrs.ia_valid = ATTR_CTIME;
  561. if (user != (uid_t) -1) {
  562. newattrs.ia_valid |= ATTR_UID;
  563. newattrs.ia_uid = user;
  564. }
  565. if (group != (gid_t) -1) {
  566. newattrs.ia_valid |= ATTR_GID;
  567. newattrs.ia_gid = group;
  568. }
  569. if (!S_ISDIR(inode->i_mode))
  570. newattrs.ia_valid |=
  571. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  572. mutex_lock(&inode->i_mutex);
  573. error = notify_change(dentry, &newattrs);
  574. mutex_unlock(&inode->i_mutex);
  575. out:
  576. return error;
  577. }
  578. asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
  579. {
  580. struct nameidata nd;
  581. int error;
  582. error = user_path_walk(filename, &nd);
  583. if (error)
  584. goto out;
  585. error = mnt_want_write(nd.path.mnt);
  586. if (error)
  587. goto out_release;
  588. error = chown_common(nd.path.dentry, user, group);
  589. mnt_drop_write(nd.path.mnt);
  590. out_release:
  591. path_put(&nd.path);
  592. out:
  593. return error;
  594. }
  595. asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
  596. gid_t group, int flag)
  597. {
  598. struct nameidata nd;
  599. int error = -EINVAL;
  600. int follow;
  601. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  602. goto out;
  603. follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  604. error = __user_walk_fd(dfd, filename, follow, &nd);
  605. if (error)
  606. goto out;
  607. error = mnt_want_write(nd.path.mnt);
  608. if (error)
  609. goto out_release;
  610. error = chown_common(nd.path.dentry, user, group);
  611. mnt_drop_write(nd.path.mnt);
  612. out_release:
  613. path_put(&nd.path);
  614. out:
  615. return error;
  616. }
  617. asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
  618. {
  619. struct nameidata nd;
  620. int error;
  621. error = user_path_walk_link(filename, &nd);
  622. if (error)
  623. goto out;
  624. error = mnt_want_write(nd.path.mnt);
  625. if (error)
  626. goto out_release;
  627. error = chown_common(nd.path.dentry, user, group);
  628. mnt_drop_write(nd.path.mnt);
  629. out_release:
  630. path_put(&nd.path);
  631. out:
  632. return error;
  633. }
  634. asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
  635. {
  636. struct file * file;
  637. int error = -EBADF;
  638. struct dentry * dentry;
  639. file = fget(fd);
  640. if (!file)
  641. goto out;
  642. error = mnt_want_write(file->f_path.mnt);
  643. if (error)
  644. goto out_fput;
  645. dentry = file->f_path.dentry;
  646. audit_inode(NULL, dentry);
  647. error = chown_common(dentry, user, group);
  648. mnt_drop_write(file->f_path.mnt);
  649. out_fput:
  650. fput(file);
  651. out:
  652. return error;
  653. }
  654. /*
  655. * You have to be very careful that these write
  656. * counts get cleaned up in error cases and
  657. * upon __fput(). This should probably never
  658. * be called outside of __dentry_open().
  659. */
  660. static inline int __get_file_write_access(struct inode *inode,
  661. struct vfsmount *mnt)
  662. {
  663. int error;
  664. error = get_write_access(inode);
  665. if (error)
  666. return error;
  667. /*
  668. * Do not take mount writer counts on
  669. * special files since no writes to
  670. * the mount itself will occur.
  671. */
  672. if (!special_file(inode->i_mode)) {
  673. /*
  674. * Balanced in __fput()
  675. */
  676. error = mnt_want_write(mnt);
  677. if (error)
  678. put_write_access(inode);
  679. }
  680. return error;
  681. }
  682. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  683. int flags, struct file *f,
  684. int (*open)(struct inode *, struct file *))
  685. {
  686. struct inode *inode;
  687. int error;
  688. f->f_flags = flags;
  689. f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
  690. FMODE_PREAD | FMODE_PWRITE;
  691. inode = dentry->d_inode;
  692. if (f->f_mode & FMODE_WRITE) {
  693. error = __get_file_write_access(inode, mnt);
  694. if (error)
  695. goto cleanup_file;
  696. if (!special_file(inode->i_mode))
  697. file_take_write(f);
  698. }
  699. f->f_mapping = inode->i_mapping;
  700. f->f_path.dentry = dentry;
  701. f->f_path.mnt = mnt;
  702. f->f_pos = 0;
  703. f->f_op = fops_get(inode->i_fop);
  704. file_move(f, &inode->i_sb->s_files);
  705. error = security_dentry_open(f);
  706. if (error)
  707. goto cleanup_all;
  708. if (!open && f->f_op)
  709. open = f->f_op->open;
  710. if (open) {
  711. error = open(inode, f);
  712. if (error)
  713. goto cleanup_all;
  714. }
  715. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  716. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  717. /* NB: we're sure to have correct a_ops only after f_op->open */
  718. if (f->f_flags & O_DIRECT) {
  719. if (!f->f_mapping->a_ops ||
  720. ((!f->f_mapping->a_ops->direct_IO) &&
  721. (!f->f_mapping->a_ops->get_xip_mem))) {
  722. fput(f);
  723. f = ERR_PTR(-EINVAL);
  724. }
  725. }
  726. return f;
  727. cleanup_all:
  728. fops_put(f->f_op);
  729. if (f->f_mode & FMODE_WRITE) {
  730. put_write_access(inode);
  731. if (!special_file(inode->i_mode)) {
  732. /*
  733. * We don't consider this a real
  734. * mnt_want/drop_write() pair
  735. * because it all happenend right
  736. * here, so just reset the state.
  737. */
  738. file_reset_write(f);
  739. mnt_drop_write(mnt);
  740. }
  741. }
  742. file_kill(f);
  743. f->f_path.dentry = NULL;
  744. f->f_path.mnt = NULL;
  745. cleanup_file:
  746. put_filp(f);
  747. dput(dentry);
  748. mntput(mnt);
  749. return ERR_PTR(error);
  750. }
  751. /**
  752. * lookup_instantiate_filp - instantiates the open intent filp
  753. * @nd: pointer to nameidata
  754. * @dentry: pointer to dentry
  755. * @open: open callback
  756. *
  757. * Helper for filesystems that want to use lookup open intents and pass back
  758. * a fully instantiated struct file to the caller.
  759. * This function is meant to be called from within a filesystem's
  760. * lookup method.
  761. * Beware of calling it for non-regular files! Those ->open methods might block
  762. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  763. * leading to a deadlock, as nobody can open that fifo anymore, because
  764. * another process to open fifo will block on locked parent when doing lookup).
  765. * Note that in case of error, nd->intent.open.file is destroyed, but the
  766. * path information remains valid.
  767. * If the open callback is set to NULL, then the standard f_op->open()
  768. * filesystem callback is substituted.
  769. */
  770. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  771. int (*open)(struct inode *, struct file *))
  772. {
  773. if (IS_ERR(nd->intent.open.file))
  774. goto out;
  775. if (IS_ERR(dentry))
  776. goto out_err;
  777. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  778. nd->intent.open.flags - 1,
  779. nd->intent.open.file,
  780. open);
  781. out:
  782. return nd->intent.open.file;
  783. out_err:
  784. release_open_intent(nd);
  785. nd->intent.open.file = (struct file *)dentry;
  786. goto out;
  787. }
  788. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  789. /**
  790. * nameidata_to_filp - convert a nameidata to an open filp.
  791. * @nd: pointer to nameidata
  792. * @flags: open flags
  793. *
  794. * Note that this function destroys the original nameidata
  795. */
  796. struct file *nameidata_to_filp(struct nameidata *nd, int flags)
  797. {
  798. struct file *filp;
  799. /* Pick up the filp from the open intent */
  800. filp = nd->intent.open.file;
  801. /* Has the filesystem initialised the file for us? */
  802. if (filp->f_path.dentry == NULL)
  803. filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
  804. NULL);
  805. else
  806. path_put(&nd->path);
  807. return filp;
  808. }
  809. /*
  810. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  811. * error.
  812. */
  813. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
  814. {
  815. int error;
  816. struct file *f;
  817. /*
  818. * We must always pass in a valid mount pointer. Historically
  819. * callers got away with not passing it, but we must enforce this at
  820. * the earliest possible point now to avoid strange problems deep in the
  821. * filesystem stack.
  822. */
  823. if (!mnt) {
  824. printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
  825. dump_stack();
  826. return ERR_PTR(-EINVAL);
  827. }
  828. error = -ENFILE;
  829. f = get_empty_filp();
  830. if (f == NULL) {
  831. dput(dentry);
  832. mntput(mnt);
  833. return ERR_PTR(error);
  834. }
  835. return __dentry_open(dentry, mnt, flags, f, NULL);
  836. }
  837. EXPORT_SYMBOL(dentry_open);
  838. /*
  839. * Find an empty file descriptor entry, and mark it busy.
  840. */
  841. int get_unused_fd_flags(int flags)
  842. {
  843. struct files_struct * files = current->files;
  844. int fd, error;
  845. struct fdtable *fdt;
  846. error = -EMFILE;
  847. spin_lock(&files->file_lock);
  848. repeat:
  849. fdt = files_fdtable(files);
  850. fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
  851. files->next_fd);
  852. /*
  853. * N.B. For clone tasks sharing a files structure, this test
  854. * will limit the total number of files that can be opened.
  855. */
  856. if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  857. goto out;
  858. /* Do we need to expand the fd array or fd set? */
  859. error = expand_files(files, fd);
  860. if (error < 0)
  861. goto out;
  862. if (error) {
  863. /*
  864. * If we needed to expand the fs array we
  865. * might have blocked - try again.
  866. */
  867. error = -EMFILE;
  868. goto repeat;
  869. }
  870. FD_SET(fd, fdt->open_fds);
  871. if (flags & O_CLOEXEC)
  872. FD_SET(fd, fdt->close_on_exec);
  873. else
  874. FD_CLR(fd, fdt->close_on_exec);
  875. files->next_fd = fd + 1;
  876. #if 1
  877. /* Sanity check */
  878. if (fdt->fd[fd] != NULL) {
  879. printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
  880. fdt->fd[fd] = NULL;
  881. }
  882. #endif
  883. error = fd;
  884. out:
  885. spin_unlock(&files->file_lock);
  886. return error;
  887. }
  888. int get_unused_fd(void)
  889. {
  890. return get_unused_fd_flags(0);
  891. }
  892. EXPORT_SYMBOL(get_unused_fd);
  893. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  894. {
  895. struct fdtable *fdt = files_fdtable(files);
  896. __FD_CLR(fd, fdt->open_fds);
  897. if (fd < files->next_fd)
  898. files->next_fd = fd;
  899. }
  900. void put_unused_fd(unsigned int fd)
  901. {
  902. struct files_struct *files = current->files;
  903. spin_lock(&files->file_lock);
  904. __put_unused_fd(files, fd);
  905. spin_unlock(&files->file_lock);
  906. }
  907. EXPORT_SYMBOL(put_unused_fd);
  908. /*
  909. * Install a file pointer in the fd array.
  910. *
  911. * The VFS is full of places where we drop the files lock between
  912. * setting the open_fds bitmap and installing the file in the file
  913. * array. At any such point, we are vulnerable to a dup2() race
  914. * installing a file in the array before us. We need to detect this and
  915. * fput() the struct file we are about to overwrite in this case.
  916. *
  917. * It should never happen - if we allow dup2() do it, _really_ bad things
  918. * will follow.
  919. */
  920. void fd_install(unsigned int fd, struct file *file)
  921. {
  922. struct files_struct *files = current->files;
  923. struct fdtable *fdt;
  924. spin_lock(&files->file_lock);
  925. fdt = files_fdtable(files);
  926. BUG_ON(fdt->fd[fd] != NULL);
  927. rcu_assign_pointer(fdt->fd[fd], file);
  928. spin_unlock(&files->file_lock);
  929. }
  930. EXPORT_SYMBOL(fd_install);
  931. long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
  932. {
  933. char *tmp = getname(filename);
  934. int fd = PTR_ERR(tmp);
  935. if (!IS_ERR(tmp)) {
  936. fd = get_unused_fd_flags(flags);
  937. if (fd >= 0) {
  938. struct file *f = do_filp_open(dfd, tmp, flags, mode);
  939. if (IS_ERR(f)) {
  940. put_unused_fd(fd);
  941. fd = PTR_ERR(f);
  942. } else {
  943. fsnotify_open(f->f_path.dentry);
  944. fd_install(fd, f);
  945. }
  946. }
  947. putname(tmp);
  948. }
  949. return fd;
  950. }
  951. asmlinkage long sys_open(const char __user *filename, int flags, int mode)
  952. {
  953. long ret;
  954. if (force_o_largefile())
  955. flags |= O_LARGEFILE;
  956. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  957. /* avoid REGPARM breakage on x86: */
  958. asmlinkage_protect(3, ret, filename, flags, mode);
  959. return ret;
  960. }
  961. asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
  962. int mode)
  963. {
  964. long ret;
  965. if (force_o_largefile())
  966. flags |= O_LARGEFILE;
  967. ret = do_sys_open(dfd, filename, flags, mode);
  968. /* avoid REGPARM breakage on x86: */
  969. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  970. return ret;
  971. }
  972. #ifndef __alpha__
  973. /*
  974. * For backward compatibility? Maybe this should be moved
  975. * into arch/i386 instead?
  976. */
  977. asmlinkage long sys_creat(const char __user * pathname, int mode)
  978. {
  979. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  980. }
  981. #endif
  982. /*
  983. * "id" is the POSIX thread ID. We use the
  984. * files pointer for this..
  985. */
  986. int filp_close(struct file *filp, fl_owner_t id)
  987. {
  988. int retval = 0;
  989. if (!file_count(filp)) {
  990. printk(KERN_ERR "VFS: Close: file count is 0\n");
  991. return 0;
  992. }
  993. if (filp->f_op && filp->f_op->flush)
  994. retval = filp->f_op->flush(filp, id);
  995. dnotify_flush(filp, id);
  996. locks_remove_posix(filp, id);
  997. fput(filp);
  998. return retval;
  999. }
  1000. EXPORT_SYMBOL(filp_close);
  1001. /*
  1002. * Careful here! We test whether the file pointer is NULL before
  1003. * releasing the fd. This ensures that one clone task can't release
  1004. * an fd while another clone is opening it.
  1005. */
  1006. asmlinkage long sys_close(unsigned int fd)
  1007. {
  1008. struct file * filp;
  1009. struct files_struct *files = current->files;
  1010. struct fdtable *fdt;
  1011. int retval;
  1012. spin_lock(&files->file_lock);
  1013. fdt = files_fdtable(files);
  1014. if (fd >= fdt->max_fds)
  1015. goto out_unlock;
  1016. filp = fdt->fd[fd];
  1017. if (!filp)
  1018. goto out_unlock;
  1019. rcu_assign_pointer(fdt->fd[fd], NULL);
  1020. FD_CLR(fd, fdt->close_on_exec);
  1021. __put_unused_fd(files, fd);
  1022. spin_unlock(&files->file_lock);
  1023. retval = filp_close(filp, files);
  1024. /* can't restart close syscall because file table entry was cleared */
  1025. if (unlikely(retval == -ERESTARTSYS ||
  1026. retval == -ERESTARTNOINTR ||
  1027. retval == -ERESTARTNOHAND ||
  1028. retval == -ERESTART_RESTARTBLOCK))
  1029. retval = -EINTR;
  1030. return retval;
  1031. out_unlock:
  1032. spin_unlock(&files->file_lock);
  1033. return -EBADF;
  1034. }
  1035. EXPORT_SYMBOL(sys_close);
  1036. /*
  1037. * This routine simulates a hangup on the tty, to arrange that users
  1038. * are given clean terminals at login time.
  1039. */
  1040. asmlinkage long sys_vhangup(void)
  1041. {
  1042. if (capable(CAP_SYS_TTY_CONFIG)) {
  1043. /* XXX: this needs locking */
  1044. tty_vhangup(current->signal->tty);
  1045. return 0;
  1046. }
  1047. return -EPERM;
  1048. }
  1049. /*
  1050. * Called when an inode is about to be open.
  1051. * We use this to disallow opening large files on 32bit systems if
  1052. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  1053. * on this flag in sys_open.
  1054. */
  1055. int generic_file_open(struct inode * inode, struct file * filp)
  1056. {
  1057. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  1058. return -EOVERFLOW;
  1059. return 0;
  1060. }
  1061. EXPORT_SYMBOL(generic_file_open);
  1062. /*
  1063. * This is used by subsystems that don't want seekable
  1064. * file descriptors
  1065. */
  1066. int nonseekable_open(struct inode *inode, struct file *filp)
  1067. {
  1068. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1069. return 0;
  1070. }
  1071. EXPORT_SYMBOL(nonseekable_open);