open.c 25 KB

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