open.c 27 KB

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