open.c 27 KB

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