open.c 28 KB

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