open.c 25 KB

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