open.c 27 KB

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