open.c 25 KB

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