PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/sys/ufs/ext2fs/ext2fs_vnops.c

https://bitbucket.org/gthummalapalle/minix
C | 1664 lines | 1180 code | 93 blank | 391 comment | 279 complexity | 0971aa255f7c3e5ad9b47cafa714848c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, WTFPL, AGPL-1.0
  1. /* $NetBSD: ext2fs_vnops.c,v 1.101 2011/11/18 21:18:51 christos Exp $ */
  2. /*
  3. * Copyright (c) 1982, 1986, 1989, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. * (c) UNIX System Laboratories, Inc.
  6. * All or some portions of this file are derived from material licensed
  7. * to the University of California by American Telephone and Telegraph
  8. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  9. * the permission of UNIX System Laboratories, Inc.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94
  36. * Modified for ext2fs by Manuel Bouyer.
  37. */
  38. /*
  39. * Copyright (c) 1997 Manuel Bouyer.
  40. *
  41. * Redistribution and use in source and binary forms, with or without
  42. * modification, are permitted provided that the following conditions
  43. * are met:
  44. * 1. Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * 2. Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in the
  48. * documentation and/or other materials provided with the distribution.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  51. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  52. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  53. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  54. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  55. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  56. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  57. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  59. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. *
  61. * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94
  62. * Modified for ext2fs by Manuel Bouyer.
  63. */
  64. #include <sys/cdefs.h>
  65. __KERNEL_RCSID(0, "$NetBSD: ext2fs_vnops.c,v 1.101 2011/11/18 21:18:51 christos Exp $");
  66. #include <sys/param.h>
  67. #include <sys/systm.h>
  68. #include <sys/resourcevar.h>
  69. #include <sys/kernel.h>
  70. #include <sys/file.h>
  71. #include <sys/stat.h>
  72. #include <sys/buf.h>
  73. #include <sys/proc.h>
  74. #include <sys/mount.h>
  75. #include <sys/namei.h>
  76. #include <sys/vnode.h>
  77. #include <sys/lockf.h>
  78. #include <sys/malloc.h>
  79. #include <sys/pool.h>
  80. #include <sys/signalvar.h>
  81. #include <sys/kauth.h>
  82. #include <miscfs/fifofs/fifo.h>
  83. #include <miscfs/genfs/genfs.h>
  84. #include <miscfs/specfs/specdev.h>
  85. #include <ufs/ufs/inode.h>
  86. #include <ufs/ufs/ufs_extern.h>
  87. #include <ufs/ufs/ufsmount.h>
  88. #include <ufs/ext2fs/ext2fs.h>
  89. #include <ufs/ext2fs/ext2fs_extern.h>
  90. #include <ufs/ext2fs/ext2fs_dir.h>
  91. extern int prtactive;
  92. static int ext2fs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *);
  93. static int ext2fs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
  94. struct lwp *);
  95. union _qcvt {
  96. int64_t qcvt;
  97. int32_t val[2];
  98. };
  99. #define SETHIGH(q, h) { \
  100. union _qcvt tmp; \
  101. tmp.qcvt = (q); \
  102. tmp.val[_QUAD_HIGHWORD] = (h); \
  103. (q) = tmp.qcvt; \
  104. }
  105. #define SETLOW(q, l) { \
  106. union _qcvt tmp; \
  107. tmp.qcvt = (q); \
  108. tmp.val[_QUAD_LOWWORD] = (l); \
  109. (q) = tmp.qcvt; \
  110. }
  111. /*
  112. * Create a regular file
  113. */
  114. int
  115. ext2fs_create(void *v)
  116. {
  117. struct vop_create_args /* {
  118. struct vnode *a_dvp;
  119. struct vnode **a_vpp;
  120. struct componentname *a_cnp;
  121. struct vattr *a_vap;
  122. } */ *ap = v;
  123. int error;
  124. error =
  125. ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
  126. ap->a_dvp, ap->a_vpp, ap->a_cnp);
  127. if (error)
  128. return (error);
  129. VN_KNOTE(ap->a_dvp, NOTE_WRITE);
  130. return (0);
  131. }
  132. /*
  133. * Mknod vnode call
  134. */
  135. /* ARGSUSED */
  136. int
  137. ext2fs_mknod(void *v)
  138. {
  139. struct vop_mknod_args /* {
  140. struct vnode *a_dvp;
  141. struct vnode **a_vpp;
  142. struct componentname *a_cnp;
  143. struct vattr *a_vap;
  144. } */ *ap = v;
  145. struct vattr *vap = ap->a_vap;
  146. struct vnode **vpp = ap->a_vpp;
  147. struct inode *ip;
  148. int error;
  149. struct mount *mp;
  150. ino_t ino;
  151. if ((error = ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
  152. ap->a_dvp, vpp, ap->a_cnp)) != 0)
  153. return (error);
  154. VN_KNOTE(ap->a_dvp, NOTE_WRITE);
  155. ip = VTOI(*vpp);
  156. mp = (*vpp)->v_mount;
  157. ino = ip->i_number;
  158. ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
  159. if (vap->va_rdev != VNOVAL) {
  160. /*
  161. * Want to be able to use this to make badblock
  162. * inodes, so don't truncate the dev number.
  163. */
  164. ip->i_din.e2fs_din->e2di_rdev = h2fs32(vap->va_rdev);
  165. }
  166. /*
  167. * Remove inode so that it will be reloaded by VFS_VGET and
  168. * checked to see if it is an alias of an existing entry in
  169. * the inode cache.
  170. */
  171. VOP_UNLOCK(*vpp);
  172. (*vpp)->v_type = VNON;
  173. vgone(*vpp);
  174. error = VFS_VGET(mp, ino, vpp);
  175. if (error != 0) {
  176. *vpp = NULL;
  177. return (error);
  178. }
  179. return (0);
  180. }
  181. /*
  182. * Open called.
  183. *
  184. * Just check the APPEND flag.
  185. */
  186. /* ARGSUSED */
  187. int
  188. ext2fs_open(void *v)
  189. {
  190. struct vop_open_args /* {
  191. struct vnode *a_vp;
  192. int a_mode;
  193. kauth_cred_t a_cred;
  194. } */ *ap = v;
  195. /*
  196. * Files marked append-only must be opened for appending.
  197. */
  198. if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) &&
  199. (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
  200. return (EPERM);
  201. return (0);
  202. }
  203. static int
  204. ext2fs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode)
  205. {
  206. /*
  207. * Disallow write attempts on read-only file systems;
  208. * unless the file is a socket, fifo, or a block or
  209. * character device resident on the file system.
  210. */
  211. if (mode & VWRITE) {
  212. switch (vp->v_type) {
  213. case VDIR:
  214. case VLNK:
  215. case VREG:
  216. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  217. return (EROFS);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. /* If immutable bit set, nobody gets to write it. */
  224. if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE))
  225. return (EPERM);
  226. return 0;
  227. }
  228. static int
  229. ext2fs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode,
  230. kauth_cred_t cred)
  231. {
  232. return genfs_can_access(vp->v_type, ip->i_e2fs_mode & ALLPERMS,
  233. ip->i_uid, ip->i_gid, mode, cred);
  234. }
  235. int
  236. ext2fs_access(void *v)
  237. {
  238. struct vop_access_args /* {
  239. struct vnode *a_vp;
  240. int a_mode;
  241. kauth_cred_t a_cred;
  242. } */ *ap = v;
  243. struct vnode *vp = ap->a_vp;
  244. struct inode *ip = VTOI(vp);
  245. mode_t mode = ap->a_mode;
  246. int error;
  247. error = ext2fs_check_possible(vp, ip, mode);
  248. if (error)
  249. return error;
  250. error = ext2fs_check_permitted(vp, ip, mode, ap->a_cred);
  251. return error;
  252. }
  253. /* ARGSUSED */
  254. int
  255. ext2fs_getattr(void *v)
  256. {
  257. struct vop_getattr_args /* {
  258. struct vnode *a_vp;
  259. struct vattr *a_vap;
  260. kauth_cred_t a_cred;
  261. } */ *ap = v;
  262. struct vnode *vp = ap->a_vp;
  263. struct inode *ip = VTOI(vp);
  264. struct vattr *vap = ap->a_vap;
  265. EXT2FS_ITIMES(ip, NULL, NULL, NULL);
  266. /*
  267. * Copy from inode table
  268. */
  269. vap->va_fsid = ip->i_dev;
  270. vap->va_fileid = ip->i_number;
  271. vap->va_mode = ip->i_e2fs_mode & ALLPERMS;
  272. vap->va_nlink = ip->i_e2fs_nlink;
  273. vap->va_uid = ip->i_uid;
  274. vap->va_gid = ip->i_gid;
  275. vap->va_rdev = (dev_t)fs2h32(ip->i_din.e2fs_din->e2di_rdev);
  276. vap->va_size = vp->v_size;
  277. vap->va_atime.tv_sec = ip->i_e2fs_atime;
  278. vap->va_atime.tv_nsec = 0;
  279. vap->va_mtime.tv_sec = ip->i_e2fs_mtime;
  280. vap->va_mtime.tv_nsec = 0;
  281. vap->va_ctime.tv_sec = ip->i_e2fs_ctime;
  282. vap->va_ctime.tv_nsec = 0;
  283. #ifdef EXT2FS_SYSTEM_FLAGS
  284. vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0;
  285. vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
  286. #else
  287. vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0;
  288. vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0;
  289. #endif
  290. vap->va_gen = ip->i_e2fs_gen;
  291. /* this doesn't belong here */
  292. if (vp->v_type == VBLK)
  293. vap->va_blocksize = BLKDEV_IOSIZE;
  294. else if (vp->v_type == VCHR)
  295. vap->va_blocksize = MAXBSIZE;
  296. else
  297. vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
  298. vap->va_bytes = dbtob((u_quad_t)ip->i_e2fs_nblock);
  299. vap->va_type = vp->v_type;
  300. vap->va_filerev = ip->i_modrev;
  301. return (0);
  302. }
  303. /*
  304. * Set attribute vnode op. called from several syscalls
  305. */
  306. int
  307. ext2fs_setattr(void *v)
  308. {
  309. struct vop_setattr_args /* {
  310. struct vnode *a_vp;
  311. struct vattr *a_vap;
  312. kauth_cred_t a_cred;
  313. } */ *ap = v;
  314. struct vattr *vap = ap->a_vap;
  315. struct vnode *vp = ap->a_vp;
  316. struct inode *ip = VTOI(vp);
  317. kauth_cred_t cred = ap->a_cred;
  318. struct lwp *l = curlwp;
  319. int error;
  320. /*
  321. * Check for unsettable attributes.
  322. */
  323. if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
  324. (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
  325. (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
  326. ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
  327. return (EINVAL);
  328. }
  329. if (vap->va_flags != VNOVAL) {
  330. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  331. return (EROFS);
  332. if (kauth_cred_geteuid(cred) != ip->i_uid &&
  333. (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
  334. NULL)))
  335. return (error);
  336. #ifdef EXT2FS_SYSTEM_FLAGS
  337. if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
  338. NULL) == 0) {
  339. if ((ip->i_e2fs_flags &
  340. (EXT2_APPEND | EXT2_IMMUTABLE)) &&
  341. kauth_authorize_system(l->l_cred,
  342. KAUTH_SYSTEM_CHSYSFLAGS, 0, NULL, NULL, NULL))
  343. return (EPERM);
  344. ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
  345. ip->i_e2fs_flags |=
  346. (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 |
  347. (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
  348. } else
  349. return (EPERM);
  350. #else
  351. ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
  352. ip->i_e2fs_flags |=
  353. (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
  354. (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
  355. #endif
  356. ip->i_flag |= IN_CHANGE;
  357. if (vap->va_flags & (IMMUTABLE | APPEND))
  358. return (0);
  359. }
  360. if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
  361. return (EPERM);
  362. /*
  363. * Go through the fields and update iff not VNOVAL.
  364. */
  365. if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
  366. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  367. return (EROFS);
  368. error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
  369. if (error)
  370. return (error);
  371. }
  372. if (vap->va_size != VNOVAL) {
  373. /*
  374. * Disallow write attempts on read-only file systems;
  375. * unless the file is a socket, fifo, or a block or
  376. * character device resident on the file system.
  377. */
  378. switch (vp->v_type) {
  379. case VDIR:
  380. return (EISDIR);
  381. case VLNK:
  382. case VREG:
  383. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  384. return (EROFS);
  385. default:
  386. break;
  387. }
  388. error = ext2fs_truncate(vp, vap->va_size, 0, cred);
  389. if (error)
  390. return (error);
  391. }
  392. ip = VTOI(vp);
  393. if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
  394. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  395. return (EROFS);
  396. error = genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred);
  397. if (error)
  398. return (error);
  399. if (vap->va_atime.tv_sec != VNOVAL)
  400. if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
  401. ip->i_flag |= IN_ACCESS;
  402. if (vap->va_mtime.tv_sec != VNOVAL) {
  403. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  404. if (vp->v_mount->mnt_flag & MNT_RELATIME)
  405. ip->i_flag |= IN_ACCESS;
  406. }
  407. error = ext2fs_update(vp, &vap->va_atime, &vap->va_mtime,
  408. UPDATE_WAIT);
  409. if (error)
  410. return (error);
  411. }
  412. error = 0;
  413. if (vap->va_mode != (mode_t)VNOVAL) {
  414. if (vp->v_mount->mnt_flag & MNT_RDONLY)
  415. return (EROFS);
  416. error = ext2fs_chmod(vp, (int)vap->va_mode, cred, l);
  417. }
  418. VN_KNOTE(vp, NOTE_ATTRIB);
  419. return (error);
  420. }
  421. /*
  422. * Change the mode on a file.
  423. * Inode must be locked before calling.
  424. */
  425. static int
  426. ext2fs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
  427. {
  428. struct inode *ip = VTOI(vp);
  429. int error;
  430. error = genfs_can_chmod(vp, cred, ip->i_uid, ip->i_gid, mode);
  431. if (error)
  432. return (error);
  433. ip->i_e2fs_mode &= ~ALLPERMS;
  434. ip->i_e2fs_mode |= (mode & ALLPERMS);
  435. ip->i_flag |= IN_CHANGE;
  436. return (0);
  437. }
  438. /*
  439. * Perform chown operation on inode ip;
  440. * inode must be locked prior to call.
  441. */
  442. static int
  443. ext2fs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
  444. struct lwp *l)
  445. {
  446. struct inode *ip = VTOI(vp);
  447. uid_t ouid;
  448. gid_t ogid;
  449. int error;
  450. if (uid == (uid_t)VNOVAL)
  451. uid = ip->i_uid;
  452. if (gid == (gid_t)VNOVAL)
  453. gid = ip->i_gid;
  454. error = genfs_can_chown(vp, cred, ip->i_uid, ip->i_gid, uid, gid);
  455. if (error)
  456. return (error);
  457. ogid = ip->i_gid;
  458. ouid = ip->i_uid;
  459. ip->i_e2fs_gid = gid & 0xffff;
  460. ip->i_e2fs_uid = uid & 0xffff;
  461. if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
  462. ip->i_e2fs_gid_high = (gid >> 16) & 0xffff;
  463. ip->i_e2fs_uid_high = (uid >> 16) & 0xffff;
  464. } else {
  465. ip->i_e2fs_gid_high = 0;
  466. ip->i_e2fs_uid_high = 0;
  467. }
  468. if (ouid != uid || ogid != gid) {
  469. ext2fs_set_inode_guid(ip);
  470. ip->i_flag |= IN_CHANGE;
  471. }
  472. if (ouid != uid && kauth_authorize_generic(cred,
  473. KAUTH_GENERIC_ISSUSER, NULL) != 0)
  474. ip->i_e2fs_mode &= ~ISUID;
  475. if (ogid != gid && kauth_authorize_generic(cred,
  476. KAUTH_GENERIC_ISSUSER, NULL) != 0)
  477. ip->i_e2fs_mode &= ~ISGID;
  478. return (0);
  479. }
  480. int
  481. ext2fs_remove(void *v)
  482. {
  483. struct vop_remove_args /* {
  484. struct vnode *a_dvp;
  485. struct vnode *a_vp;
  486. struct componentname *a_cnp;
  487. } */ *ap = v;
  488. struct inode *ip;
  489. struct vnode *vp = ap->a_vp;
  490. struct vnode *dvp = ap->a_dvp;
  491. struct ufs_lookup_results *ulr;
  492. int error;
  493. /* XXX should handle this material another way */
  494. ulr = &VTOI(dvp)->i_crap;
  495. UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
  496. ip = VTOI(vp);
  497. if (vp->v_type == VDIR ||
  498. (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
  499. (VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
  500. error = EPERM;
  501. } else {
  502. error = ext2fs_dirremove(dvp, ulr, ap->a_cnp);
  503. if (error == 0) {
  504. ip->i_e2fs_nlink--;
  505. ip->i_flag |= IN_CHANGE;
  506. }
  507. }
  508. VN_KNOTE(vp, NOTE_DELETE);
  509. VN_KNOTE(dvp, NOTE_WRITE);
  510. if (dvp == vp)
  511. vrele(vp);
  512. else
  513. vput(vp);
  514. vput(dvp);
  515. return (error);
  516. }
  517. /*
  518. * ext2fs_link: create hard link.
  519. */
  520. int
  521. ext2fs_link(void *v)
  522. {
  523. struct vop_link_args /* {
  524. struct vnode *a_dvp;
  525. struct vnode *a_vp;
  526. struct componentname *a_cnp;
  527. } */ *ap = v;
  528. struct vnode *dvp = ap->a_dvp;
  529. struct vnode *vp = ap->a_vp;
  530. struct componentname *cnp = ap->a_cnp;
  531. struct inode *ip;
  532. int error;
  533. struct ufs_lookup_results *ulr;
  534. KASSERT(dvp != vp);
  535. KASSERT(vp->v_type != VDIR);
  536. KASSERT(dvp->v_mount == vp->v_mount);
  537. /* XXX should handle this material another way */
  538. ulr = &VTOI(dvp)->i_crap;
  539. UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
  540. error = vn_lock(vp, LK_EXCLUSIVE);
  541. if (error) {
  542. VOP_ABORTOP(dvp, cnp);
  543. goto out2;
  544. }
  545. ip = VTOI(vp);
  546. if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
  547. VOP_ABORTOP(dvp, cnp);
  548. error = EMLINK;
  549. goto out1;
  550. }
  551. if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
  552. VOP_ABORTOP(dvp, cnp);
  553. error = EPERM;
  554. goto out1;
  555. }
  556. ip->i_e2fs_nlink++;
  557. ip->i_flag |= IN_CHANGE;
  558. error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
  559. if (!error)
  560. error = ext2fs_direnter(ip, dvp, ulr, cnp);
  561. if (error) {
  562. ip->i_e2fs_nlink--;
  563. ip->i_flag |= IN_CHANGE;
  564. }
  565. out1:
  566. VOP_UNLOCK(vp);
  567. out2:
  568. VN_KNOTE(vp, NOTE_LINK);
  569. VN_KNOTE(dvp, NOTE_WRITE);
  570. vput(dvp);
  571. return (error);
  572. }
  573. /*
  574. * Rename system call.
  575. * rename("foo", "bar");
  576. * is essentially
  577. * unlink("bar");
  578. * link("foo", "bar");
  579. * unlink("foo");
  580. * but ``atomically''. Can't do full commit without saving state in the
  581. * inode on disk which isn't feasible at this time. Best we can do is
  582. * always guarantee the target exists.
  583. *
  584. * Basic algorithm is:
  585. *
  586. * 1) Bump link count on source while we're linking it to the
  587. * target. This also ensure the inode won't be deleted out
  588. * from underneath us while we work (it may be truncated by
  589. * a concurrent `trunc' or `open' for creation).
  590. * 2) Link source to destination. If destination already exists,
  591. * delete it first.
  592. * 3) Unlink source reference to inode if still around. If a
  593. * directory was moved and the parent of the destination
  594. * is different from the source, patch the ".." entry in the
  595. * directory.
  596. */
  597. int
  598. ext2fs_rename(void *v)
  599. {
  600. struct vop_rename_args /* {
  601. struct vnode *a_fdvp;
  602. struct vnode *a_fvp;
  603. struct componentname *a_fcnp;
  604. struct vnode *a_tdvp;
  605. struct vnode *a_tvp;
  606. struct componentname *a_tcnp;
  607. } */ *ap = v;
  608. struct vnode *tvp = ap->a_tvp;
  609. struct vnode *tdvp = ap->a_tdvp;
  610. struct vnode *fvp = ap->a_fvp;
  611. struct vnode *fdvp = ap->a_fdvp;
  612. struct componentname *tcnp = ap->a_tcnp;
  613. struct componentname *fcnp = ap->a_fcnp;
  614. struct inode *ip, *xp, *dp;
  615. struct ext2fs_dirtemplate dirbuf;
  616. int doingdirectory = 0, oldparent = 0, newparent = 0;
  617. int error = 0;
  618. u_char namlen;
  619. /*
  620. * Check for cross-device rename.
  621. */
  622. if ((fvp->v_mount != tdvp->v_mount) ||
  623. (tvp && (fvp->v_mount != tvp->v_mount))) {
  624. error = EXDEV;
  625. abortit:
  626. VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
  627. if (tdvp == tvp)
  628. vrele(tdvp);
  629. else
  630. vput(tdvp);
  631. if (tvp)
  632. vput(tvp);
  633. VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
  634. vrele(fdvp);
  635. vrele(fvp);
  636. return (error);
  637. }
  638. /*
  639. * Check if just deleting a link name.
  640. */
  641. if (tvp && ((VTOI(tvp)->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
  642. (VTOI(tdvp)->i_e2fs_flags & EXT2_APPEND))) {
  643. error = EPERM;
  644. goto abortit;
  645. }
  646. if (fvp == tvp) {
  647. if (fvp->v_type == VDIR) {
  648. error = EINVAL;
  649. goto abortit;
  650. }
  651. /* Release destination completely. */
  652. VOP_ABORTOP(tdvp, tcnp);
  653. vput(tdvp);
  654. vput(tvp);
  655. /* Delete source. */
  656. vrele(fvp);
  657. fcnp->cn_flags &= ~(MODMASK);
  658. fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
  659. fcnp->cn_nameiop = DELETE;
  660. vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
  661. if ((error = relookup(fdvp, &fvp, fcnp, 0))) {
  662. vput(fdvp);
  663. return (error);
  664. }
  665. return (VOP_REMOVE(fdvp, fvp, fcnp));
  666. }
  667. if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
  668. goto abortit;
  669. dp = VTOI(fdvp);
  670. ip = VTOI(fvp);
  671. if ((nlink_t) ip->i_e2fs_nlink >= LINK_MAX) {
  672. VOP_UNLOCK(fvp);
  673. error = EMLINK;
  674. goto abortit;
  675. }
  676. if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
  677. (dp->i_e2fs_flags & EXT2_APPEND)) {
  678. VOP_UNLOCK(fvp);
  679. error = EPERM;
  680. goto abortit;
  681. }
  682. if ((ip->i_e2fs_mode & IFMT) == IFDIR) {
  683. error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
  684. if (!error && tvp)
  685. error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred);
  686. if (error) {
  687. VOP_UNLOCK(fvp);
  688. error = EACCES;
  689. goto abortit;
  690. }
  691. /*
  692. * Avoid ".", "..", and aliases of "." for obvious reasons.
  693. */
  694. if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
  695. dp == ip ||
  696. (fcnp->cn_flags & ISDOTDOT) ||
  697. (tcnp->cn_flags & ISDOTDOT) ||
  698. (ip->i_flag & IN_RENAME)) {
  699. VOP_UNLOCK(fvp);
  700. error = EINVAL;
  701. goto abortit;
  702. }
  703. ip->i_flag |= IN_RENAME;
  704. oldparent = dp->i_number;
  705. doingdirectory = 1;
  706. }
  707. VN_KNOTE(fdvp, NOTE_WRITE); /* XXXLUKEM/XXX: right place? */
  708. /*
  709. * When the target exists, both the directory
  710. * and target vnodes are returned locked.
  711. */
  712. dp = VTOI(tdvp);
  713. xp = NULL;
  714. if (tvp)
  715. xp = VTOI(tvp);
  716. /*
  717. * 1) Bump link count while we're moving stuff
  718. * around. If we crash somewhere before
  719. * completing our work, the link count
  720. * may be wrong, but correctable.
  721. */
  722. ip->i_e2fs_nlink++;
  723. ip->i_flag |= IN_CHANGE;
  724. if ((error = ext2fs_update(fvp, NULL, NULL, UPDATE_WAIT)) != 0) {
  725. VOP_UNLOCK(fvp);
  726. goto bad;
  727. }
  728. /*
  729. * If ".." must be changed (ie the directory gets a new
  730. * parent) then the source directory must not be in the
  731. * directory hierarchy above the target, as this would
  732. * orphan everything below the source directory. Also
  733. * the user must have write permission in the source so
  734. * as to be able to change "..". We must repeat the call
  735. * to namei, as the parent directory is unlocked by the
  736. * call to checkpath().
  737. */
  738. error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
  739. VOP_UNLOCK(fvp);
  740. if (oldparent != dp->i_number)
  741. newparent = dp->i_number;
  742. if (doingdirectory && newparent) {
  743. if (error) /* write access check above */
  744. goto bad;
  745. if (xp != NULL)
  746. vput(tvp);
  747. vref(tdvp); /* compensate for the ref checkpath loses */
  748. error = ext2fs_checkpath(ip, dp, tcnp->cn_cred);
  749. if (error != 0) {
  750. vrele(tdvp);
  751. goto out;
  752. }
  753. vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
  754. if ((error = relookup(tdvp, &tvp, tcnp, 0)) != 0) {
  755. vput(tdvp);
  756. goto out;
  757. }
  758. dp = VTOI(tdvp);
  759. xp = NULL;
  760. if (tvp)
  761. xp = VTOI(tvp);
  762. }
  763. /*
  764. * 2) If target doesn't exist, link the target
  765. * to the source and unlink the source.
  766. * Otherwise, rewrite the target directory
  767. * entry to reference the source inode and
  768. * expunge the original entry's existence.
  769. */
  770. if (xp == NULL) {
  771. if (dp->i_dev != ip->i_dev)
  772. panic("rename: EXDEV");
  773. /*
  774. * Account for ".." in new directory.
  775. * When source and destination have the same
  776. * parent we don't fool with the link count.
  777. */
  778. if (doingdirectory && newparent) {
  779. if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
  780. error = EMLINK;
  781. goto bad;
  782. }
  783. dp->i_e2fs_nlink++;
  784. dp->i_flag |= IN_CHANGE;
  785. if ((error = ext2fs_update(tdvp, NULL, NULL,
  786. UPDATE_WAIT)) != 0)
  787. goto bad;
  788. }
  789. error = ext2fs_direnter(ip, tdvp, &VTOI(tdvp)->i_crap, tcnp);
  790. if (error != 0) {
  791. if (doingdirectory && newparent) {
  792. dp->i_e2fs_nlink--;
  793. dp->i_flag |= IN_CHANGE;
  794. (void)ext2fs_update(tdvp, NULL, NULL,
  795. UPDATE_WAIT);
  796. }
  797. goto bad;
  798. }
  799. VN_KNOTE(tdvp, NOTE_WRITE);
  800. vput(tdvp);
  801. } else {
  802. if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
  803. panic("rename: EXDEV");
  804. /*
  805. * Short circuit rename(foo, foo).
  806. */
  807. if (xp->i_number == ip->i_number)
  808. panic("rename: same file");
  809. /*
  810. * If the parent directory is "sticky", then the user must
  811. * own the parent directory, or the destination of the rename,
  812. * otherwise the destination may not be changed (except by
  813. * root). This implements append-only directories.
  814. */
  815. if ((dp->i_e2fs_mode & S_ISTXT) &&
  816. kauth_authorize_generic(tcnp->cn_cred,
  817. KAUTH_GENERIC_ISSUSER, NULL) != 0 &&
  818. kauth_cred_geteuid(tcnp->cn_cred) != dp->i_uid &&
  819. xp->i_uid != kauth_cred_geteuid(tcnp->cn_cred)) {
  820. error = EPERM;
  821. goto bad;
  822. }
  823. /*
  824. * Target must be empty if a directory and have no links
  825. * to it. Also, ensure source and target are compatible
  826. * (both directories, or both not directories).
  827. */
  828. if ((xp->i_e2fs_mode & IFMT) == IFDIR) {
  829. if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
  830. xp->i_e2fs_nlink > 2) {
  831. error = ENOTEMPTY;
  832. goto bad;
  833. }
  834. if (!doingdirectory) {
  835. error = ENOTDIR;
  836. goto bad;
  837. }
  838. cache_purge(tdvp);
  839. } else if (doingdirectory) {
  840. error = EISDIR;
  841. goto bad;
  842. }
  843. error = ext2fs_dirrewrite(dp, &dp->i_crap, ip, tcnp);
  844. if (error != 0)
  845. goto bad;
  846. /*
  847. * If the target directory is in the same
  848. * directory as the source directory,
  849. * decrement the link count on the parent
  850. * of the target directory.
  851. */
  852. if (doingdirectory && !newparent) {
  853. dp->i_e2fs_nlink--;
  854. dp->i_flag |= IN_CHANGE;
  855. }
  856. /*
  857. * Adjust the link count of the target to
  858. * reflect the dirrewrite above. If this is
  859. * a directory it is empty and there are
  860. * no links to it, so we can squash the inode and
  861. * any space associated with it. We disallowed
  862. * renaming over top of a directory with links to
  863. * it above, as the remaining link would point to
  864. * a directory without "." or ".." entries.
  865. */
  866. xp->i_e2fs_nlink--;
  867. if (doingdirectory) {
  868. if (--xp->i_e2fs_nlink != 0)
  869. panic("rename: linked directory");
  870. error = ext2fs_truncate(tvp, (off_t)0, IO_SYNC,
  871. tcnp->cn_cred);
  872. }
  873. xp->i_flag |= IN_CHANGE;
  874. VN_KNOTE(tdvp, NOTE_WRITE);
  875. vput(tdvp);
  876. VN_KNOTE(tvp, NOTE_DELETE);
  877. vput(tvp);
  878. xp = NULL;
  879. }
  880. /*
  881. * 3) Unlink the source.
  882. */
  883. fcnp->cn_flags &= ~(MODMASK);
  884. fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
  885. vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
  886. if ((error = relookup(fdvp, &fvp, fcnp, 0))) {
  887. vput(fdvp);
  888. vrele(ap->a_fvp);
  889. return (error);
  890. }
  891. if (fvp != NULL) {
  892. xp = VTOI(fvp);
  893. dp = VTOI(fdvp);
  894. } else {
  895. /*
  896. * From name has disappeared.
  897. */
  898. if (doingdirectory)
  899. panic("ext2fs_rename: lost dir entry");
  900. vrele(ap->a_fvp);
  901. return (0);
  902. }
  903. /*
  904. * Ensure that the directory entry still exists and has not
  905. * changed while the new name has been entered. If the source is
  906. * a file then the entry may have been unlinked or renamed. In
  907. * either case there is no further work to be done. If the source
  908. * is a directory then it cannot have been rmdir'ed; its link
  909. * count of three would cause a rmdir to fail with ENOTEMPTY.
  910. * The IRENAME flag ensures that it cannot be moved by another
  911. * rename.
  912. */
  913. if (xp != ip) {
  914. if (doingdirectory)
  915. panic("ext2fs_rename: lost dir entry");
  916. } else {
  917. /*
  918. * If the source is a directory with a
  919. * new parent, the link count of the old
  920. * parent directory must be decremented
  921. * and ".." set to point to the new parent.
  922. */
  923. if (doingdirectory && newparent) {
  924. KASSERT(dp != NULL);
  925. dp->i_e2fs_nlink--;
  926. dp->i_flag |= IN_CHANGE;
  927. error = vn_rdwr(UIO_READ, fvp, (void *)&dirbuf,
  928. sizeof (struct ext2fs_dirtemplate), (off_t)0,
  929. UIO_SYSSPACE, IO_NODELOCKED,
  930. tcnp->cn_cred, (size_t *)0, NULL);
  931. if (error == 0) {
  932. namlen = dirbuf.dotdot_namlen;
  933. if (namlen != 2 ||
  934. dirbuf.dotdot_name[0] != '.' ||
  935. dirbuf.dotdot_name[1] != '.') {
  936. ufs_dirbad(xp, (doff_t)12,
  937. "ext2fs_rename: mangled dir");
  938. } else {
  939. dirbuf.dotdot_ino = h2fs32(newparent);
  940. (void) vn_rdwr(UIO_WRITE, fvp,
  941. (void *)&dirbuf,
  942. sizeof (struct dirtemplate),
  943. (off_t)0, UIO_SYSSPACE,
  944. IO_NODELOCKED|IO_SYNC,
  945. tcnp->cn_cred, (size_t *)0,
  946. NULL);
  947. cache_purge(fdvp);
  948. }
  949. }
  950. }
  951. error = ext2fs_dirremove(fdvp, &VTOI(fdvp)->i_crap, fcnp);
  952. if (!error) {
  953. xp->i_e2fs_nlink--;
  954. xp->i_flag |= IN_CHANGE;
  955. }
  956. xp->i_flag &= ~IN_RENAME;
  957. }
  958. VN_KNOTE(fvp, NOTE_RENAME);
  959. if (dp)
  960. vput(fdvp);
  961. if (xp)
  962. vput(fvp);
  963. vrele(ap->a_fvp);
  964. return (error);
  965. bad:
  966. if (xp)
  967. vput(ITOV(xp));
  968. vput(ITOV(dp));
  969. out:
  970. if (doingdirectory)
  971. ip->i_flag &= ~IN_RENAME;
  972. if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
  973. ip->i_e2fs_nlink--;
  974. ip->i_flag |= IN_CHANGE;
  975. vput(fvp);
  976. } else
  977. vrele(fvp);
  978. vrele(fdvp);
  979. return (error);
  980. }
  981. /*
  982. * Mkdir system call
  983. */
  984. int
  985. ext2fs_mkdir(void *v)
  986. {
  987. struct vop_mkdir_args /* {
  988. struct vnode *a_dvp;
  989. struct vnode **a_vpp;
  990. struct componentname *a_cnp;
  991. struct vattr *a_vap;
  992. } */ *ap = v;
  993. struct vnode *dvp = ap->a_dvp;
  994. struct vattr *vap = ap->a_vap;
  995. struct componentname *cnp = ap->a_cnp;
  996. struct inode *ip, *dp = VTOI(dvp);
  997. struct vnode *tvp;
  998. struct ext2fs_dirtemplate dirtemplate;
  999. int error, dmode;
  1000. struct ufs_lookup_results *ulr;
  1001. /* XXX should handle this material another way */
  1002. ulr = &VTOI(dvp)->i_crap;
  1003. UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
  1004. if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
  1005. error = EMLINK;
  1006. goto out;
  1007. }
  1008. dmode = vap->va_mode & ACCESSPERMS;
  1009. dmode |= IFDIR;
  1010. /*
  1011. * Must simulate part of ext2fs_makeinode here to acquire the inode,
  1012. * but not have it entered in the parent directory. The entry is
  1013. * made later after writing "." and ".." entries.
  1014. */
  1015. if ((error = ext2fs_valloc(dvp, dmode, cnp->cn_cred, &tvp)) != 0)
  1016. goto out;
  1017. ip = VTOI(tvp);
  1018. ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
  1019. ip->i_e2fs_uid = ip->i_uid & 0xffff;
  1020. ip->i_e2fs_gid = dp->i_e2fs_gid;
  1021. if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
  1022. ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
  1023. ip->i_e2fs_gid_high = dp->i_e2fs_gid_high;
  1024. } else {
  1025. ip->i_e2fs_uid_high = 0;
  1026. ip->i_e2fs_gid_high = 0;
  1027. }
  1028. ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
  1029. ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
  1030. ip->i_e2fs_mode = dmode;
  1031. tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
  1032. ip->i_e2fs_nlink = 2;
  1033. /*
  1034. * Bump link count in parent directory
  1035. * to reflect work done below. Should
  1036. * be done before reference is created
  1037. * so reparation is possible if we crash.
  1038. */
  1039. dp->i_e2fs_nlink++;
  1040. dp->i_flag |= IN_CHANGE;
  1041. if ((error = ext2fs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
  1042. goto bad;
  1043. /* Initialize directory with "." and ".." from static template. */
  1044. memset(&dirtemplate, 0, sizeof(dirtemplate));
  1045. dirtemplate.dot_ino = h2fs32(ip->i_number);
  1046. dirtemplate.dot_reclen = h2fs16(12);
  1047. dirtemplate.dot_namlen = 1;
  1048. if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
  1049. (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
  1050. dirtemplate.dot_type = EXT2_FT_DIR;
  1051. }
  1052. dirtemplate.dot_name[0] = '.';
  1053. dirtemplate.dotdot_ino = h2fs32(dp->i_number);
  1054. dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
  1055. dirtemplate.dotdot_namlen = 2;
  1056. if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
  1057. (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
  1058. dirtemplate.dotdot_type = EXT2_FT_DIR;
  1059. }
  1060. dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
  1061. error = vn_rdwr(UIO_WRITE, tvp, (void *)&dirtemplate,
  1062. sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
  1063. IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (size_t *)0, NULL);
  1064. if (error) {
  1065. dp->i_e2fs_nlink--;
  1066. dp->i_flag |= IN_CHANGE;
  1067. goto bad;
  1068. }
  1069. if (VTOI(dvp)->i_e2fs->e2fs_bsize > dvp->v_mount->mnt_stat.f_bsize)
  1070. panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
  1071. else {
  1072. error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize);
  1073. if (error) {
  1074. dp->i_e2fs_nlink--;
  1075. dp->i_flag |= IN_CHANGE;
  1076. goto bad;
  1077. }
  1078. ip->i_flag |= IN_CHANGE;
  1079. uvm_vnp_setsize(tvp, ext2fs_size(ip));
  1080. }
  1081. /* Directory set up, now install it's entry in the parent directory. */
  1082. error = ext2fs_direnter(ip, dvp, ulr, cnp);
  1083. if (error != 0) {
  1084. dp->i_e2fs_nlink--;
  1085. dp->i_flag |= IN_CHANGE;
  1086. }
  1087. bad:
  1088. /*
  1089. * No need to do an explicit ext2fs_truncate here, vrele will do this
  1090. * for us because we set the link count to 0.
  1091. */
  1092. if (error) {
  1093. ip->i_e2fs_nlink = 0;
  1094. ip->i_flag |= IN_CHANGE;
  1095. vput(tvp);
  1096. } else {
  1097. VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
  1098. *ap->a_vpp = tvp;
  1099. }
  1100. out:
  1101. vput(dvp);
  1102. return (error);
  1103. }
  1104. /*
  1105. * Rmdir system call.
  1106. */
  1107. int
  1108. ext2fs_rmdir(void *v)
  1109. {
  1110. struct vop_rmdir_args /* {
  1111. struct vnode *a_dvp;
  1112. struct vnode *a_vp;
  1113. struct componentname *a_cnp;
  1114. } */ *ap = v;
  1115. struct vnode *vp = ap->a_vp;
  1116. struct vnode *dvp = ap->a_dvp;
  1117. struct componentname *cnp = ap->a_cnp;
  1118. struct inode *ip, *dp;
  1119. int error;
  1120. struct ufs_lookup_results *ulr;
  1121. ip = VTOI(vp);
  1122. dp = VTOI(dvp);
  1123. /* XXX should handle this material another way */
  1124. ulr = &dp->i_crap;
  1125. UFS_CHECK_CRAPCOUNTER(dp);
  1126. /*
  1127. * No rmdir "." please.
  1128. */
  1129. if (dp == ip) {
  1130. vrele(dvp);
  1131. vput(vp);
  1132. return (EINVAL);
  1133. }
  1134. /*
  1135. * Verify the directory is empty (and valid).
  1136. * (Rmdir ".." won't be valid since
  1137. * ".." will contain a reference to
  1138. * the current directory and thus be
  1139. * non-empty.)
  1140. */
  1141. error = 0;
  1142. if (ip->i_e2fs_nlink != 2 ||
  1143. !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
  1144. error = ENOTEMPTY;
  1145. goto out;
  1146. }
  1147. if ((dp->i_e2fs_flags & EXT2_APPEND) ||
  1148. (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
  1149. error = EPERM;
  1150. goto out;
  1151. }
  1152. /*
  1153. * Delete reference to directory before purging
  1154. * inode. If we crash in between, the directory
  1155. * will be reattached to lost+found,
  1156. */
  1157. error = ext2fs_dirremove(dvp, ulr, cnp);
  1158. if (error != 0)
  1159. goto out;
  1160. dp->i_e2fs_nlink--;
  1161. dp->i_flag |= IN_CHANGE;
  1162. VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
  1163. cache_purge(dvp);
  1164. vput(dvp);
  1165. dvp = NULL;
  1166. /*
  1167. * Truncate inode. The only stuff left
  1168. * in the directory is "." and "..". The
  1169. * "." reference is inconsequential since
  1170. * we're quashing it. The ".." reference
  1171. * has already been adjusted above. We've
  1172. * removed the "." reference and the reference
  1173. * in the parent directory, but there may be
  1174. * other hard links so decrement by 2 and
  1175. * worry about them later.
  1176. */
  1177. ip->i_e2fs_nlink -= 2;
  1178. error = ext2fs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
  1179. cache_purge(ITOV(ip));
  1180. out:
  1181. VN_KNOTE(vp, NOTE_DELETE);
  1182. if (dvp)
  1183. vput(dvp);
  1184. vput(vp);
  1185. return (error);
  1186. }
  1187. /*
  1188. * symlink -- make a symbolic link
  1189. */
  1190. int
  1191. ext2fs_symlink(void *v)
  1192. {
  1193. struct vop_symlink_args /* {
  1194. struct vnode *a_dvp;
  1195. struct vnode **a_vpp;
  1196. struct componentname *a_cnp;
  1197. struct vattr *a_vap;
  1198. char *a_target;
  1199. } */ *ap = v;
  1200. struct vnode *vp, **vpp;
  1201. struct inode *ip;
  1202. int len, error;
  1203. vpp = ap->a_vpp;
  1204. error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
  1205. vpp, ap->a_cnp);
  1206. if (error)
  1207. return (error);
  1208. VN_KNOTE(ap->a_dvp, NOTE_WRITE);
  1209. vp = *vpp;
  1210. len = strlen(ap->a_target);
  1211. ip = VTOI(vp);
  1212. if (len < ip->i_ump->um_maxsymlinklen) {
  1213. memcpy((char *)ip->i_din.e2fs_din->e2di_shortlink, ap->a_target, len);
  1214. error = ext2fs_setsize(ip, len);
  1215. if (error)
  1216. goto bad;
  1217. ip->i_flag |= IN_CHANGE | IN_UPDATE;
  1218. if (vp->v_mount->mnt_flag & MNT_RELATIME)
  1219. ip->i_flag |= IN_ACCESS;
  1220. uvm_vnp_setsize(vp, len);
  1221. } else
  1222. error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
  1223. UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
  1224. (size_t *)0, NULL);
  1225. bad:
  1226. if (error)
  1227. vput(vp);
  1228. return (error);
  1229. }
  1230. /*
  1231. * Return target name of a symbolic link
  1232. */
  1233. int
  1234. ext2fs_readlink(void *v)
  1235. {
  1236. struct vop_readlink_args /* {
  1237. struct vnode *a_vp;
  1238. struct uio *a_uio;
  1239. kauth_cred_t a_cred;
  1240. } */ *ap = v;
  1241. struct vnode *vp = ap->a_vp;
  1242. struct inode *ip = VTOI(vp);
  1243. struct ufsmount *ump = ip->i_ump;
  1244. int isize;
  1245. isize = ext2fs_size(ip);
  1246. if (isize < ump->um_maxsymlinklen ||
  1247. (ump->um_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) {
  1248. uiomove((char *)ip->i_din.e2fs_din->e2di_shortlink, isize, ap->a_uio);
  1249. return (0);
  1250. }
  1251. return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
  1252. }
  1253. /*
  1254. * Advisory record locking support
  1255. */
  1256. int
  1257. ext2fs_advlock(void *v)
  1258. {
  1259. struct vop_advlock_args /* {
  1260. struct vnode *a_vp;
  1261. void * a_id;
  1262. int a_op;
  1263. struct flock *a_fl;
  1264. int a_flags;
  1265. } */ *ap = v;
  1266. struct inode *ip = VTOI(ap->a_vp);
  1267. return lf_advlock(ap, &ip->i_lockf, ext2fs_size(ip));
  1268. }
  1269. int
  1270. ext2fs_fsync(void *v)
  1271. {
  1272. struct vop_fsync_args /* {
  1273. struct vnode *a_vp;
  1274. kauth_cred_t a_cred;
  1275. int a_flags;
  1276. off_t offlo;
  1277. off_t offhi;
  1278. struct proc *a_p;
  1279. } */ *ap = v;
  1280. struct vnode *vp = ap->a_vp;
  1281. int wait;
  1282. int error;
  1283. wait = (ap->a_flags & FSYNC_WAIT) != 0;
  1284. if (vp->v_type == VBLK)
  1285. error = spec_fsync(v);
  1286. else
  1287. error = vflushbuf(vp, wait);
  1288. if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0)
  1289. error = ext2fs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
  1290. if (error == 0 && ap->a_flags & FSYNC_CACHE) {
  1291. int l = 0;
  1292. error = VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
  1293. curlwp->l_cred);
  1294. }
  1295. return error;
  1296. }
  1297. /*
  1298. * Initialize the vnode associated with a new inode, handle aliased
  1299. * vnodes.
  1300. */
  1301. int
  1302. ext2fs_vinit(struct mount *mntp, int (**specops)(void *),
  1303. int (**fifoops)(void *), struct vnode **vpp)
  1304. {
  1305. struct timeval tv;
  1306. struct inode *ip;
  1307. struct vnode *vp;
  1308. vp = *vpp;
  1309. ip = VTOI(vp);
  1310. switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) {
  1311. case VCHR:
  1312. case VBLK:
  1313. vp->v_op = specops;
  1314. spec_node_init(vp, fs2h32(ip->i_din.e2fs_din->e2di_rdev));
  1315. break;
  1316. case VFIFO:
  1317. vp->v_op = fifoops;
  1318. break;
  1319. case VNON:
  1320. case VBAD:
  1321. case VSOCK:
  1322. case VLNK:
  1323. case VDIR:
  1324. case VREG:
  1325. break;
  1326. }
  1327. if (ip->i_number == ROOTINO)
  1328. vp->v_vflag |= VV_ROOT;
  1329. /*
  1330. * Initialize modrev times
  1331. */
  1332. getmicrouptime(&tv);
  1333. SETHIGH(ip->i_modrev, tv.tv_sec);
  1334. SETLOW(ip->i_modrev, tv.tv_usec * 4294);
  1335. *vpp = vp;
  1336. return (0);
  1337. }
  1338. /*
  1339. * Allocate a new inode.
  1340. */
  1341. int
  1342. ext2fs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
  1343. struct componentname *cnp)
  1344. {
  1345. struct inode *ip, *pdir;
  1346. struct vnode *tvp;
  1347. int error, ismember = 0;
  1348. struct ufs_lookup_results *ulr;
  1349. pdir = VTOI(dvp);
  1350. /* XXX should handle this material another way */
  1351. ulr = &pdir->i_crap;
  1352. UFS_CHECK_CRAPCOUNTER(pdir);
  1353. *vpp = NULL;
  1354. if ((mode & IFMT) == 0)
  1355. mode |= IFREG;
  1356. if ((error = ext2fs_valloc(dvp, mode, cnp->cn_cred, &tvp)) != 0) {
  1357. vput(dvp);
  1358. return (error);
  1359. }
  1360. ip = VTOI(tvp);
  1361. ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
  1362. ip->i_e2fs_uid = ip->i_uid & 0xffff;
  1363. ip->i_e2fs_gid = pdir->i_e2fs_gid;
  1364. if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
  1365. ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
  1366. ip->i_e2fs_gid_high = pdir->i_e2fs_gid_high;
  1367. } else {
  1368. ip->i_e2fs_uid_high = 0;
  1369. ip->i_e2fs_gid_high = 0;
  1370. }
  1371. ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
  1372. ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
  1373. ip->i_e2fs_mode = mode;
  1374. tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
  1375. ip->i_e2fs_nlink = 1;
  1376. if ((ip->i_e2fs_mode & ISGID) && (kauth_cred_ismember_gid(cnp->cn_cred,
  1377. ip->i_gid, &ismember) != 0 || !ismember) &&
  1378. kauth_authorize_generic(cnp->cn_cred, KAUTH_GENERIC_ISSUSER, NULL))
  1379. ip->i_e2fs_mode &= ~ISGID;
  1380. /*
  1381. * Make sure inode goes to disk before directory entry.
  1382. */
  1383. if ((error = ext2fs_update(tvp, NULL, NULL, UPDATE_WAIT)) != 0)
  1384. goto bad;
  1385. error = ext2fs_direnter(ip, dvp, ulr, cnp);
  1386. if (error != 0)
  1387. goto bad;
  1388. vput(dvp);
  1389. *vpp = tvp;
  1390. return (0);
  1391. bad:
  1392. /*
  1393. * Write error occurred trying to update the inode
  1394. * or the directory so must deallocate the inode.
  1395. */
  1396. tvp->v_type = VNON; /* Stop explosion if VBLK */
  1397. ip->i_e2fs_nlink = 0;
  1398. ip->i_flag |= IN_CHANGE;
  1399. vput(tvp);
  1400. vput(dvp);
  1401. return (error);
  1402. }
  1403. /*
  1404. * Reclaim an inode so that it can be used for other purposes.
  1405. */
  1406. int
  1407. ext2fs_reclaim(void *v)
  1408. {
  1409. struct vop_reclaim_args /* {
  1410. struct vnode *a_vp;
  1411. } */ *ap = v;
  1412. struct vnode *vp = ap->a_vp;
  1413. struct inode *ip = VTOI(vp);
  1414. int error;
  1415. /*
  1416. * The inode must be freed and updated before being removed
  1417. * from its hash chain. Other threads trying to gain a hold
  1418. * on the inode will be stalled because it is locked (VI_XLOCK).
  1419. */
  1420. if (ip->i_omode == 1 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
  1421. ext2fs_vfree(vp, ip->i_number, ip->i_e2fs_mode);
  1422. if ((error = ufs_reclaim(vp)) != 0)
  1423. return (error);
  1424. if (ip->i_din.e2fs_din != NULL)
  1425. pool_put(&ext2fs_dinode_pool, ip->i_din.e2fs_din);
  1426. genfs_node_destroy(vp);
  1427. pool_put(&ext2fs_inode_pool, vp->v_data);
  1428. vp->v_data = NULL;
  1429. return (0);
  1430. }
  1431. /* Global vfs data structures for ext2fs. */
  1432. int (**ext2fs_vnodeop_p)(void *);
  1433. const struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
  1434. { &vop_default_desc, vn_default_error },
  1435. { &vop_lookup_desc, ext2fs_lookup }, /* lookup */
  1436. { &vop_create_desc, ext2fs_create }, /* create */
  1437. { &vop_mknod_desc, ext2fs_mknod }, /* mknod */
  1438. { &vop_open_desc, ext2fs_open }, /* open */
  1439. { &vop_close_desc, ufs_close }, /* close */
  1440. { &vop_access_desc, ext2fs_access }, /* access */
  1441. { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
  1442. { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
  1443. { &vop_read_desc, ext2fs_read }, /* read */
  1444. { &vop_write_desc, ext2fs_write }, /* write */
  1445. { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
  1446. { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
  1447. { &vop_poll_desc, ufs_poll }, /* poll */
  1448. { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
  1449. { &vop_revoke_desc, ufs_revoke }, /* revoke */
  1450. { &vop_mmap_desc, ufs_mmap }, /* mmap */
  1451. { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
  1452. { &vop_seek_desc, ufs_seek }, /* seek */
  1453. { &vop_remove_desc, ext2fs_remove }, /* remove */
  1454. { &vop_link_desc, ext2fs_link }, /* link */
  1455. { &vop_rename_desc, ext2fs_rename }, /* rename */
  1456. { &vop_mkdir_desc, ext2fs_mkdir }, /* mkdir */
  1457. { &vop_rmdir_desc, ext2fs_rmdir }, /* rmdir */
  1458. { &vop_symlink_desc, ext2fs_symlink }, /* symlink */
  1459. { &vop_readdir_desc, ext2fs_readdir }, /* readdir */
  1460. { &vop_readlink_desc, ext2fs_readlink }, /* readlink */
  1461. { &vop_abortop_desc, ufs_abortop }, /* abortop */
  1462. { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
  1463. { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
  1464. { &vop_lock_desc, ufs_lock }, /* lock */
  1465. { &vop_unlock_desc, ufs_unlock }, /* unlock */
  1466. { &vop_bmap_desc, ext2fs_bmap }, /* bmap */
  1467. { &vop_strategy_desc, ufs_strategy }, /* strategy */
  1468. { &vop_print_desc, ufs_print }, /* print */
  1469. { &vop_islocked_desc, ufs_islocked }, /* islocked */
  1470. { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
  1471. { &vop_advlock_desc, ext2fs_advlock }, /* advlock */
  1472. { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
  1473. { &vop_getpages_desc, genfs_getpages }, /* getpages */
  1474. { &vop_putpages_desc, genfs_putpages }, /* putpages */
  1475. { NULL, NULL }
  1476. };
  1477. const struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
  1478. { &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
  1479. int (**ext2fs_specop_p)(void *);
  1480. const struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
  1481. { &vop_default_desc, vn_default_error },
  1482. { &vop_lookup_desc, spec_lookup }, /* lookup */
  1483. { &vop_create_desc, spec_create }, /* create */
  1484. { &vop_mknod_desc, spec_mknod }, /* mknod */
  1485. { &vop_open_desc, spec_open }, /* open */
  1486. { &vop_close_desc, ufsspec_close }, /* close */
  1487. { &vop_access_desc, ext2fs_access }, /* access */
  1488. { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
  1489. { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
  1490. { &vop_read_desc, ufsspec_read }, /* read */
  1491. { &vop_write_desc, ufsspec_write }, /* write */
  1492. { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
  1493. { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
  1494. { &vop_poll_desc, spec_poll }, /* poll */
  1495. { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
  1496. { &vop_revoke_desc, spec_revoke }, /* revoke */
  1497. { &vop_mmap_desc, spec_mmap }, /* mmap */
  1498. { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
  1499. { &vop_seek_desc, spec_seek }, /* seek */
  1500. { &vop_remove_desc, spec_remove }, /* remove */
  1501. { &vop_link_desc, spec_link }, /* link */
  1502. { &vop_rename_desc, spec_rename }, /* rename */
  1503. { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
  1504. { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
  1505. { &vop_symlink_desc, spec_symlink }, /* symlink */
  1506. { &vop_readdir_desc, spec_readdir }, /* readdir */
  1507. { &vop_readlink_desc, spec_readlink }, /* readlink */
  1508. { &vop_abortop_desc, spec_abortop }, /* abortop */
  1509. { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
  1510. { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
  1511. { &vop_lock_desc, ufs_lock }, /* lock */
  1512. { &vop_unlock_desc, ufs_unlock }, /* unlock */
  1513. { &vop_bmap_desc, spec_bmap }, /* bmap */
  1514. { &vop_strategy_desc, spec_strategy }, /* strategy */
  1515. { &vop_print_desc, ufs_print }, /* print */
  1516. { &vop_islocked_desc, ufs_islocked }, /* islocked */
  1517. { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
  1518. { &vop_advlock_desc, spec_advlock }, /* advlock */
  1519. { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
  1520. { &vop_getpages_desc, spec_getpages }, /* getpages */
  1521. { &vop_putpages_desc, spec_putpages }, /* putpages */
  1522. { NULL, NULL }
  1523. };
  1524. const struct vnodeopv_desc ext2fs_specop_opv_desc =
  1525. { &ext2fs_specop_p, ext2fs_specop_entries };
  1526. int (**ext2fs_fifoop_p)(void *);
  1527. const struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
  1528. { &vop_default_desc, vn_default_error },
  1529. { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
  1530. { &vop_create_desc, vn_fifo_bypass }, /* create */
  1531. { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
  1532. { &vop_open_desc, vn_fifo_bypass }, /* open */
  1533. { &vop_close_desc, ufsfifo_close }, /* close */
  1534. { &vop_access_desc, ext2fs_access }, /* access */
  1535. { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
  1536. { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
  1537. { &vop_read_desc, ufsfifo_read }, /* read */
  1538. { &vop_write_desc, ufsfifo_write }, /* write */
  1539. { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
  1540. { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
  1541. { &vop_poll_desc, vn_fifo_bypass }, /* poll */
  1542. { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
  1543. { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
  1544. { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
  1545. { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
  1546. { &vop_seek_desc, vn_fifo_bypass }, /* seek */
  1547. { &vop_remove_desc, vn_fifo_bypass }, /* remove */
  1548. { &vop_link_desc, vn_fifo_bypass }, /* link */
  1549. { &vop_rename_desc, vn_fifo_bypass }, /* rename */
  1550. { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
  1551. { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
  1552. { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
  1553. { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
  1554. { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
  1555. { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
  1556. { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
  1557. { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
  1558. { &vop_lock_desc, ufs_lock }, /* lock */
  1559. { &vop_unlock_desc, ufs_unlock }, /* unlock */
  1560. { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
  1561. { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
  1562. { &vop_print_desc, ufs_print }, /* print */
  1563. { &vop_islocked_desc, ufs_islocked }, /* islocked */
  1564. { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
  1565. { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
  1566. { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
  1567. { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
  1568. { NULL, NULL }
  1569. };
  1570. const struct vnodeopv_desc ext2fs_fifoop_opv_desc =
  1571. { &ext2fs_fifoop_p, ext2fs_fifoop_entries };