ext2fs_vfsops.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /* $OpenBSD: ext2fs_vfsops.c,v 1.85 2015/03/14 03:38:52 jsg Exp $ */
  2. /* $NetBSD: ext2fs_vfsops.c,v 1.1 1997/06/11 09:34:07 bouyer Exp $ */
  3. /*
  4. * Copyright (c) 1997 Manuel Bouyer.
  5. * Copyright (c) 1989, 1991, 1993, 1994
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
  33. * Modified for ext2fs by Manuel Bouyer.
  34. */
  35. #include <sys/param.h>
  36. #include <sys/systm.h>
  37. #include <sys/namei.h>
  38. #include <sys/proc.h>
  39. #include <sys/kernel.h>
  40. #include <sys/vnode.h>
  41. #include <sys/socket.h>
  42. #include <sys/mount.h>
  43. #include <sys/buf.h>
  44. #include <sys/disk.h>
  45. #include <sys/mbuf.h>
  46. #include <sys/file.h>
  47. #include <sys/disklabel.h>
  48. #include <sys/ioctl.h>
  49. #include <sys/errno.h>
  50. #include <sys/malloc.h>
  51. #include <sys/pool.h>
  52. #include <sys/lock.h>
  53. #include <sys/dkio.h>
  54. #include <sys/specdev.h>
  55. #include <ufs/ufs/quota.h>
  56. #include <ufs/ufs/ufsmount.h>
  57. #include <ufs/ufs/inode.h>
  58. #include <ufs/ufs/dir.h>
  59. #include <ufs/ufs/ufs_extern.h>
  60. #include <ufs/ext2fs/ext2fs.h>
  61. #include <ufs/ext2fs/ext2fs_extern.h>
  62. extern struct lock ufs_hashlock;
  63. int ext2fs_sbupdate(struct ufsmount *, int);
  64. static int e2fs_sbcheck(struct ext2fs *, int);
  65. const struct vfsops ext2fs_vfsops = {
  66. ext2fs_mount,
  67. ufs_start,
  68. ext2fs_unmount,
  69. ufs_root,
  70. ufs_quotactl,
  71. ext2fs_statfs,
  72. ext2fs_sync,
  73. ext2fs_vget,
  74. ext2fs_fhtovp,
  75. ext2fs_vptofh,
  76. ext2fs_init,
  77. ext2fs_sysctl,
  78. ufs_check_export
  79. };
  80. struct pool ext2fs_inode_pool;
  81. struct pool ext2fs_dinode_pool;
  82. extern u_long ext2gennumber;
  83. int
  84. ext2fs_init(struct vfsconf *vfsp)
  85. {
  86. pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, PR_WAITOK,
  87. "ext2inopl", NULL);
  88. pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0,
  89. PR_WAITOK, "ext2dinopl", NULL);
  90. return (ufs_init(vfsp));
  91. }
  92. /*
  93. * Called by main() when ext2fs is going to be mounted as root.
  94. *
  95. * Name is updated by mount(8) after booting.
  96. */
  97. #define ROOTNAME "root_device"
  98. int
  99. ext2fs_mountroot(void)
  100. {
  101. struct m_ext2fs *fs;
  102. struct mount *mp;
  103. struct proc *p = curproc; /* XXX */
  104. struct ufsmount *ump;
  105. int error;
  106. /*
  107. * Get vnodes for swapdev and rootdev.
  108. */
  109. if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
  110. panic("ext2fs_mountroot: can't setup bdevvp's");
  111. if ((error = vfs_rootmountalloc("ext2fs", "root_device", &mp)) != 0) {
  112. vrele(rootvp);
  113. return (error);
  114. }
  115. if ((error = ext2fs_mountfs(rootvp, mp, p)) != 0) {
  116. mp->mnt_vfc->vfc_refcount--;
  117. vfs_unbusy(mp);
  118. free(mp, M_MOUNT, sizeof *mp);
  119. vrele(rootvp);
  120. return (error);
  121. }
  122. TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
  123. ump = VFSTOUFS(mp);
  124. fs = ump->um_e2fs;
  125. memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
  126. strlcpy(fs->e2fs_fsmnt, mp->mnt_stat.f_mntonname, sizeof(fs->e2fs_fsmnt));
  127. if (fs->e2fs.e2fs_rev > E2FS_REV0) {
  128. memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
  129. strlcpy(fs->e2fs.e2fs_fsmnt, mp->mnt_stat.f_mntonname,
  130. sizeof(fs->e2fs.e2fs_fsmnt));
  131. }
  132. (void)ext2fs_statfs(mp, &mp->mnt_stat, p);
  133. vfs_unbusy(mp);
  134. inittodr(fs->e2fs.e2fs_wtime);
  135. return (0);
  136. }
  137. /*
  138. * VFS Operations.
  139. *
  140. * mount system call
  141. */
  142. int
  143. ext2fs_mount(struct mount *mp, const char *path, void *data,
  144. struct nameidata *ndp, struct proc *p)
  145. {
  146. struct vnode *devvp;
  147. struct ufs_args args;
  148. struct ufsmount *ump = NULL;
  149. struct m_ext2fs *fs;
  150. char fname[MNAMELEN];
  151. char fspec[MNAMELEN];
  152. int error, flags;
  153. mode_t accessmode;
  154. error = copyin(data, &args, sizeof(struct ufs_args));
  155. if (error)
  156. return (error);
  157. /*
  158. * If updating, check whether changing from read-only to
  159. * read/write; if there is no device name, that's all we do.
  160. */
  161. if (mp->mnt_flag & MNT_UPDATE) {
  162. ump = VFSTOUFS(mp);
  163. fs = ump->um_e2fs;
  164. if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
  165. flags = WRITECLOSE;
  166. if (mp->mnt_flag & MNT_FORCE)
  167. flags |= FORCECLOSE;
  168. error = ext2fs_flushfiles(mp, flags, p);
  169. if (error == 0 &&
  170. ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
  171. (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
  172. fs->e2fs.e2fs_state = E2FS_ISCLEAN;
  173. (void)ext2fs_sbupdate(ump, MNT_WAIT);
  174. }
  175. if (error)
  176. return (error);
  177. fs->e2fs_ronly = 1;
  178. }
  179. if (mp->mnt_flag & MNT_RELOAD) {
  180. error = ext2fs_reload(mp, ndp->ni_cnd.cn_cred, p);
  181. if (error)
  182. return (error);
  183. }
  184. if (fs->e2fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
  185. /*
  186. * If upgrade to read-write by non-root, then verify
  187. * that user has necessary permissions on the device.
  188. */
  189. if (suser(p, 0) != 0) {
  190. devvp = ump->um_devvp;
  191. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  192. error = VOP_ACCESS(devvp, VREAD | VWRITE,
  193. p->p_ucred, p);
  194. VOP_UNLOCK(devvp, 0, p);
  195. if (error)
  196. return (error);
  197. }
  198. fs->e2fs_ronly = 0;
  199. if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
  200. fs->e2fs.e2fs_state = 0;
  201. else
  202. fs->e2fs.e2fs_state = E2FS_ERRORS;
  203. fs->e2fs_fmod = 1;
  204. }
  205. if (args.fspec == NULL) {
  206. /*
  207. * Process export requests.
  208. */
  209. return (vfs_export(mp, &ump->um_export,
  210. &args.export_info));
  211. }
  212. }
  213. /*
  214. * Not an update, or updating the name: look up the name
  215. * and verify that it refers to a sensible block device.
  216. */
  217. error = copyinstr(args.fspec, fspec, sizeof(fspec), NULL);
  218. if (error)
  219. goto error;
  220. if (disk_map(fspec, fname, MNAMELEN, DM_OPENBLCK) == -1)
  221. memcpy(fname, fspec, sizeof(fname));
  222. NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, p);
  223. if ((error = namei(ndp)) != 0)
  224. goto error;
  225. devvp = ndp->ni_vp;
  226. if (devvp->v_type != VBLK) {
  227. error = ENOTBLK;
  228. goto error_devvp;
  229. }
  230. if (major(devvp->v_rdev) >= nblkdev) {
  231. error = ENXIO;
  232. goto error_devvp;
  233. }
  234. /*
  235. * If mount by non-root, then verify that user has necessary
  236. * permissions on the device.
  237. */
  238. if (suser(p, 0) != 0) {
  239. accessmode = VREAD;
  240. if ((mp->mnt_flag & MNT_RDONLY) == 0)
  241. accessmode |= VWRITE;
  242. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  243. error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
  244. VOP_UNLOCK(devvp, 0, p);
  245. if (error)
  246. goto error_devvp;
  247. }
  248. if ((mp->mnt_flag & MNT_UPDATE) == 0)
  249. error = ext2fs_mountfs(devvp, mp, p);
  250. else {
  251. if (devvp != ump->um_devvp)
  252. error = EINVAL; /* XXX needs translation */
  253. else
  254. vrele(devvp);
  255. }
  256. if (error)
  257. goto error_devvp;
  258. ump = VFSTOUFS(mp);
  259. fs = ump->um_e2fs;
  260. memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
  261. strlcpy(fs->e2fs_fsmnt, path, sizeof(fs->e2fs_fsmnt));
  262. if (fs->e2fs.e2fs_rev > E2FS_REV0) {
  263. memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
  264. strlcpy(fs->e2fs.e2fs_fsmnt, mp->mnt_stat.f_mntonname,
  265. sizeof(fs->e2fs.e2fs_fsmnt));
  266. }
  267. memcpy(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt, MNAMELEN);
  268. memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
  269. strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN);
  270. memset(mp->mnt_stat.f_mntfromspec, 0, MNAMELEN);
  271. strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
  272. if (fs->e2fs_fmod != 0) { /* XXX */
  273. fs->e2fs_fmod = 0;
  274. if (fs->e2fs.e2fs_state == 0)
  275. fs->e2fs.e2fs_wtime = time_second;
  276. else
  277. printf("%s: file system not clean; please fsck(8)\n",
  278. mp->mnt_stat.f_mntfromname);
  279. ext2fs_cgupdate(ump, MNT_WAIT);
  280. }
  281. goto success;
  282. error_devvp:
  283. /* Error with devvp held. */
  284. vrele(devvp);
  285. error:
  286. /* Error with no state to backout. */
  287. success:
  288. return (error);
  289. }
  290. int ext2fs_reload_vnode(struct vnode *, void *args);
  291. struct ext2fs_reload_args {
  292. struct m_ext2fs *fs;
  293. struct proc *p;
  294. struct ucred *cred;
  295. struct vnode *devvp;
  296. };
  297. int
  298. ext2fs_reload_vnode(struct vnode *vp, void *args)
  299. {
  300. struct ext2fs_reload_args *era = args;
  301. struct buf *bp;
  302. struct inode *ip;
  303. int error;
  304. caddr_t cp;
  305. /*
  306. * Step 4: invalidate all inactive vnodes.
  307. */
  308. if (vp->v_usecount == 0) {
  309. vgonel(vp, era->p);
  310. return (0);
  311. }
  312. /*
  313. * Step 5: invalidate all cached file data.
  314. */
  315. if (vget(vp, LK_EXCLUSIVE, era->p))
  316. return (0);
  317. if (vinvalbuf(vp, 0, era->cred, era->p, 0, 0))
  318. panic("ext2fs_reload: dirty2");
  319. /*
  320. * Step 6: re-read inode data for all active vnodes.
  321. */
  322. ip = VTOI(vp);
  323. error = bread(era->devvp,
  324. fsbtodb(era->fs, ino_to_fsba(era->fs, ip->i_number)),
  325. (int)era->fs->e2fs_bsize, &bp);
  326. if (error) {
  327. vput(vp);
  328. return (error);
  329. }
  330. cp = (caddr_t)bp->b_data +
  331. (ino_to_fsbo(era->fs, ip->i_number) * EXT2_DINODE_SIZE(era->fs));
  332. e2fs_iload(era->fs, (struct ext2fs_dinode *)cp, ip->i_e2din);
  333. brelse(bp);
  334. vput(vp);
  335. return (0);
  336. }
  337. static off_t
  338. ext2fs_maxfilesize(struct m_ext2fs *fs)
  339. {
  340. bool huge = fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_HUGE_FILE;
  341. off_t b = fs->e2fs_bsize / 4;
  342. off_t physically, logically;
  343. physically = dbtob(huge ? ((1ULL << 48) - 1) : UINT_MAX);
  344. logically = (12ULL + b + b*b + b*b*b) * fs->e2fs_bsize;
  345. return MIN(logically, physically);
  346. }
  347. static int
  348. e2fs_sbfill(struct vnode *devvp, struct m_ext2fs *fs)
  349. {
  350. struct buf *bp = NULL;
  351. int i, error;
  352. /* XXX assume hardware block size == 512 */
  353. fs->e2fs_ncg = howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
  354. fs->e2fs.e2fs_bpg);
  355. fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
  356. fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
  357. fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
  358. fs->e2fs_fsize = 1024 << fs->e2fs.e2fs_log_fsize;
  359. fs->e2fs_qbmask = fs->e2fs_bsize - 1;
  360. fs->e2fs_bmask = ~fs->e2fs_qbmask;
  361. fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE(fs);
  362. fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
  363. /* Re-read group descriptors from the disk. */
  364. fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
  365. fs->e2fs_bsize / sizeof(struct ext2_gd));
  366. fs->e2fs_gd = mallocarray(fs->e2fs_ngdb, fs->e2fs_bsize,
  367. M_UFSMNT, M_WAITOK);
  368. for (i = 0; i < fs->e2fs_ngdb; ++i) {
  369. daddr_t dblk = ((fs->e2fs_bsize > 1024) ? 0 : 1) + i + 1;
  370. size_t gdesc = i * fs->e2fs_bsize / sizeof(struct ext2_gd);
  371. struct ext2_gd *gd;
  372. error = bread(devvp, fsbtodb(fs, dblk), fs->e2fs_bsize, &bp);
  373. if (error) {
  374. size_t gdescs_space = fs->e2fs_ngdb * fs->e2fs_bsize;
  375. free(fs->e2fs_gd, M_UFSMNT, gdescs_space);
  376. fs->e2fs_gd = NULL;
  377. brelse(bp);
  378. return (error);
  379. }
  380. gd = (struct ext2_gd *) bp->b_data;
  381. e2fs_cgload(gd, fs->e2fs_gd + gdesc, fs->e2fs_bsize);
  382. brelse(bp);
  383. bp = NULL;
  384. }
  385. if ((fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE) == 0 ||
  386. (fs->e2fs.e2fs_rev == E2FS_REV0))
  387. fs->e2fs_maxfilesize = INT_MAX;
  388. else
  389. fs->e2fs_maxfilesize = ext2fs_maxfilesize(fs);
  390. if (fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_EXTENTS)
  391. fs->e2fs_maxfilesize *= 4;
  392. return (0);
  393. }
  394. /*
  395. * Reload all incore data for a filesystem (used after running fsck on
  396. * the root filesystem and finding things to fix). The filesystem must
  397. * be mounted read-only.
  398. *
  399. * Things to do to update the mount:
  400. * 1) invalidate all cached meta-data.
  401. * 2) re-read superblock from disk.
  402. * 3) re-read summary information from disk.
  403. * 4) invalidate all inactive vnodes.
  404. * 5) invalidate all cached file data.
  405. * 6) re-read inode data for all active vnodes.
  406. */
  407. int
  408. ext2fs_reload(struct mount *mountp, struct ucred *cred, struct proc *p)
  409. {
  410. struct vnode *devvp;
  411. struct buf *bp;
  412. struct m_ext2fs *fs;
  413. struct ext2fs *newfs;
  414. int error;
  415. struct ext2fs_reload_args era;
  416. if ((mountp->mnt_flag & MNT_RDONLY) == 0)
  417. return (EINVAL);
  418. /*
  419. * Step 1: invalidate all cached meta-data.
  420. */
  421. devvp = VFSTOUFS(mountp)->um_devvp;
  422. if (vinvalbuf(devvp, 0, cred, p, 0, 0))
  423. panic("ext2fs_reload: dirty1");
  424. /*
  425. * Step 2: re-read superblock from disk.
  426. */
  427. error = bread(devvp, (daddr_t)(SBOFF / DEV_BSIZE), SBSIZE, &bp);
  428. if (error) {
  429. brelse(bp);
  430. return (error);
  431. }
  432. newfs = (struct ext2fs *)bp->b_data;
  433. error = e2fs_sbcheck(newfs, (mountp->mnt_flag & MNT_RDONLY));
  434. if (error) {
  435. brelse(bp);
  436. return (error);
  437. }
  438. fs = VFSTOUFS(mountp)->um_e2fs;
  439. /*
  440. * Copy in the new superblock, compute in-memory values
  441. * and load group descriptors.
  442. */
  443. e2fs_sbload(newfs, &fs->e2fs);
  444. if ((error = e2fs_sbfill(devvp, fs)) != 0)
  445. return (error);
  446. era.p = p;
  447. era.cred = cred;
  448. era.fs = fs;
  449. era.devvp = devvp;
  450. error = vfs_mount_foreach_vnode(mountp, ext2fs_reload_vnode, &era);
  451. return (error);
  452. }
  453. /*
  454. * Common code for mount and mountroot
  455. */
  456. int
  457. ext2fs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
  458. {
  459. struct ufsmount *ump;
  460. struct buf *bp;
  461. struct ext2fs *fs;
  462. dev_t dev;
  463. int error, ronly;
  464. struct ucred *cred;
  465. dev = devvp->v_rdev;
  466. cred = p ? p->p_ucred : NOCRED;
  467. /*
  468. * Disallow multiple mounts of the same device.
  469. * Disallow mounting of a device that is currently in use
  470. * (except for root, which might share swap device for miniroot).
  471. * Flush out any old buffers remaining from a previous use.
  472. */
  473. if ((error = vfs_mountedon(devvp)) != 0)
  474. return (error);
  475. if (vcount(devvp) > 1 && devvp != rootvp)
  476. return (EBUSY);
  477. if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
  478. return (error);
  479. ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
  480. error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
  481. if (error)
  482. return (error);
  483. bp = NULL;
  484. ump = NULL;
  485. /*
  486. * Read the superblock from disk.
  487. */
  488. error = bread(devvp, (daddr_t)(SBOFF / DEV_BSIZE), SBSIZE, &bp);
  489. if (error)
  490. goto out;
  491. fs = (struct ext2fs *)bp->b_data;
  492. error = e2fs_sbcheck(fs, ronly);
  493. if (error)
  494. goto out;
  495. ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
  496. ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT,
  497. M_WAITOK | M_ZERO);
  498. /*
  499. * Copy in the superblock, compute in-memory values
  500. * and load group descriptors.
  501. */
  502. e2fs_sbload(fs, &ump->um_e2fs->e2fs);
  503. if ((error = e2fs_sbfill(devvp, ump->um_e2fs)) != 0)
  504. goto out;
  505. brelse(bp);
  506. bp = NULL;
  507. fs = &ump->um_e2fs->e2fs;
  508. ump->um_e2fs->e2fs_ronly = ronly;
  509. ump->um_fstype = UM_EXT2FS;
  510. if (ronly == 0) {
  511. if (fs->e2fs_state == E2FS_ISCLEAN)
  512. fs->e2fs_state = 0;
  513. else
  514. fs->e2fs_state = E2FS_ERRORS;
  515. ump->um_e2fs->e2fs_fmod = 1;
  516. }
  517. mp->mnt_data = (qaddr_t)ump;
  518. mp->mnt_stat.f_fsid.val[0] = (long)dev;
  519. mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
  520. mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
  521. mp->mnt_flag |= MNT_LOCAL;
  522. ump->um_mountp = mp;
  523. ump->um_dev = dev;
  524. ump->um_devvp = devvp;
  525. ump->um_nindir = NINDIR(ump->um_e2fs);
  526. ump->um_bptrtodb = ump->um_e2fs->e2fs_fsbtodb;
  527. ump->um_seqinc = 1; /* no frags */
  528. devvp->v_specmountpoint = mp;
  529. return (0);
  530. out:
  531. if (bp)
  532. brelse(bp);
  533. vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
  534. (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
  535. VOP_UNLOCK(devvp, 0, p);
  536. if (ump) {
  537. free(ump->um_e2fs, M_UFSMNT, sizeof *ump->um_e2fs);
  538. free(ump, M_UFSMNT, sizeof *ump);
  539. mp->mnt_data = (qaddr_t)0;
  540. }
  541. return (error);
  542. }
  543. /*
  544. * unmount system call
  545. */
  546. int
  547. ext2fs_unmount(struct mount *mp, int mntflags, struct proc *p)
  548. {
  549. struct ufsmount *ump;
  550. struct m_ext2fs *fs;
  551. int error, flags;
  552. size_t gdescs_space;
  553. flags = 0;
  554. if (mntflags & MNT_FORCE)
  555. flags |= FORCECLOSE;
  556. if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
  557. return (error);
  558. ump = VFSTOUFS(mp);
  559. fs = ump->um_e2fs;
  560. gdescs_space = fs->e2fs_ngdb * fs->e2fs_bsize;
  561. if (!fs->e2fs_ronly && ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
  562. (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
  563. fs->e2fs.e2fs_state = E2FS_ISCLEAN;
  564. (void) ext2fs_sbupdate(ump, MNT_WAIT);
  565. }
  566. if (ump->um_devvp->v_type != VBAD)
  567. ump->um_devvp->v_specmountpoint = NULL;
  568. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
  569. error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
  570. NOCRED, p);
  571. vput(ump->um_devvp);
  572. free(fs->e2fs_gd, M_UFSMNT, gdescs_space);
  573. free(fs, M_UFSMNT, sizeof *fs);
  574. free(ump, M_UFSMNT, sizeof *ump);
  575. mp->mnt_data = (qaddr_t)0;
  576. mp->mnt_flag &= ~MNT_LOCAL;
  577. return (error);
  578. }
  579. /*
  580. * Flush out all the files in a filesystem.
  581. */
  582. int
  583. ext2fs_flushfiles(struct mount *mp, int flags, struct proc *p)
  584. {
  585. struct ufsmount *ump;
  586. int error;
  587. ump = VFSTOUFS(mp);
  588. /*
  589. * Flush all the files.
  590. */
  591. if ((error = vflush(mp, NULL, flags)) != 0)
  592. return (error);
  593. /*
  594. * Flush filesystem metadata.
  595. */
  596. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
  597. error = VOP_FSYNC(ump->um_devvp, p->p_ucred, MNT_WAIT, p);
  598. VOP_UNLOCK(ump->um_devvp, 0, p);
  599. return (error);
  600. }
  601. /*
  602. * Get file system statistics.
  603. */
  604. int
  605. ext2fs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
  606. {
  607. struct ufsmount *ump;
  608. struct m_ext2fs *fs;
  609. u_int32_t overhead, overhead_per_group;
  610. int i, ngroups;
  611. ump = VFSTOUFS(mp);
  612. fs = ump->um_e2fs;
  613. if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
  614. panic("ext2fs_statfs");
  615. /*
  616. * Compute the overhead (FS structures)
  617. */
  618. overhead_per_group = 1 /* block bitmap */ + 1 /* inode bitmap */ +
  619. fs->e2fs_itpg;
  620. overhead = fs->e2fs.e2fs_first_dblock +
  621. fs->e2fs_ncg * overhead_per_group;
  622. if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
  623. fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
  624. for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
  625. if (cg_has_sb(i))
  626. ngroups++;
  627. }
  628. } else {
  629. ngroups = fs->e2fs_ncg;
  630. }
  631. overhead += ngroups * (1 + fs->e2fs_ngdb);
  632. sbp->f_bsize = fs->e2fs_bsize;
  633. sbp->f_iosize = fs->e2fs_bsize;
  634. sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
  635. sbp->f_bfree = fs->e2fs.e2fs_fbcount;
  636. sbp->f_bavail = sbp->f_bfree - fs->e2fs.e2fs_rbcount;
  637. sbp->f_files = fs->e2fs.e2fs_icount;
  638. sbp->f_ffree = fs->e2fs.e2fs_ficount;
  639. if (sbp != &mp->mnt_stat) {
  640. memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
  641. memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
  642. memcpy(sbp->f_mntfromspec, mp->mnt_stat.f_mntfromspec, MNAMELEN);
  643. }
  644. strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
  645. return (0);
  646. }
  647. int ext2fs_sync_vnode(struct vnode *vp, void *);
  648. struct ext2fs_sync_args {
  649. int allerror;
  650. int waitfor;
  651. struct proc *p;
  652. struct ucred *cred;
  653. };
  654. int
  655. ext2fs_sync_vnode(struct vnode *vp, void *args)
  656. {
  657. struct ext2fs_sync_args *esa = args;
  658. struct inode *ip;
  659. int error;
  660. ip = VTOI(vp);
  661. if (vp->v_type == VNON ||
  662. ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
  663. LIST_EMPTY(&vp->v_dirtyblkhd)) ||
  664. esa->waitfor == MNT_LAZY) {
  665. return (0);
  666. }
  667. if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT, esa->p))
  668. return (0);
  669. if ((error = VOP_FSYNC(vp, esa->cred, esa->waitfor, esa->p)) != 0)
  670. esa->allerror = error;
  671. vput(vp);
  672. return (0);
  673. }
  674. /*
  675. * Go through the disk queues to initiate sandbagged IO;
  676. * go through the inodes to write those that have been modified;
  677. * initiate the writing of the super block if it has been modified.
  678. *
  679. * Should always be called with the mount point locked.
  680. */
  681. int
  682. ext2fs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
  683. {
  684. struct ufsmount *ump = VFSTOUFS(mp);
  685. struct m_ext2fs *fs;
  686. int error, allerror = 0;
  687. struct ext2fs_sync_args esa;
  688. fs = ump->um_e2fs;
  689. if (fs->e2fs_ronly != 0) { /* XXX */
  690. printf("fs = %s\n", fs->e2fs_fsmnt);
  691. panic("update: rofs mod");
  692. }
  693. /*
  694. * Write back each (modified) inode.
  695. */
  696. esa.p = p;
  697. esa.cred = cred;
  698. esa.allerror = 0;
  699. esa.waitfor = waitfor;
  700. vfs_mount_foreach_vnode(mp, ext2fs_sync_vnode, &esa);
  701. if (esa.allerror != 0)
  702. allerror = esa.allerror;
  703. /*
  704. * Force stale file system control information to be flushed.
  705. */
  706. if (waitfor != MNT_LAZY) {
  707. vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
  708. if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
  709. allerror = error;
  710. VOP_UNLOCK(ump->um_devvp, 0, p);
  711. }
  712. /*
  713. * Write back modified superblock.
  714. */
  715. if (fs->e2fs_fmod != 0) {
  716. fs->e2fs_fmod = 0;
  717. fs->e2fs.e2fs_wtime = time_second;
  718. if ((error = ext2fs_cgupdate(ump, waitfor)))
  719. allerror = error;
  720. }
  721. return (allerror);
  722. }
  723. /*
  724. * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
  725. * in from disk. If it is in core, wait for the lock bit to clear, then
  726. * return the inode locked. Detection and handling of mount points must be
  727. * done by the calling routine.
  728. */
  729. int
  730. ext2fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
  731. {
  732. struct m_ext2fs *fs;
  733. struct inode *ip;
  734. struct ext2fs_dinode *dp;
  735. struct ufsmount *ump;
  736. struct buf *bp;
  737. struct vnode *vp;
  738. dev_t dev;
  739. int error;
  740. if (ino > (ufsino_t)-1)
  741. panic("ext2fs_vget: alien ino_t %llu",
  742. (unsigned long long)ino);
  743. ump = VFSTOUFS(mp);
  744. dev = ump->um_dev;
  745. retry:
  746. if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
  747. return (0);
  748. /* Allocate a new vnode/inode. */
  749. if ((error = getnewvnode(VT_EXT2FS, mp, &ext2fs_vops, &vp)) != 0) {
  750. *vpp = NULL;
  751. return (error);
  752. }
  753. ip = pool_get(&ext2fs_inode_pool, PR_WAITOK|PR_ZERO);
  754. lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
  755. vp->v_data = ip;
  756. ip->i_vnode = vp;
  757. ip->i_ump = ump;
  758. ip->i_e2fs = fs = ump->um_e2fs;
  759. ip->i_dev = dev;
  760. ip->i_number = ino;
  761. ip->i_e2fs_last_lblk = 0;
  762. ip->i_e2fs_last_blk = 0;
  763. /*
  764. * Put it onto its hash chain and lock it so that other requests for
  765. * this inode will block if they arrive while we are sleeping waiting
  766. * for old data structures to be purged or for the contents of the
  767. * disk portion of this inode to be read.
  768. */
  769. error = ufs_ihashins(ip);
  770. if (error) {
  771. vrele(vp);
  772. if (error == EEXIST)
  773. goto retry;
  774. return (error);
  775. }
  776. /* Read in the disk contents for the inode, copy into the inode. */
  777. error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
  778. (int)fs->e2fs_bsize, &bp);
  779. if (error) {
  780. /*
  781. * The inode does not contain anything useful, so it would
  782. * be misleading to leave it on its hash chain. With mode
  783. * still zero, it will be unlinked and returned to the free
  784. * list by vput().
  785. */
  786. vput(vp);
  787. brelse(bp);
  788. *vpp = NULL;
  789. return (error);
  790. }
  791. dp = (struct ext2fs_dinode *) ((char *)bp->b_data
  792. + EXT2_DINODE_SIZE(fs) * ino_to_fsbo(fs, ino));
  793. ip->i_e2din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
  794. e2fs_iload(fs, dp, ip->i_e2din);
  795. brelse(bp);
  796. ip->i_effnlink = ip->i_e2fs_nlink;
  797. /*
  798. * The fields for storing the UID and GID of an ext2fs inode are
  799. * limited to 16 bits. To overcome this limitation, Linux decided to
  800. * scatter the highest bits of these values into a previously reserved
  801. * area on the disk inode. We deal with this situation by having two
  802. * 32-bit fields *out* of the disk inode to hold the complete values.
  803. * Now that we are reading in the inode, compute these fields.
  804. */
  805. ip->i_e2fs_uid = ip->i_e2fs_uid_low | (ip->i_e2fs_uid_high << 16);
  806. ip->i_e2fs_gid = ip->i_e2fs_gid_low | (ip->i_e2fs_gid_high << 16);
  807. /* If the inode was deleted, reset all fields */
  808. if (ip->i_e2fs_dtime != 0) {
  809. ip->i_e2fs_mode = ip->i_e2fs_nblock = 0;
  810. (void)ext2fs_setsize(ip, 0);
  811. }
  812. /*
  813. * Initialize the vnode from the inode, check for aliases.
  814. * Note that the underlying vnode may have changed.
  815. */
  816. error = ext2fs_vinit(mp, &ext2fs_specvops, EXT2FS_FIFOOPS, &vp);
  817. if (error) {
  818. vput(vp);
  819. *vpp = NULL;
  820. return (error);
  821. }
  822. /*
  823. * Finish inode initialization now that aliasing has been resolved.
  824. */
  825. vref(ip->i_devvp);
  826. /*
  827. * Set up a generation number for this inode if it does not
  828. * already have one. This should only happen on old filesystems.
  829. */
  830. if (ip->i_e2fs_gen == 0) {
  831. if (++ext2gennumber < (u_long)time_second)
  832. ext2gennumber = time_second;
  833. ip->i_e2fs_gen = ext2gennumber;
  834. if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
  835. ip->i_flag |= IN_MODIFIED;
  836. }
  837. *vpp = vp;
  838. return (0);
  839. }
  840. /*
  841. * File handle to vnode
  842. *
  843. * Have to be really careful about stale file handles:
  844. * - check that the inode number is valid
  845. * - call ext2fs_vget() to get the locked inode
  846. * - check for an unallocated inode (i_mode == 0)
  847. * - check that the given client host has export rights and return
  848. * those rights via. exflagsp and credanonp
  849. */
  850. int
  851. ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
  852. {
  853. struct inode *ip;
  854. struct vnode *nvp;
  855. int error;
  856. struct ufid *ufhp;
  857. struct m_ext2fs *fs;
  858. ufhp = (struct ufid *)fhp;
  859. fs = VFSTOUFS(mp)->um_e2fs;
  860. if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
  861. ufhp->ufid_ino > fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
  862. return (ESTALE);
  863. if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
  864. *vpp = NULLVP;
  865. return (error);
  866. }
  867. ip = VTOI(nvp);
  868. if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
  869. ip->i_e2fs_gen != ufhp->ufid_gen) {
  870. vput(nvp);
  871. *vpp = NULLVP;
  872. return (ESTALE);
  873. }
  874. *vpp = nvp;
  875. return (0);
  876. }
  877. /*
  878. * Vnode pointer to File handle
  879. */
  880. /* ARGSUSED */
  881. int
  882. ext2fs_vptofh(struct vnode *vp, struct fid *fhp)
  883. {
  884. struct inode *ip;
  885. struct ufid *ufhp;
  886. ip = VTOI(vp);
  887. ufhp = (struct ufid *)fhp;
  888. ufhp->ufid_len = sizeof(struct ufid);
  889. ufhp->ufid_ino = ip->i_number;
  890. ufhp->ufid_gen = ip->i_e2fs_gen;
  891. return (0);
  892. }
  893. /*
  894. * no sysctl for ext2fs
  895. */
  896. int
  897. ext2fs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
  898. void *newp, size_t newlen, struct proc *p)
  899. {
  900. return (EOPNOTSUPP);
  901. }
  902. /*
  903. * Write a superblock and associated information back to disk.
  904. */
  905. int
  906. ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
  907. {
  908. struct m_ext2fs *fs = mp->um_e2fs;
  909. struct buf *bp;
  910. int error = 0;
  911. bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
  912. e2fs_sbsave(&fs->e2fs, (struct ext2fs *) bp->b_data);
  913. if (waitfor == MNT_WAIT)
  914. error = bwrite(bp);
  915. else
  916. bawrite(bp);
  917. return (error);
  918. }
  919. int
  920. ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
  921. {
  922. struct m_ext2fs *fs = mp->um_e2fs;
  923. struct buf *bp;
  924. int i, error = 0, allerror = 0;
  925. allerror = ext2fs_sbupdate(mp, waitfor);
  926. for (i = 0; i < fs->e2fs_ngdb; i++) {
  927. bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
  928. fs->e2fs_bsize, 0, 0);
  929. e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)], (struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
  930. if (waitfor == MNT_WAIT)
  931. error = bwrite(bp);
  932. else
  933. bawrite(bp);
  934. }
  935. if (!allerror && error)
  936. allerror = error;
  937. return (allerror);
  938. }
  939. /* This is called before the superblock is copied. Watch out for endianity! */
  940. static int
  941. e2fs_sbcheck(struct ext2fs *fs, int ronly)
  942. {
  943. u_int32_t tmp;
  944. tmp = letoh16(fs->e2fs_magic);
  945. if (tmp != E2FS_MAGIC) {
  946. printf("ext2fs: wrong magic number 0x%x\n", tmp);
  947. return (EIO); /* XXX needs translation */
  948. }
  949. tmp = letoh32(fs->e2fs_log_bsize);
  950. if (tmp > 2) {
  951. /* skewed log(block size): 1024 -> 0 | 2048 -> 1 | 4096 -> 2 */
  952. tmp += 10;
  953. printf("ext2fs: wrong log2(block size) %d\n", tmp);
  954. return (EIO); /* XXX needs translation */
  955. }
  956. if (fs->e2fs_bpg == 0) {
  957. printf("ext2fs: zero blocks per group\n");
  958. return (EIO);
  959. }
  960. tmp = letoh32(fs->e2fs_rev);
  961. if (tmp > E2FS_REV1) {
  962. printf("ext2fs: wrong revision number 0x%x\n", tmp);
  963. return (EIO); /* XXX needs translation */
  964. }
  965. else if (tmp == E2FS_REV0)
  966. return (0);
  967. tmp = letoh32(fs->e2fs_first_ino);
  968. if (tmp != EXT2_FIRSTINO) {
  969. printf("ext2fs: first inode at 0x%x\n", tmp);
  970. return (EINVAL); /* XXX needs translation */
  971. }
  972. tmp = letoh32(fs->e2fs_features_incompat);
  973. if (tmp & ~(EXT2F_INCOMPAT_SUPP | EXT4F_RO_INCOMPAT_SUPP)) {
  974. printf("ext2fs: unsupported incompat features 0x%x\n", tmp);
  975. return (EINVAL); /* XXX needs translation */
  976. }
  977. if (!ronly && (tmp & EXT4F_RO_INCOMPAT_SUPP)) {
  978. printf("ext4fs: only read-only support right now\n");
  979. return (EROFS); /* XXX needs translation */
  980. }
  981. if (tmp & EXT2F_INCOMPAT_RECOVER) {
  982. printf("ext2fs: your file system says it needs recovery\n");
  983. if (!ronly)
  984. return (EROFS); /* XXX needs translation */
  985. }
  986. tmp = letoh32(fs->e2fs_features_rocompat);
  987. if (!ronly && (tmp & ~EXT2F_ROCOMPAT_SUPP)) {
  988. printf("ext2fs: unsupported R/O compat features 0x%x\n", tmp);
  989. return (EROFS); /* XXX needs translation */
  990. }
  991. return (0);
  992. }