open.c 28 KB

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