open.c 27 KB

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